forked from hailinzeng/Programming-POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwlock.c
276 lines (253 loc) · 6.83 KB
/
rwlock.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* rwlock.c
*
* This file implements the "read-write lock" synchronization
* construct.
*
* A read-write lock allows a thread to lock shared data either
* for shared read access or exclusive write access.
*
* The rwl_init() and rwl_destroy() functions, respectively,
* allow you to initialize/create and destroy/free the
* read-write lock.
*
* The rwl_readlock() function locks a read-write lock for
* shared read access, and rwl_readunlock() releases the
* lock. rwl_readtrylock() attempts to lock a read-write lock
* for read access, and returns EBUSY instead of blocking.
*
* The rwl_writelock() function locks a read-write lock for
* exclusive write access, and rwl_writeunlock() releases the
* lock. rwl_writetrylock() attempts to lock a read-write lock
* for write access, and returns EBUSY instead of blocking.
*/
#include <pthread.h>
#include "errors.h"
#include "rwlock.h"
/*
* Initialize a read-write lock
*/
int rwl_init (rwlock_t *rwl)
{
int status;
rwl->r_active = 0;
rwl->r_wait = rwl->w_wait = 0;
rwl->w_active = 0;
status = pthread_mutex_init (&rwl->mutex, NULL);
if (status != 0)
return status;
status = pthread_cond_init (&rwl->read, NULL);
if (status != 0) {
/* if unable to create read CV, destroy mutex */
pthread_mutex_destroy (&rwl->mutex);
return status;
}
status = pthread_cond_init (&rwl->write, NULL);
if (status != 0) {
/* if unable to create write CV, destroy read CV and mutex */
pthread_cond_destroy (&rwl->read);
pthread_mutex_destroy (&rwl->mutex);
return status;
}
rwl->valid = RWLOCK_VALID;
return 0;
}
/*
* Destroy a read-write lock
*/
int rwl_destroy (rwlock_t *rwl)
{
int status, status1, status2;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
/*
* Check whether any threads own the lock; report "BUSY" if
* so.
*/
if (rwl->r_active > 0 || rwl->w_active) {
pthread_mutex_unlock (&rwl->mutex);
return EBUSY;
}
/*
* Check whether any threads are known to be waiting; report
* EBUSY if so.
*/
if (rwl->r_wait != 0 || rwl->w_wait != 0) {
pthread_mutex_unlock (&rwl->mutex);
return EBUSY;
}
rwl->valid = 0;
status = pthread_mutex_unlock (&rwl->mutex);
if (status != 0)
return status;
status = pthread_mutex_destroy (&rwl->mutex);
status1 = pthread_cond_destroy (&rwl->read);
status2 = pthread_cond_destroy (&rwl->write);
return (status == 0 ? status : (status1 == 0 ? status1 : status2));
}
/*
* Handle cleanup when the read lock condition variable
* wait is cancelled.
*
* Simply record that the thread is no longer waiting,
* and unlock the mutex.
*/
static void rwl_readcleanup (void *arg)
{
rwlock_t *rwl = (rwlock_t *)arg;
rwl->r_wait--;
pthread_mutex_unlock (&rwl->mutex);
}
/*
* Lock a read-write lock for read access.
*/
int rwl_readlock (rwlock_t *rwl)
{
int status;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
if (rwl->w_active) {
rwl->r_wait++;
pthread_cleanup_push (rwl_readcleanup, (void*)rwl);
while (rwl->w_active) {
status = pthread_cond_wait (&rwl->read, &rwl->mutex);
if (status != 0)
break;
}
pthread_cleanup_pop (0);
rwl->r_wait--;
}
if (status == 0)
rwl->r_active++;
pthread_mutex_unlock (&rwl->mutex);
return status;
}
/*
* Attempt to lock a read-write lock for read access (don't
* block if unavailable).
*/
int rwl_readtrylock (rwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
if (rwl->w_active)
status = EBUSY;
else
rwl->r_active++;
status2 = pthread_mutex_unlock (&rwl->mutex);
return (status2 != 0 ? status2 : status);
}
/*
* Unlock a read-write lock from read access.
*/
int rwl_readunlock (rwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
rwl->r_active--;
if (rwl->r_active == 0 && rwl->w_wait > 0)
status = pthread_cond_signal (&rwl->write);
status2 = pthread_mutex_unlock (&rwl->mutex);
return (status2 == 0 ? status : status2);
}
/*
* Handle cleanup when the write lock condition variable
* wait is cancelled.
*
* Simply record that the thread is no longer waiting,
* and unlock the mutex.
*/
static void rwl_writecleanup (void *arg)
{
rwlock_t *rwl = (rwlock_t *)arg;
rwl->w_wait--;
pthread_mutex_unlock (&rwl->mutex);
}
/*
* Lock a read-write lock for write access.
*/
int rwl_writelock (rwlock_t *rwl)
{
int status;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
if (rwl->w_active || rwl->r_active > 0) {
rwl->w_wait++;
pthread_cleanup_push (rwl_writecleanup, (void*)rwl);
while (rwl->w_active || rwl->r_active > 0) {
status = pthread_cond_wait (&rwl->write, &rwl->mutex);
if (status != 0)
break;
}
pthread_cleanup_pop (0);
rwl->w_wait--;
}
if (status == 0)
rwl->w_active = 1;
pthread_mutex_unlock (&rwl->mutex);
return status;
}
/*
* Attempt to lock a read-write lock for write access. Don't
* block if unavailable.
*/
int rwl_writetrylock (rwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
if (rwl->w_active || rwl->r_active > 0)
status = EBUSY;
else
rwl->w_active = 1;
status2 = pthread_mutex_unlock (&rwl->mutex);
return (status != 0 ? status : status2);
}
/*
* Unlock a read-write lock from write access.
*/
int rwl_writeunlock (rwlock_t *rwl)
{
int status;
if (rwl->valid != RWLOCK_VALID)
return EINVAL;
status = pthread_mutex_lock (&rwl->mutex);
if (status != 0)
return status;
rwl->w_active = 0;
if (rwl->r_wait > 0) {
status = pthread_cond_broadcast (&rwl->read);
if (status != 0) {
pthread_mutex_unlock (&rwl->mutex);
return status;
}
} else if (rwl->w_wait > 0) {
status = pthread_cond_signal (&rwl->write);
if (status != 0) {
pthread_mutex_unlock (&rwl->mutex);
return status;
}
}
status = pthread_mutex_unlock (&rwl->mutex);
return status;
}