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/libc_r/uthread/uthread_rwlock.c,v 1.4.2.2 2002/10/22 14:44:03 fjoe Exp $
34 #include "pthread_private.h"
36 /* maximum number of times a read lock may be obtained */
37 #define MAX_READ_LOCKS (INT_MAX - 1)
39 __weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy);
40 __weak_reference(_pthread_rwlock_init, pthread_rwlock_init);
41 __weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock);
42 __weak_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
43 __weak_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
44 __weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock);
45 __weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock);
47 static int init_static (pthread_rwlock_t *rwlock);
49 static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER;
52 init_static (pthread_rwlock_t *rwlock)
56 _SPINLOCK(&static_init_lock);
59 ret = pthread_rwlock_init(rwlock, NULL);
63 _SPINUNLOCK(&static_init_lock);
69 _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
76 pthread_rwlock_t prwlock;
80 pthread_mutex_destroy(&prwlock->lock);
81 pthread_cond_destroy(&prwlock->read_signal);
82 pthread_cond_destroy(&prwlock->write_signal);
94 _pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
96 pthread_rwlock_t prwlock;
99 /* allocate rwlock object */
100 prwlock = (pthread_rwlock_t)malloc(sizeof(struct pthread_rwlock));
105 /* initialize the lock */
106 if ((ret = pthread_mutex_init(&prwlock->lock, NULL)) != 0)
109 /* initialize the read condition signal */
110 ret = pthread_cond_init(&prwlock->read_signal, NULL);
113 pthread_mutex_destroy(&prwlock->lock);
116 /* initialize the write condition signal */
117 ret = pthread_cond_init(&prwlock->write_signal, NULL);
120 pthread_cond_destroy(&prwlock->read_signal);
121 pthread_mutex_destroy(&prwlock->lock);
126 prwlock->blocked_writers = 0;
137 _pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
139 pthread_rwlock_t prwlock;
147 /* check for static initialization */
148 if (prwlock == NULL) {
149 if ((ret = init_static(rwlock)) != 0)
155 /* grab the monitor lock */
156 if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
159 /* give writers priority over readers */
160 while (prwlock->blocked_writers || prwlock->state < 0) {
161 ret = pthread_cond_wait(&prwlock->read_signal, &prwlock->lock);
164 /* can't do a whole lot if this fails */
165 pthread_mutex_unlock(&prwlock->lock);
170 /* check lock count */
171 if (prwlock->state == MAX_READ_LOCKS)
174 ++prwlock->state; /* indicate we are locked for reading */
177 * Something is really wrong if this call fails. Returning
178 * error won't do because we've already obtained the read
179 * lock. Decrementing 'state' is no good because we probably
180 * don't have the monitor lock.
182 pthread_mutex_unlock(&prwlock->lock);
188 _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
190 pthread_rwlock_t prwlock;
198 /* check for static initialization */
199 if (prwlock == NULL) {
200 if ((ret = init_static(rwlock)) != 0)
206 /* grab the monitor lock */
207 if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
210 /* give writers priority over readers */
211 if (prwlock->blocked_writers || prwlock->state < 0)
213 else if (prwlock->state == MAX_READ_LOCKS)
214 ret = EAGAIN; /* too many read locks acquired */
216 ++prwlock->state; /* indicate we are locked for reading */
218 /* see the comment on this in pthread_rwlock_rdlock */
219 pthread_mutex_unlock(&prwlock->lock);
225 _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
227 pthread_rwlock_t prwlock;
235 /* check for static initialization */
236 if (prwlock == NULL) {
237 if ((ret = init_static(rwlock)) != 0)
243 /* grab the monitor lock */
244 if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
247 if (prwlock->state != 0)
250 /* indicate we are locked for writing */
253 /* see the comment on this in pthread_rwlock_rdlock */
254 pthread_mutex_unlock(&prwlock->lock);
260 _pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
262 pthread_rwlock_t prwlock;
273 /* grab the monitor lock */
274 if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
277 if (prwlock->state > 0) {
278 if (--prwlock->state == 0 && prwlock->blocked_writers)
279 ret = pthread_cond_signal(&prwlock->write_signal);
280 } else if (prwlock->state < 0) {
283 if (prwlock->blocked_writers)
284 ret = pthread_cond_signal(&prwlock->write_signal);
286 ret = pthread_cond_broadcast(&prwlock->read_signal);
290 /* see the comment on this in pthread_rwlock_rdlock */
291 pthread_mutex_unlock(&prwlock->lock);
297 _pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
299 pthread_rwlock_t prwlock;
307 /* check for static initialization */
308 if (prwlock == NULL) {
309 if ((ret = init_static(rwlock)) != 0)
315 /* grab the monitor lock */
316 if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
319 while (prwlock->state != 0) {
320 ++prwlock->blocked_writers;
322 ret = pthread_cond_wait(&prwlock->write_signal, &prwlock->lock);
325 --prwlock->blocked_writers;
326 pthread_mutex_unlock(&prwlock->lock);
330 --prwlock->blocked_writers;
333 /* indicate we are locked for writing */
336 /* see the comment on this in pthread_rwlock_rdlock */
337 pthread_mutex_unlock(&prwlock->lock);