-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwmutex.c
227 lines (180 loc) · 5.17 KB
/
rwmutex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// rwmutex.c
//
// Created by Hussian Alamri
//
// Last Updated: Fri Jan 04, 2008 @ 10:35 pm
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "rwmutex.h"
rwmutex_t *create_rwmutex(int N)
{
if(N < 1)
{
printf("Error, lock value must be greater than 0\n");
exit(-1);
}
rwmutex_t *ReadWriteMutex = (rwmutex_t *)malloc(sizeof(rwmutex_t));
ReadWriteMutex->readersActive = 0;
ReadWriteMutex->writersActive = 0;
ReadWriteMutex->readersWaiting = 0;
ReadWriteMutex->writersWaiting = 0;
ReadWriteMutex->priorityValue = N;
ReadWriteMutex->ReaderThreadIDs = NULL;
if(pthread_mutex_init(&ReadWriteMutex->ReadWriteLock, NULL) != 0) {
perror("Could not initialize mutex");
exit(2);
}
if(pthread_cond_init(&ReadWriteMutex->readCondition, NULL) != 0) {
perror("Could not initialize read Condition");
exit(2);
}
if(pthread_cond_init(&ReadWriteMutex->writeCondition, NULL) != 0) {
perror("Could not initialize write Condition");
exit(2);
}
return ReadWriteMutex;
}
void read_lock(rwmutex_t *ReadWriteMutex)
{
list_search(&n, pthread_self());
pause();
if(!pthread_equal(n->data, pthread_self()))
{
pthread_mutex_lock(&ReadWriteMutex->ReadWriteLock);
#ifdef LOG
printf("Locking read\n");
#endif
if(ReadWriteMutex->writersActive > 0 || ReadWriteMutex->writersWaiting > 0)
{
ReadWriteMutex->readersWaiting++;
#ifdef LOG
printf("readers waiting %d\n",ReadWriteMutex->readersWaiting);
#endif
while (ReadWriteMutex-> writersActive > 0)
pthread_cond_wait(&ReadWriteMutex->readCondition, &ReadWriteMutex->ReadWriteLock);
ReadWriteMutex->readersWaiting--;
#ifdef LOG
printf("readers waiting %d\n",ReadWriteMutex->readersWaiting);
#endif
}
ReadWriteMutex->readersActive++;
list_add(&n, pthread_self());
list_print(&n);
#ifdef LOG
printf("readers active %d\n",ReadWriteMutex->readersActive);
#endif
pthread_mutex_unlock(&ReadWriteMutex->ReadWriteLock);
}
return;
}
void read_unlock(rwmutex_t *ReadWriteMutex)
{
if(ReadWriteMutex->readersActive > 0)
{
list_search(&n, pthread_self());
if(pthread_equal(n->data, pthread_self()))
{
list_remove(&n, list_search(&n, pthread_self()));
pthread_mutex_lock(&ReadWriteMutex->ReadWriteLock);
ReadWriteMutex->readersActive--;
#ifdef LOG
printf("Unlocking read\n");
#endif
if(ReadWriteMutex->readersActive == 0 && ReadWriteMutex->writersWaiting>0)
pthread_cond_signal(&ReadWriteMutex->writeCondition);
else
pthread_cond_broadcast(&ReadWriteMutex->readCondition);
pthread_mutex_unlock(&ReadWriteMutex->ReadWriteLock);
}
}
return;
}
void write_lock(rwmutex_t *ReadWriteMutex)
{
if(!pthread_equal(ReadWriteMutex->lockingThread, pthread_self()))
{
pthread_mutex_lock(&ReadWriteMutex->ReadWriteLock);
#ifdef LOG
printf("Locking Write\n");
#endif
if(ReadWriteMutex->writersActive > 0 || ReadWriteMutex->readersActive > 0)
{
ReadWriteMutex->writersWaiting++;
#ifdef LOG
printf("writers waiting %d\n",ReadWriteMutex->writersWaiting);
#endif
while(ReadWriteMutex->writersActive > 0 || ReadWriteMutex->readersActive > 0)
pthread_cond_wait(&ReadWriteMutex->writeCondition,
&ReadWriteMutex->ReadWriteLock);
ReadWriteMutex->writersWaiting--;
}
#ifdef LOG
printf("Write Locked\n");
#endif
ReadWriteMutex->writersActive = 1;
ReadWriteMutex->lockingThread = pthread_self();
pthread_mutex_unlock(&ReadWriteMutex->ReadWriteLock);
}
return;
}
void write_unlock(rwmutex_t *ReadWriteMutex)
{
if(ReadWriteMutex->writersActive > 0)
{
if(pthread_equal(ReadWriteMutex->lockingThread, pthread_self()))
{
pthread_mutex_lock(&ReadWriteMutex->ReadWriteLock);
#ifdef LOG
printf("Unlocking Write\n");
#endif
ReadWriteMutex->writersActive = 0;
ReadWriteMutex->lockingThread = 0;
if(ReadWriteMutex->readersWaiting < ReadWriteMutex->priorityValue)
if(pthread_cond_signal(&ReadWriteMutex->writeCondition)!= 0) {
perror("Error while signaling");
exit(2);
}
else if(ReadWriteMutex->readersWaiting >= ReadWriteMutex->priorityValue)
if(pthread_cond_broadcast(&ReadWriteMutex->readCondition) != 0) {
perror("Error while broadcasting");
exit(2);
}
#ifdef LOG
printf("Write Unlocked\n");
#endif
pthread_mutex_unlock(&ReadWriteMutex->ReadWriteLock);
}
}
return;
}
/* Linked List Implementation copied from http://en.wikipedia.org/wiki/Linked_list with modifications*/
/* All text is available under the terms of the GNU Free Documentation License */
node *list_add(node **p, pthread_t i) {
/* some compilers don't require a cast of return value for malloc */
node *n = (node *)malloc(sizeof(node));
if (n == NULL)
return NULL;
n->next = *p;
*p = n;
n->data = i;
return n;
}
void list_remove(node **p) { /* remove head */
if (*p != NULL) {
node *n = *p;
*p = (*p)->next;
free(n);
}
}
node **list_search(node **n, pthread_t i) {
while (*n != NULL) {
if (pthread_equal((*n)->data,i)) {
return n;
}
n = &(*n)->next;
}
return NULL;
}