2 * Copyright (c) 1998 Alex Nash
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libpthread/thread/thr_rwlock.c,v 1.14 2004/01/08 15:37:09 deischen Exp $
27 * $DragonFly: src/lib/libthread_xu/thread/thr_rwlock.c,v 1.7 2006/04/06 13:03:09 davidxu Exp $
30 #include "namespace.h"
31 #include <machine/tls.h>
37 #include "un-namespace.h"
39 #include "thr_private.h"
41 /* maximum number of times a read lock may be obtained */
42 #define MAX_READ_LOCKS (INT_MAX - 1)
44 umtx_t _rwlock_static_lock;
47 rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr __unused)
49 pthread_rwlock_t prwlock;
52 /* allocate rwlock object */
53 prwlock = (pthread_rwlock_t)malloc(sizeof(struct pthread_rwlock));
58 /* initialize the lock */
59 if ((ret = _pthread_mutex_init(&prwlock->lock, NULL)) != 0)
62 /* initialize the read condition signal */
63 ret = _pthread_cond_init(&prwlock->read_signal, NULL);
66 _pthread_mutex_destroy(&prwlock->lock);
69 /* initialize the write condition signal */
70 ret = _pthread_cond_init(&prwlock->write_signal, NULL);
73 _pthread_cond_destroy(&prwlock->read_signal);
74 _pthread_mutex_destroy(&prwlock->lock);
79 prwlock->blocked_writers = 0;
89 _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
96 pthread_rwlock_t prwlock;
100 _pthread_mutex_destroy(&prwlock->lock);
101 _pthread_cond_destroy(&prwlock->read_signal);
102 _pthread_cond_destroy(&prwlock->write_signal);
113 init_static(struct pthread *thread, pthread_rwlock_t *rwlock)
117 THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock);
120 ret = rwlock_init(rwlock, NULL);
124 THR_LOCK_RELEASE(thread, &_rwlock_static_lock);
130 _pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
133 return (rwlock_init(rwlock, attr));
137 rwlock_rdlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
139 struct pthread *curthread = tls_get_curthread();
140 pthread_rwlock_t prwlock;
148 /* check for static initialization */
149 if (prwlock == NULL) {
150 if ((ret = init_static(curthread, rwlock)) != 0)
156 /* grab the monitor lock */
157 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
160 /* check lock count */
161 if (prwlock->state == MAX_READ_LOCKS) {
162 _pthread_mutex_unlock(&prwlock->lock);
166 curthread = tls_get_curthread();
167 if ((curthread->rdlock_count > 0) && (prwlock->state > 0)) {
169 * To avoid having to track all the rdlocks held by
170 * a thread or all of the threads that hold a rdlock,
171 * we keep a simple count of all the rdlocks held by
172 * a thread. If a thread holds any rdlocks it is
173 * possible that it is attempting to take a recursive
174 * rdlock. If there are blocked writers and precedence
175 * is given to them, then that would result in the thread
176 * deadlocking. So allowing a thread to take the rdlock
177 * when it already has one or more rdlocks avoids the
178 * deadlock. I hope the reader can follow that logic ;-)
180 ; /* nothing needed */
182 /* give writers priority over readers */
183 while (prwlock->blocked_writers || prwlock->state < 0) {
185 ret = _pthread_cond_timedwait
186 (&prwlock->read_signal,
187 &prwlock->lock, abstime);
189 ret = _pthread_cond_wait(&prwlock->read_signal,
192 /* can't do a whole lot if this fails */
193 _pthread_mutex_unlock(&prwlock->lock);
199 curthread->rdlock_count++;
200 prwlock->state++; /* indicate we are locked for reading */
203 * Something is really wrong if this call fails. Returning
204 * error won't do because we've already obtained the read
205 * lock. Decrementing 'state' is no good because we probably
206 * don't have the monitor lock.
208 _pthread_mutex_unlock(&prwlock->lock);
214 _pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
216 return (rwlock_rdlock_common(rwlock, NULL));
220 _pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
221 const struct timespec *abstime)
223 return (rwlock_rdlock_common(rwlock, abstime));
227 _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
229 struct pthread *curthread = tls_get_curthread();
230 pthread_rwlock_t prwlock;
238 /* check for static initialization */
239 if (prwlock == NULL) {
240 if ((ret = init_static(curthread, rwlock)) != 0)
246 /* grab the monitor lock */
247 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
250 curthread = tls_get_curthread();
251 if (prwlock->state == MAX_READ_LOCKS)
253 else if ((curthread->rdlock_count > 0) && (prwlock->state > 0)) {
254 /* see comment for pthread_rwlock_rdlock() */
255 curthread->rdlock_count++;
258 /* give writers priority over readers */
259 else if (prwlock->blocked_writers || prwlock->state < 0)
262 curthread->rdlock_count++;
263 prwlock->state++; /* indicate we are locked for reading */
266 /* see the comment on this in pthread_rwlock_rdlock */
267 _pthread_mutex_unlock(&prwlock->lock);
273 _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
275 struct pthread *curthread = tls_get_curthread();
276 pthread_rwlock_t prwlock;
284 /* check for static initialization */
285 if (prwlock == NULL) {
286 if ((ret = init_static(curthread, rwlock)) != 0)
292 /* grab the monitor lock */
293 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
296 if (prwlock->state != 0)
299 /* indicate we are locked for writing */
302 /* see the comment on this in pthread_rwlock_rdlock */
303 _pthread_mutex_unlock(&prwlock->lock);
309 _pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
311 struct pthread *curthread;
312 pthread_rwlock_t prwlock;
323 /* grab the monitor lock */
324 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
327 curthread = tls_get_curthread();
328 if (prwlock->state > 0) {
329 curthread->rdlock_count--;
331 if (prwlock->state == 0 && prwlock->blocked_writers)
332 ret = _pthread_cond_signal(&prwlock->write_signal);
333 } else if (prwlock->state < 0) {
336 if (prwlock->blocked_writers)
337 ret = _pthread_cond_signal(&prwlock->write_signal);
339 ret = _pthread_cond_broadcast(&prwlock->read_signal);
343 /* see the comment on this in pthread_rwlock_rdlock */
344 _pthread_mutex_unlock(&prwlock->lock);
350 rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
352 struct pthread *curthread = tls_get_curthread();
353 pthread_rwlock_t prwlock;
361 /* check for static initialization */
362 if (prwlock == NULL) {
363 if ((ret = init_static(curthread, rwlock)) != 0)
369 /* grab the monitor lock */
370 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
373 while (prwlock->state != 0) {
374 prwlock->blocked_writers++;
377 ret = _pthread_cond_timedwait(&prwlock->write_signal,
378 &prwlock->lock, abstime);
380 ret = _pthread_cond_wait(&prwlock->write_signal,
383 prwlock->blocked_writers--;
384 _pthread_mutex_unlock(&prwlock->lock);
388 prwlock->blocked_writers--;
391 /* indicate we are locked for writing */
394 /* see the comment on this in pthread_rwlock_rdlock */
395 _pthread_mutex_unlock(&prwlock->lock);
401 _pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
403 return (rwlock_wrlock_common (rwlock, NULL));
407 _pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
408 const struct timespec *abstime)
410 return (rwlock_wrlock_common (rwlock, abstime));
413 __strong_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy);
414 __strong_reference(_pthread_rwlock_init, pthread_rwlock_init);
415 __strong_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock);
416 __strong_reference(_pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
417 __strong_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
418 __strong_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
419 __strong_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock);
420 __strong_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock);
421 __strong_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);