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 (__predict_false((prwlock = (*rwlock)) <= THR_RWLOCK_DESTROYED)) { \
50 if (prwlock == THR_RWLOCK_INITIALIZER) { \
52 ret = init_static(_get_curthread(), rwlock); \
55 } else if (prwlock == THR_RWLOCK_DESTROYED) { \
66 rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr __unused)
68 pthread_rwlock_t prwlock;
70 prwlock = (pthread_rwlock_t)calloc(1, sizeof(struct pthread_rwlock));
78 _pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
80 pthread_rwlock_t prwlock;
84 if (prwlock == THR_RWLOCK_INITIALIZER)
86 else if (prwlock == THR_RWLOCK_DESTROYED)
89 *rwlock = THR_RWLOCK_DESTROYED;
98 init_static(struct pthread *thread, pthread_rwlock_t *rwlock)
102 THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock);
104 if (*rwlock == THR_RWLOCK_INITIALIZER)
105 ret = rwlock_init(rwlock, NULL);
109 THR_LOCK_RELEASE(thread, &_rwlock_static_lock);
115 _pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
118 return (rwlock_init(rwlock, attr));
122 rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
124 struct pthread *curthread = _get_curthread();
125 pthread_rwlock_t prwlock;
129 CHECK_AND_INIT_RWLOCK
131 if (curthread->rdlock_count) {
133 * To avoid having to track all the rdlocks held by
134 * a thread or all of the threads that hold a rdlock,
135 * we keep a simple count of all the rdlocks held by
136 * a thread. If a thread holds any rdlocks it is
137 * possible that it is attempting to take a recursive
138 * rdlock. If there are blocked writers and precedence
139 * is given to them, then that would result in the thread
140 * deadlocking. So allowing a thread to take the rdlock
141 * when it already has one or more rdlocks avoids the
142 * deadlock. I hope the reader can follow that logic ;-)
144 flags = URWLOCK_PREFER_READER;
150 * POSIX said the validity of the abstimeout parameter need
151 * not be checked if the lock can be immediately acquired.
153 ret = _thr_rwlock_tryrdlock(&prwlock->lock, flags);
155 curthread->rdlock_count++;
159 if (__predict_false(abstime &&
160 (abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0)))
164 /* goto kernel and lock it */
165 ret = __thr_rwlock_rdlock(&prwlock->lock, flags, abstime);
169 /* if interrupted, try to lock it in userland again. */
170 if (_thr_rwlock_tryrdlock(&prwlock->lock, flags) == 0) {
176 curthread->rdlock_count++;
181 _pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
183 return (rwlock_rdlock_common(rwlock, NULL));
187 _pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
188 const struct timespec *abstime)
190 return (rwlock_rdlock_common(rwlock, abstime));
194 _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
196 struct pthread *curthread = _get_curthread();
197 pthread_rwlock_t prwlock;
201 CHECK_AND_INIT_RWLOCK
203 if (curthread->rdlock_count) {
205 * To avoid having to track all the rdlocks held by
206 * a thread or all of the threads that hold a rdlock,
207 * we keep a simple count of all the rdlocks held by
208 * a thread. If a thread holds any rdlocks it is
209 * possible that it is attempting to take a recursive
210 * rdlock. If there are blocked writers and precedence
211 * is given to them, then that would result in the thread
212 * deadlocking. So allowing a thread to take the rdlock
213 * when it already has one or more rdlocks avoids the
214 * deadlock. I hope the reader can follow that logic ;-)
216 flags = URWLOCK_PREFER_READER;
221 ret = _thr_rwlock_tryrdlock(&prwlock->lock, flags);
223 curthread->rdlock_count++;
228 _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
230 struct pthread *curthread = _get_curthread();
231 pthread_rwlock_t prwlock;
234 CHECK_AND_INIT_RWLOCK
236 ret = _thr_rwlock_trywrlock(&prwlock->lock);
238 prwlock->owner = curthread;
243 rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime)
245 struct pthread *curthread = _get_curthread();
246 pthread_rwlock_t prwlock;
249 CHECK_AND_INIT_RWLOCK
252 * POSIX said the validity of the abstimeout parameter need
253 * not be checked if the lock can be immediately acquired.
255 ret = _thr_rwlock_trywrlock(&prwlock->lock);
257 prwlock->owner = curthread;
261 if (__predict_false(abstime &&
262 (abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0)))
266 /* goto kernel and lock it */
267 ret = __thr_rwlock_wrlock(&prwlock->lock, abstime);
269 prwlock->owner = curthread;
276 /* if interrupted, try to lock it in userland again. */
277 if (_thr_rwlock_trywrlock(&prwlock->lock) == 0) {
279 prwlock->owner = curthread;
287 _pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
289 return (rwlock_wrlock_common (rwlock, NULL));
293 _pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
294 const struct timespec *abstime)
296 return (rwlock_wrlock_common (rwlock, abstime));
300 _pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
302 struct pthread *curthread = _get_curthread();
303 pthread_rwlock_t prwlock;
309 if (__predict_false(prwlock <= THR_RWLOCK_DESTROYED))
312 state = prwlock->lock.rw_state;
313 if (state & URWLOCK_WRITE_OWNER) {
314 if (__predict_false(prwlock->owner != curthread))
316 prwlock->owner = NULL;
319 ret = _thr_rwlock_unlock(&prwlock->lock);
320 if (ret == 0 && (state & URWLOCK_WRITE_OWNER) == 0)
321 curthread->rdlock_count--;