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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/*
* Copyright (C) 2008-2012 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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 General Public License
* for more details.
*/
#define _GNU_SOURCE
#include <pthread.h>
#include <library.h>
#include <utils/debug.h>
#include "rwlock.h"
#include "rwlock_condvar.h"
#include "thread.h"
#include "condvar.h"
#include "mutex.h"
#include "lock_profiler.h"
#ifdef __APPLE__
/* while pthread_rwlock_rdlock(3) says that it supports multiple read locks,
* this does not seem to be true. After releasing a recursive rdlock,
* a subsequent wrlock fails... */
# undef HAVE_PTHREAD_RWLOCK_INIT
#endif
typedef struct private_rwlock_t private_rwlock_t;
typedef struct private_rwlock_condvar_t private_rwlock_condvar_t;
/**
* private data of rwlock
*/
struct private_rwlock_t {
/**
* public functions
*/
rwlock_t public;
#ifdef HAVE_PTHREAD_RWLOCK_INIT
/**
* wrapped pthread rwlock
*/
pthread_rwlock_t rwlock;
#else
/**
* mutex to emulate a native rwlock
*/
mutex_t *mutex;
/**
* condvar to handle writers
*/
condvar_t *writers;
/**
* condvar to handle readers
*/
condvar_t *readers;
/**
* number of waiting writers
*/
u_int waiting_writers;
/**
* number of readers holding the lock
*/
u_int reader_count;
/**
* TRUE, if a writer is holding the lock currently
*/
bool writer;
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
/**
* profiling info, if enabled
*/
lock_profile_t profile;
};
/**
* private data of condvar
*/
struct private_rwlock_condvar_t {
/**
* public interface
*/
rwlock_condvar_t public;
/**
* mutex used to implement rwlock condvar
*/
mutex_t *mutex;
/**
* regular condvar to implement rwlock condvar
*/
condvar_t *condvar;
};
#ifdef HAVE_PTHREAD_RWLOCK_INIT
METHOD(rwlock_t, read_lock, void,
private_rwlock_t *this)
{
int err;
profiler_start(&this->profile);
err = pthread_rwlock_rdlock(&this->rwlock);
if (err != 0)
{
DBG1(DBG_LIB, "!!! RWLOCK READ LOCK ERROR: %s !!!", strerror(err));
}
profiler_end(&this->profile);
}
METHOD(rwlock_t, write_lock, void,
private_rwlock_t *this)
{
int err;
profiler_start(&this->profile);
err = pthread_rwlock_wrlock(&this->rwlock);
if (err != 0)
{
DBG1(DBG_LIB, "!!! RWLOCK WRITE LOCK ERROR: %s !!!", strerror(err));
}
profiler_end(&this->profile);
}
METHOD(rwlock_t, try_write_lock, bool,
private_rwlock_t *this)
{
return pthread_rwlock_trywrlock(&this->rwlock) == 0;
}
METHOD(rwlock_t, unlock, void,
private_rwlock_t *this)
{
int err;
err = pthread_rwlock_unlock(&this->rwlock);
if (err != 0)
{
DBG1(DBG_LIB, "!!! RWLOCK UNLOCK ERROR: %s !!!", strerror(err));
}
}
METHOD(rwlock_t, destroy, void,
private_rwlock_t *this)
{
pthread_rwlock_destroy(&this->rwlock);
profiler_cleanup(&this->profile);
free(this);
}
/*
* see header file
*/
rwlock_t *rwlock_create(rwlock_type_t type)
{
switch (type)
{
case RWLOCK_TYPE_DEFAULT:
default:
{
private_rwlock_t *this;
INIT(this,
.public = {
.read_lock = _read_lock,
.write_lock = _write_lock,
.try_write_lock = _try_write_lock,
.unlock = _unlock,
.destroy = _destroy,
}
);
pthread_rwlock_init(&this->rwlock, NULL);
profiler_init(&this->profile);
return &this->public;
}
}
}
#else /* HAVE_PTHREAD_RWLOCK_INIT */
/**
* This implementation of the rwlock_t interface uses mutex_t and condvar_t
* primitives, if the pthread_rwlock_* group of functions is not available or
* don't allow recursive locking for readers.
*
* The following constraints are enforced:
* - Multiple readers can hold the lock at the same time.
* - Only a single writer can hold the lock at any given time.
* - A writer must block until all readers have released the lock before
* obtaining the lock exclusively.
* - Readers that don't hold any read lock and arrive while a writer is
* waiting to acquire the lock will block until after the writer has
* obtained and released the lock.
* These constraints allow for read sharing, prevent write sharing, prevent
* read-write sharing and (largely) prevent starvation of writers by a steady
* stream of incoming readers. Reader starvation is not prevented (this could
* happen if there are more writers than readers).
*
* The implementation supports recursive locking of the read lock but not of
* the write lock. Readers must not acquire the lock exclusively at the same
* time and vice-versa (this is not checked or enforced so behave yourself to
* prevent deadlocks).
*
* Since writers are preferred a thread currently holding the read lock that
* tries to acquire the read lock recursively while a writer is waiting would
* result in a deadlock. In order to avoid having to use a thread-specific
* value for each rwlock_t (or a list of threads) to keep track if a thread
* already acquired the read lock we use a single thread-specific value for all
* rwlock_t objects that keeps track of how many read locks a thread currently
* holds. Preferring readers that already hold ANY read locks prevents this
* deadlock while it still largely avoids writer starvation (for locks that can
* only be acquired while holding another read lock this will obviously not
* work).
*/
/**
* Keep track of how many read locks a thread holds.
*/
static pthread_key_t is_reader;
/**
* Only initialize the read lock counter once.
*/
static pthread_once_t is_reader_initialized = PTHREAD_ONCE_INIT;
/**
* Initialize the read lock counter.
*/
static void initialize_is_reader()
{
pthread_key_create(&is_reader, NULL);
}
METHOD(rwlock_t, read_lock, void,
private_rwlock_t *this)
{
uintptr_t reading;
bool old;
reading = (uintptr_t)pthread_getspecific(is_reader);
profiler_start(&this->profile);
this->mutex->lock(this->mutex);
if (!this->writer && reading > 0)
{
/* directly allow threads that hold ANY read locks, to avoid a deadlock
* caused by preferring writers in the loop below */
}
else
{
old = thread_cancelability(FALSE);
while (this->writer || this->waiting_writers)
{
this->readers->wait(this->readers, this->mutex);
}
thread_cancelability(old);
}
this->reader_count++;
profiler_end(&this->profile);
this->mutex->unlock(this->mutex);
pthread_setspecific(is_reader, (void*)(reading + 1));
}
METHOD(rwlock_t, write_lock, void,
private_rwlock_t *this)
{
bool old;
profiler_start(&this->profile);
this->mutex->lock(this->mutex);
this->waiting_writers++;
old = thread_cancelability(FALSE);
while (this->writer || this->reader_count)
{
this->writers->wait(this->writers, this->mutex);
}
thread_cancelability(old);
this->waiting_writers--;
this->writer = TRUE;
profiler_end(&this->profile);
this->mutex->unlock(this->mutex);
}
METHOD(rwlock_t, try_write_lock, bool,
private_rwlock_t *this)
{
bool res = FALSE;
this->mutex->lock(this->mutex);
if (!this->writer && !this->reader_count)
{
res = this->writer = TRUE;
}
this->mutex->unlock(this->mutex);
return res;
}
METHOD(rwlock_t, unlock, void,
private_rwlock_t *this)
{
this->mutex->lock(this->mutex);
if (this->writer)
{
this->writer = FALSE;
}
else
{
uintptr_t reading;
this->reader_count--;
reading = (uintptr_t)pthread_getspecific(is_reader);
pthread_setspecific(is_reader, (void*)(reading - 1));
}
if (!this->reader_count)
{
if (this->waiting_writers)
{
this->writers->signal(this->writers);
}
else
{
this->readers->broadcast(this->readers);
}
}
this->mutex->unlock(this->mutex);
}
METHOD(rwlock_t, destroy, void,
private_rwlock_t *this)
{
this->mutex->destroy(this->mutex);
this->writers->destroy(this->writers);
this->readers->destroy(this->readers);
profiler_cleanup(&this->profile);
free(this);
}
/*
* see header file
*/
rwlock_t *rwlock_create(rwlock_type_t type)
{
pthread_once(&is_reader_initialized, initialize_is_reader);
switch (type)
{
case RWLOCK_TYPE_DEFAULT:
default:
{
private_rwlock_t *this;
INIT(this,
.public = {
.read_lock = _read_lock,
.write_lock = _write_lock,
.try_write_lock = _try_write_lock,
.unlock = _unlock,
.destroy = _destroy,
},
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
.writers = condvar_create(CONDVAR_TYPE_DEFAULT),
.readers = condvar_create(CONDVAR_TYPE_DEFAULT),
);
profiler_init(&this->profile);
return &this->public;
}
}
}
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
METHOD(rwlock_condvar_t, wait_, void,
private_rwlock_condvar_t *this, rwlock_t *lock)
{
/* at this point we have the write lock locked, to make signals more
* predictable we try to prevent other threads from signaling by acquiring
* the mutex while we still hold the write lock (this assumes they will
* hold the write lock themselves when signaling, which is not mandatory) */
this->mutex->lock(this->mutex);
/* unlock the rwlock and wait for a signal */
lock->unlock(lock);
/* if the calling thread enabled thread cancelability we want to replicate
* the behavior of the regular condvar, i.e. the lock will be held again
* before executing cleanup functions registered by the calling thread */
thread_cleanup_push((thread_cleanup_t)lock->write_lock, lock);
thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
this->condvar->wait(this->condvar, this->mutex);
/* we release the mutex to allow other threads into the condvar (might even
* be required so we can acquire the lock again below) */
thread_cleanup_pop(TRUE);
/* finally we reacquire the lock we held previously */
thread_cleanup_pop(TRUE);
}
METHOD(rwlock_condvar_t, timed_wait_abs, bool,
private_rwlock_condvar_t *this, rwlock_t *lock, timeval_t time)
{
bool timed_out;
/* see wait() above for details on what is going on here */
this->mutex->lock(this->mutex);
lock->unlock(lock);
thread_cleanup_push((thread_cleanup_t)lock->write_lock, lock);
thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
timed_out = this->condvar->timed_wait_abs(this->condvar, this->mutex, time);
thread_cleanup_pop(TRUE);
thread_cleanup_pop(TRUE);
return timed_out;
}
METHOD(rwlock_condvar_t, timed_wait, bool,
private_rwlock_condvar_t *this, rwlock_t *lock, u_int timeout)
{
timeval_t tv;
u_int s, ms;
time_monotonic(&tv);
s = timeout / 1000;
ms = timeout % 1000;
tv.tv_sec += s;
timeval_add_ms(&tv, ms);
return timed_wait_abs(this, lock, tv);
}
METHOD(rwlock_condvar_t, signal_, void,
private_rwlock_condvar_t *this)
{
this->mutex->lock(this->mutex);
this->condvar->signal(this->condvar);
this->mutex->unlock(this->mutex);
}
METHOD(rwlock_condvar_t, broadcast, void,
private_rwlock_condvar_t *this)
{
this->mutex->lock(this->mutex);
this->condvar->broadcast(this->condvar);
this->mutex->unlock(this->mutex);
}
METHOD(rwlock_condvar_t, condvar_destroy, void,
private_rwlock_condvar_t *this)
{
this->condvar->destroy(this->condvar);
this->mutex->destroy(this->mutex);
free(this);
}
/*
* see header file
*/
rwlock_condvar_t *rwlock_condvar_create()
{
private_rwlock_condvar_t *this;
INIT(this,
.public = {
.wait = _wait_,
.timed_wait = _timed_wait,
.timed_wait_abs = _timed_wait_abs,
.signal = _signal_,
.broadcast = _broadcast,
.destroy = _condvar_destroy,
},
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
.condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
);
return &this->public;
}
|