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 __weak_reference(_pthread_rwlock_destroy, pthread_rwlock_destroy);
39 __weak_reference(_pthread_rwlock_init, pthread_rwlock_init);
40 __weak_reference(_pthread_rwlock_rdlock, pthread_rwlock_rdlock);
41 __weak_reference(_pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
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);
46 __weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
48 #define CHECK_AND_INIT_RWLOCK \
49 if (*rwlock == THR_PSHARED_PTR) { \
50 prwlock = __thr_pshared_offpage(rwlock, 0); \
51 if (prwlock == NULL) \
53 } else if (__predict_false((prwlock = (*rwlock)) <= \
54 THR_RWLOCK_DESTROYED)) { \
55 if (prwlock == THR_RWLOCK_INITIALIZER) { \
57 ret = init_static(_get_curthread(), rwlock); \
60 } else if (prwlock == THR_RWLOCK_DESTROYED) { \
71 rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
73 pthread_rwlock_t prwlock;
75 if (attr == NULL || *attr == NULL ||
76 (*attr)->pshared == PTHREAD_PROCESS_PRIVATE) {
77 prwlock = calloc(1, sizeof(struct pthread_rwlock));
82 prwlock = __thr_pshared_offpage(rwlock, 1);
85 prwlock->lock.rw_flags |= USYNC_PROCESS_SHARED;
86 *rwlock = THR_PSHARED_PTR;
92 _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
94 pthread_rwlock_t prwlock;
98 if (prwlock == THR_RWLOCK_INITIALIZER)
100 else if (prwlock == THR_RWLOCK_DESTROYED)
102 else if (prwlock == THR_PSHARED_PTR) {
103 *rwlock = THR_RWLOCK_DESTROYED;
104 __thr_pshared_destroy(rwlock);
107 *rwlock = THR_RWLOCK_DESTROYED;
115 init_static(struct pthread *thread, pthread_rwlock_t *rwlock)
119 THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock);
121 if (*rwlock == THR_RWLOCK_INITIALIZER)
122 ret = rwlock_init(rwlock, NULL);
126 THR_LOCK_RELEASE(thread, &_rwlock_static_lock);
132 _pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
136 return (rwlock_init(rwlock, attr));
140 rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
142 struct pthread *curthread = _get_curthread();
143 pthread_rwlock_t prwlock;
147 CHECK_AND_INIT_RWLOCK
149 if (curthread->rdlock_count) {
151 * To avoid having to track all the rdlocks held by
152 * a thread or all of the threads that hold a rdlock,
153 * we keep a simple count of all the rdlocks held by
154 * a thread. If a thread holds any rdlocks it is
155 * possible that it is attempting to take a recursive
156 * rdlock. If there are blocked writers and precedence
157 * is given to them, then that would result in the thread
158 * deadlocking. So allowing a thread to take the rdlock
159 * when it already has one or more rdlocks avoids the
160 * deadlock. I hope the reader can follow that logic ;-)
162 flags = URWLOCK_PREFER_READER;
168 * POSIX said the validity of the abstimeout parameter need
169 * not be checked if the lock can be immediately acquired.
171 ret = _thr_rwlock_tryrdlock(&prwlock->lock, flags);
173 curthread->rdlock_count++;
177 if (__predict_false(abstime &&
178 (abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0)))
182 /* goto kernel and lock it */
183 ret = __thr_rwlock_rdlock(&prwlock->lock, flags, abstime);
187 /* if interrupted, try to lock it in userland again. */
188 if (_thr_rwlock_tryrdlock(&prwlock->lock, flags) == 0) {
194 curthread->rdlock_count++;
199 _pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
201 return (rwlock_rdlock_common(rwlock, NULL));
205 _pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
206 const struct timespec *abstime)
208 return (rwlock_rdlock_common(rwlock, abstime));
212 _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
214 struct pthread *curthread = _get_curthread();
215 pthread_rwlock_t prwlock;
219 CHECK_AND_INIT_RWLOCK
221 if (curthread->rdlock_count) {
223 * To avoid having to track all the rdlocks held by
224 * a thread or all of the threads that hold a rdlock,
225 * we keep a simple count of all the rdlocks held by
226 * a thread. If a thread holds any rdlocks it is
227 * possible that it is attempting to take a recursive
228 * rdlock. If there are blocked writers and precedence
229 * is given to them, then that would result in the thread
230 * deadlocking. So allowing a thread to take the rdlock
231 * when it already has one or more rdlocks avoids the
232 * deadlock. I hope the reader can follow that logic ;-)
234 flags = URWLOCK_PREFER_READER;
239 ret = _thr_rwlock_tryrdlock(&prwlock->lock, flags);
241 curthread->rdlock_count++;
246 _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
248 struct pthread *curthread = _get_curthread();
249 pthread_rwlock_t prwlock;
252 CHECK_AND_INIT_RWLOCK
254 ret = _thr_rwlock_trywrlock(&prwlock->lock);
256 prwlock->owner = TID(curthread);
261 rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
263 struct pthread *curthread = _get_curthread();
264 pthread_rwlock_t prwlock;
267 CHECK_AND_INIT_RWLOCK
270 * POSIX said the validity of the abstimeout parameter need
271 * not be checked if the lock can be immediately acquired.
273 ret = _thr_rwlock_trywrlock(&prwlock->lock);
275 prwlock->owner = TID(curthread);
279 if (__predict_false(abstime &&
280 (abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0)))
284 /* goto kernel and lock it */
285 ret = __thr_rwlock_wrlock(&prwlock->lock, abstime);
287 prwlock->owner = TID(curthread);
294 /* if interrupted, try to lock it in userland again. */
295 if (_thr_rwlock_trywrlock(&prwlock->lock) == 0) {
297 prwlock->owner = TID(curthread);
305 _pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
307 return (rwlock_wrlock_common (rwlock, NULL));
311 _pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
312 const struct timespec *abstime)
314 return (rwlock_wrlock_common (rwlock, abstime));
318 _pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
320 struct pthread *curthread = _get_curthread();
321 pthread_rwlock_t prwlock;
325 if (*rwlock == THR_PSHARED_PTR) {
326 prwlock = __thr_pshared_offpage(rwlock, 0);
333 if (__predict_false(prwlock <= THR_RWLOCK_DESTROYED))
336 state = prwlock->lock.rw_state;
337 if (state & URWLOCK_WRITE_OWNER) {
338 if (__predict_false(prwlock->owner != TID(curthread)))
343 ret = _thr_rwlock_unlock(&prwlock->lock);
344 if (ret == 0 && (state & URWLOCK_WRITE_OWNER) == 0)
345 curthread->rdlock_count--;