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
33 #include "namespace.h"
35 #include "un-namespace.h"
36 #include "thr_private.h"
38 /* maximum number of times a read lock may be obtained */
39 #define MAX_READ_LOCKS (INT_MAX - 1)
41 __weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy);
42 __weak_reference(_pthread_rwlock_init, pthread_rwlock_init);
43 __weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock);
44 __weak_reference(_pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
45 __weak_reference(_pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
46 __weak_reference(_pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
47 __weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock);
48 __weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock);
49 __weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
56 rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr __unused)
58 pthread_rwlock_t prwlock;
61 /* allocate rwlock object */
62 prwlock = (pthread_rwlock_t)malloc(sizeof(struct pthread_rwlock));
67 /* initialize the lock */
68 if ((ret = _pthread_mutex_init(&prwlock->lock, NULL)) != 0)
71 /* initialize the read condition signal */
72 ret = _pthread_cond_init(&prwlock->read_signal, NULL);
75 _pthread_mutex_destroy(&prwlock->lock);
78 /* initialize the write condition signal */
79 ret = _pthread_cond_init(&prwlock->write_signal, NULL);
82 _pthread_cond_destroy(&prwlock->read_signal);
83 _pthread_mutex_destroy(&prwlock->lock);
88 prwlock->blocked_writers = 0;
98 _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
105 pthread_rwlock_t prwlock;
109 _pthread_mutex_destroy(&prwlock->lock);
110 _pthread_cond_destroy(&prwlock->read_signal);
111 _pthread_cond_destroy(&prwlock->write_signal);
122 init_static(struct pthread *thread, pthread_rwlock_t *rwlock)
126 THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock);
129 ret = rwlock_init(rwlock, NULL);
133 THR_LOCK_RELEASE(thread, &_rwlock_static_lock);
139 _pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
142 return (rwlock_init(rwlock, attr));
146 rwlock_rdlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
148 struct pthread *curthread = _get_curthread();
149 pthread_rwlock_t prwlock;
157 /* check for static initialization */
158 if (prwlock == NULL) {
159 if ((ret = init_static(curthread, rwlock)) != 0)
165 /* grab the monitor lock */
166 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
169 /* check lock count */
170 if (prwlock->state == MAX_READ_LOCKS) {
171 _pthread_mutex_unlock(&prwlock->lock);
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 = _pthread_cond_wait(&prwlock->read_signal,
200 /* can't do a whole lot if this fails */
201 _pthread_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 _pthread_mutex_unlock(&prwlock->lock);
222 _pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
224 return (rwlock_rdlock_common(rwlock, NULL));
228 _pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
229 const struct timespec *abstime)
231 return (rwlock_rdlock_common(rwlock, abstime));
235 _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
237 struct pthread *curthread = _get_curthread();
238 pthread_rwlock_t prwlock;
246 /* check for static initialization */
247 if (prwlock == NULL) {
248 if ((ret = init_static(curthread, rwlock)) != 0)
254 /* grab the monitor lock */
255 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
258 if (prwlock->state == MAX_READ_LOCKS)
260 else if ((curthread->rdlock_count > 0) && (prwlock->state > 0)) {
261 /* see comment for pthread_rwlock_rdlock() */
262 curthread->rdlock_count++;
265 /* give writers priority over readers */
266 else if (prwlock->blocked_writers || prwlock->state < 0)
269 curthread->rdlock_count++;
270 prwlock->state++; /* indicate we are locked for reading */
273 /* see the comment on this in pthread_rwlock_rdlock */
274 _pthread_mutex_unlock(&prwlock->lock);
280 _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
282 struct pthread *curthread = _get_curthread();
283 pthread_rwlock_t prwlock;
291 /* check for static initialization */
292 if (prwlock == NULL) {
293 if ((ret = init_static(curthread, rwlock)) != 0)
299 /* grab the monitor lock */
300 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
303 if (prwlock->state != 0)
306 /* indicate we are locked for writing */
309 /* see the comment on this in pthread_rwlock_rdlock */
310 _pthread_mutex_unlock(&prwlock->lock);
316 _pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
318 struct pthread *curthread = _get_curthread();
319 pthread_rwlock_t prwlock;
330 /* grab the monitor lock */
331 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
334 if (prwlock->state > 0) {
335 curthread->rdlock_count--;
337 if (prwlock->state == 0 && prwlock->blocked_writers)
338 ret = _pthread_cond_signal(&prwlock->write_signal);
339 } else if (prwlock->state < 0) {
342 if (prwlock->blocked_writers)
343 ret = _pthread_cond_signal(&prwlock->write_signal);
345 ret = _pthread_cond_broadcast(&prwlock->read_signal);
349 /* see the comment on this in pthread_rwlock_rdlock */
350 _pthread_mutex_unlock(&prwlock->lock);
356 rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
358 struct pthread *curthread = _get_curthread();
359 pthread_rwlock_t prwlock;
367 /* check for static initialization */
368 if (prwlock == NULL) {
369 if ((ret = init_static(curthread, rwlock)) != 0)
375 /* grab the monitor lock */
376 if ((ret = _pthread_mutex_lock(&prwlock->lock)) != 0)
379 while (prwlock->state != 0) {
380 prwlock->blocked_writers++;
383 ret = _pthread_cond_timedwait(&prwlock->write_signal,
384 &prwlock->lock, abstime);
386 ret = _pthread_cond_wait(&prwlock->write_signal,
389 prwlock->blocked_writers--;
390 _pthread_mutex_unlock(&prwlock->lock);
394 prwlock->blocked_writers--;
397 /* indicate we are locked for writing */
400 /* see the comment on this in pthread_rwlock_rdlock */
401 _pthread_mutex_unlock(&prwlock->lock);
407 _pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
409 return (rwlock_wrlock_common (rwlock, NULL));
413 _pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
414 const struct timespec *abstime)
416 return (rwlock_wrlock_common (rwlock, abstime));