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.1 2005/02/01 12:38:27 davidxu Exp $
35 #include "thr_private.h"
37 /* maximum number of times a read lock may be obtained */
38 #define MAX_READ_LOCKS (INT_MAX - 1)
40 __weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy);
41 __weak_reference(_pthread_rwlock_init, pthread_rwlock_init);
42 __weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock);
43 __weak_reference(_pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
44 __weak_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
45 __weak_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
46 __weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock);
47 __weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock);
48 __weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
55 rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
57 pthread_rwlock_t prwlock;
60 /* allocate rwlock object */
61 prwlock = (pthread_rwlock_t)malloc(sizeof(struct pthread_rwlock));
66 /* initialize the lock */
67 if ((ret = _pthread_mutex_init(&prwlock->lock, NULL)) != 0)
70 /* initialize the read condition signal */
71 ret = _pthread_cond_init(&prwlock->read_signal, NULL);
74 _pthread_mutex_destroy(&prwlock->lock);
77 /* initialize the write condition signal */
78 ret = _pthread_cond_init(&prwlock->write_signal, NULL);
81 _pthread_cond_destroy(&prwlock->read_signal);
82 _pthread_mutex_destroy(&prwlock->lock);
87 prwlock->blocked_writers = 0;
97 _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
104 pthread_rwlock_t prwlock;
108 _pthread_mutex_destroy(&prwlock->lock);
109 _pthread_cond_destroy(&prwlock->read_signal);
110 _pthread_cond_destroy(&prwlock->write_signal);
121 init_static(struct pthread *thread, pthread_rwlock_t *rwlock)
125 THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock);
128 ret = rwlock_init(rwlock, NULL);
132 THR_LOCK_RELEASE(thread, &_rwlock_static_lock);
138 _pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
141 return (rwlock_init(rwlock, attr));
145 rwlock_rdlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
147 struct pthread *curthread = _get_curthread();
148 pthread_rwlock_t prwlock;
156 /* check for static initialization */
157 if (prwlock == NULL) {
158 if ((ret = init_static(curthread, rwlock)) != 0)
164 /* grab the monitor lock */
165 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0)
168 /* check lock count */
169 if (prwlock->state == MAX_READ_LOCKS) {
170 _thr_mutex_unlock(&prwlock->lock);
174 curthread = _get_curthread();
175 if ((curthread->rdlock_count > 0) && (prwlock->state > 0)) {
177 * To avoid having to track all the rdlocks held by
178 * a thread or all of the threads that hold a rdlock,
179 * we keep a simple count of all the rdlocks held by
180 * a thread. If a thread holds any rdlocks it is
181 * possible that it is attempting to take a recursive
182 * rdlock. If there are blocked writers and precedence
183 * is given to them, then that would result in the thread
184 * deadlocking. So allowing a thread to take the rdlock
185 * when it already has one or more rdlocks avoids the
186 * deadlock. I hope the reader can follow that logic ;-)
188 ; /* nothing needed */
190 /* give writers priority over readers */
191 while (prwlock->blocked_writers || prwlock->state < 0) {
193 ret = _pthread_cond_timedwait
194 (&prwlock->read_signal,
195 &prwlock->lock, abstime);
197 ret = _thr_cond_wait(&prwlock->read_signal,
200 /* can't do a whole lot if this fails */
201 _thr_mutex_unlock(&prwlock->lock);
207 curthread->rdlock_count++;
208 prwlock->state++; /* indicate we are locked for reading */
211 * Something is really wrong if this call fails. Returning
212 * error won't do because we've already obtained the read
213 * lock. Decrementing 'state' is no good because we probably
214 * don't have the monitor lock.
216 _thr_mutex_unlock(&prwlock->lock);
222 _pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
224 return (rwlock_rdlock_common(rwlock, NULL));
227 __strong_reference(_pthread_rwlock_rdlock, _thr_rwlock_rdlock);
230 _pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
231 const struct timespec *abstime)
233 return (rwlock_rdlock_common(rwlock, abstime));
237 _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
239 struct pthread *curthread = _get_curthread();
240 pthread_rwlock_t prwlock;
248 /* check for static initialization */
249 if (prwlock == NULL) {
250 if ((ret = init_static(curthread, rwlock)) != 0)
256 /* grab the monitor lock */
257 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
260 curthread = _get_curthread();
261 if (prwlock->state == MAX_READ_LOCKS)
263 else if ((curthread->rdlock_count > 0) && (prwlock->state > 0)) {
264 /* see comment for pthread_rwlock_rdlock() */
265 curthread->rdlock_count++;
268 /* give writers priority over readers */
269 else if (prwlock->blocked_writers || prwlock->state < 0)
272 curthread->rdlock_count++;
273 prwlock->state++; /* indicate we are locked for reading */
276 /* see the comment on this in pthread_rwlock_rdlock */
277 _pthread_mutex_unlock(&prwlock->lock);
283 _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
285 struct pthread *curthread = _get_curthread();
286 pthread_rwlock_t prwlock;
294 /* check for static initialization */
295 if (prwlock == NULL) {
296 if ((ret = init_static(curthread, rwlock)) != 0)
302 /* grab the monitor lock */
303 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
306 if (prwlock->state != 0)
309 /* indicate we are locked for writing */
312 /* see the comment on this in pthread_rwlock_rdlock */
313 _pthread_mutex_unlock(&prwlock->lock);
319 _pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
321 struct pthread *curthread;
322 pthread_rwlock_t prwlock;
333 /* grab the monitor lock */
334 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0)
337 curthread = _get_curthread();
338 if (prwlock->state > 0) {
339 curthread->rdlock_count--;
341 if (prwlock->state == 0 && prwlock->blocked_writers)
342 ret = _thr_cond_signal(&prwlock->write_signal);
343 } else if (prwlock->state < 0) {
346 if (prwlock->blocked_writers)
347 ret = _thr_cond_signal(&prwlock->write_signal);
349 ret = _thr_cond_broadcast(&prwlock->read_signal);
353 /* see the comment on this in pthread_rwlock_rdlock */
354 _thr_mutex_unlock(&prwlock->lock);
359 __strong_reference(_pthread_rwlock_unlock, _thr_rwlock_unlock);
362 rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
364 struct pthread *curthread = _get_curthread();
365 pthread_rwlock_t prwlock;
373 /* check for static initialization */
374 if (prwlock == NULL) {
375 if ((ret = init_static(curthread, rwlock)) != 0)
381 /* grab the monitor lock */
382 if ((ret = _thr_mutex_lock(&prwlock->lock)) != 0)
385 while (prwlock->state != 0) {
386 prwlock->blocked_writers++;
389 ret = _pthread_cond_timedwait(&prwlock->write_signal,
390 &prwlock->lock, abstime);
392 ret = _thr_cond_wait(&prwlock->write_signal,
395 prwlock->blocked_writers--;
396 _thr_mutex_unlock(&prwlock->lock);
400 prwlock->blocked_writers--;
403 /* indicate we are locked for writing */
406 /* see the comment on this in pthread_rwlock_rdlock */
407 _thr_mutex_unlock(&prwlock->lock);
413 _pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
415 return (rwlock_wrlock_common (rwlock, NULL));
417 __strong_reference(_pthread_rwlock_wrlock, _thr_rwlock_wrlock);
420 _pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
421 const struct timespec *abstime)
423 return (rwlock_wrlock_common (rwlock, abstime));