/
rwlock.cc
318 lines (288 loc) · 8.25 KB
/
rwlock.cc
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2001-2011 Free Software Foundation Europe e.V.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* BAREOS Thread Read/Write locking code. It permits
* multiple readers but only one writer. Note, however,
* that the writer thread is permitted to make multiple
* nested write lock calls.
*
* Kern Sibbald, January MMI
*
* This code adapted from "Programming with POSIX Threads", by David R. Butenhof
*/
#define _LOCKMGR_COMPLIANT
#include "include/bareos.h"
/*
* Initialize a read/write lock
*
* Returns: 0 on success
* errno on failure
*/
int RwlInit(brwlock_t *rwl, int priority)
{
int status;
rwl->r_active = rwl->w_active = 0;
rwl->r_wait = rwl->w_wait = 0;
rwl->priority = priority;
if ((status = pthread_mutex_init(&rwl->mutex, NULL)) != 0) {
return status;
}
if ((status = pthread_cond_init(&rwl->read, NULL)) != 0) {
pthread_mutex_destroy(&rwl->mutex);
return status;
}
if ((status = pthread_cond_init(&rwl->write, NULL)) != 0) {
pthread_cond_destroy(&rwl->read);
pthread_mutex_destroy(&rwl->mutex);
return status;
}
rwl->valid = RWLOCK_VALID;
return 0;
}
/*
* Destroy a read/write lock
*
* Returns: 0 on success
* errno on failure
*/
int RwlDestroy(brwlock_t *rwl)
{
int status, status1, status2;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
/*
* If any threads are active, report EBUSY
*/
if (rwl->r_active > 0 || rwl->w_active) {
pthread_mutex_unlock(&rwl->mutex);
return EBUSY;
}
/*
* If any threads are waiting, report EBUSY
*/
if (rwl->r_wait > 0 || rwl->w_wait > 0) {
pthread_mutex_unlock(&rwl->mutex);
return EBUSY;
}
rwl->valid = 0;
if ((status = pthread_mutex_unlock(&rwl->mutex)) != 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));
}
bool RwlIsInit(brwlock_t *rwl)
{
return (rwl->valid == RWLOCK_VALID);
}
/*
* Handle cleanup when the read lock condition variable
* wait is released.
*/
static void RwlReadRelease(void *arg)
{
brwlock_t *rwl = (brwlock_t *)arg;
rwl->r_wait--;
pthread_mutex_unlock(&rwl->mutex);
}
/*
* Handle cleanup when the write lock condition variable wait
* is released.
*/
static void RwlWriteRelease(void *arg)
{
brwlock_t *rwl = (brwlock_t *)arg;
rwl->w_wait--;
pthread_mutex_unlock(&rwl->mutex);
}
/*
* Lock for read access, wait until locked (or error).
*/
int RwlReadlock(brwlock_t *rwl)
{
int status;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
if (rwl->w_active) {
rwl->r_wait++; /* indicate that we are waiting */
pthread_cleanup_push(RwlReadRelease, (void *)rwl);
while (rwl->w_active) {
status = pthread_cond_wait(&rwl->read, &rwl->mutex);
if (status != 0) {
break; /* error, bail out */
}
}
pthread_cleanup_pop(0);
rwl->r_wait--; /* we are no longer waiting */
}
if (status == 0) {
rwl->r_active++; /* we are running */
}
pthread_mutex_unlock(&rwl->mutex);
return status;
}
/*
* Attempt to lock for read access, don't wait
*/
int RwlReadtrylock(brwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
if (rwl->w_active) {
status = EBUSY;
} else {
rwl->r_active++; /* we are running */
}
status2 = pthread_mutex_unlock(&rwl->mutex);
return (status == 0 ? status2 : status);
}
/*
* Unlock read lock
*/
int RwlReadunlock(brwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
rwl->r_active--;
if (rwl->r_active == 0 && rwl->w_wait > 0) { /* if writers waiting */
status = pthread_cond_broadcast(&rwl->write);
}
status2 = pthread_mutex_unlock(&rwl->mutex);
return (status == 0 ? status2 : status);
}
/*
* Lock for write access, wait until locked (or error).
* Multiple nested write locking is permitted.
*/
int RwlWritelock_p(brwlock_t *rwl, const char *file, int line)
{
int status;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
if (rwl->w_active && pthread_equal(rwl->writer_id, pthread_self())) {
rwl->w_active++;
pthread_mutex_unlock(&rwl->mutex);
return 0;
}
LmgrPreLock(rwl, rwl->priority, file, line);
if (rwl->w_active || rwl->r_active > 0) {
rwl->w_wait++; /* indicate that we are waiting */
pthread_cleanup_push(RwlWriteRelease, (void *)rwl);
while (rwl->w_active || rwl->r_active > 0) {
if ((status = pthread_cond_wait(&rwl->write, &rwl->mutex)) != 0) {
LmgrDoUnlock(rwl);
break; /* error, bail out */
}
}
pthread_cleanup_pop(0);
rwl->w_wait--; /* we are no longer waiting */
}
if (status == 0) {
rwl->w_active++; /* we are running */
rwl->writer_id = pthread_self(); /* save writer thread's id */
LmgrPostLock();
}
pthread_mutex_unlock(&rwl->mutex);
return status;
}
/*
* Attempt to lock for write access, don't wait
*/
int RwlWritetrylock(brwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
if (rwl->w_active && pthread_equal(rwl->writer_id, pthread_self())) {
rwl->w_active++;
pthread_mutex_unlock(&rwl->mutex);
return 0;
}
if (rwl->w_active || rwl->r_active > 0) {
status = EBUSY;
} else {
rwl->w_active = 1; /* we are running */
rwl->writer_id = pthread_self(); /* save writer thread's id */
LmgrDoLock(rwl, rwl->priority, __FILE__, __LINE__);
}
status2 = pthread_mutex_unlock(&rwl->mutex);
return (status == 0 ? status2 : status);
}
/*
* Unlock write lock
* Start any waiting writers in preference to waiting readers
*/
int RwlWriteunlock(brwlock_t *rwl)
{
int status, status2;
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
return status;
}
if (rwl->w_active <= 0) {
pthread_mutex_unlock(&rwl->mutex);
Jmsg0(NULL, M_ABORT, 0, _("RwlWriteunlock called too many times.\n"));
}
rwl->w_active--;
if (!pthread_equal(pthread_self(), rwl->writer_id)) {
pthread_mutex_unlock(&rwl->mutex);
Jmsg0(NULL, M_ABORT, 0, _("RwlWriteunlock by non-owner.\n"));
}
if (rwl->w_active > 0) {
status = 0; /* writers still active */
} else {
LmgrDoUnlock(rwl);
/* No more writers, awaken someone */
if (rwl->r_wait > 0) { /* if readers waiting */
status = pthread_cond_broadcast(&rwl->read);
} else if (rwl->w_wait > 0) {
status = pthread_cond_broadcast(&rwl->write);
}
}
status2 = pthread_mutex_unlock(&rwl->mutex);
return (status == 0 ? status2 : status);
}