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 $
29 #include "namespace.h"
30 #include <machine/tls.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 umtx_t _rwlock_static_lock
;
44 rwlock_init(pthread_rwlock_t
*rwlock
, const pthread_rwlockattr_t
*attr __unused
)
46 pthread_rwlock_t prwlock
;
49 /* allocate rwlock object */
50 prwlock
= (pthread_rwlock_t
)malloc(sizeof(struct pthread_rwlock
));
55 /* initialize the lock */
56 if ((ret
= _pthread_mutex_init(&prwlock
->lock
, NULL
)) != 0)
59 /* initialize the read condition signal */
60 ret
= _pthread_cond_init(&prwlock
->read_signal
, NULL
);
63 _pthread_mutex_destroy(&prwlock
->lock
);
66 /* initialize the write condition signal */
67 ret
= _pthread_cond_init(&prwlock
->write_signal
, NULL
);
70 _pthread_cond_destroy(&prwlock
->read_signal
);
71 _pthread_mutex_destroy(&prwlock
->lock
);
76 prwlock
->blocked_writers
= 0;
86 _pthread_rwlock_destroy (pthread_rwlock_t
*rwlock
)
92 else if (*rwlock
== NULL
)
95 pthread_rwlock_t prwlock
;
99 _pthread_mutex_destroy(&prwlock
->lock
);
100 _pthread_cond_destroy(&prwlock
->read_signal
);
101 _pthread_cond_destroy(&prwlock
->write_signal
);
112 init_static(struct pthread
*thread
, pthread_rwlock_t
*rwlock
)
116 THR_LOCK_ACQUIRE(thread
, &_rwlock_static_lock
);
119 ret
= rwlock_init(rwlock
, NULL
);
123 THR_LOCK_RELEASE(thread
, &_rwlock_static_lock
);
129 _pthread_rwlock_init(pthread_rwlock_t
*rwlock
, const pthread_rwlockattr_t
*attr
)
132 return (rwlock_init(rwlock
, attr
));
136 rwlock_rdlock_common(pthread_rwlock_t
*rwlock
, const struct timespec
*abstime
)
138 struct pthread
*curthread
= tls_get_curthread();
139 pthread_rwlock_t prwlock
;
147 /* check for static initialization */
148 if (prwlock
== NULL
) {
149 if ((ret
= init_static(curthread
, rwlock
)) != 0)
155 /* grab the monitor lock */
156 if ((ret
= _pthread_mutex_lock(&prwlock
->lock
)) != 0)
159 /* check lock count */
160 if (prwlock
->state
== MAX_READ_LOCKS
) {
161 _pthread_mutex_unlock(&prwlock
->lock
);
165 curthread
= tls_get_curthread();
166 if ((curthread
->rdlock_count
> 0) && (prwlock
->state
> 0)) {
168 * To avoid having to track all the rdlocks held by
169 * a thread or all of the threads that hold a rdlock,
170 * we keep a simple count of all the rdlocks held by
171 * a thread. If a thread holds any rdlocks it is
172 * possible that it is attempting to take a recursive
173 * rdlock. If there are blocked writers and precedence
174 * is given to them, then that would result in the thread
175 * deadlocking. So allowing a thread to take the rdlock
176 * when it already has one or more rdlocks avoids the
177 * deadlock. I hope the reader can follow that logic ;-)
179 ; /* nothing needed */
181 /* give writers priority over readers */
182 while (prwlock
->blocked_writers
|| prwlock
->state
< 0) {
184 ret
= _pthread_cond_timedwait
185 (&prwlock
->read_signal
,
186 &prwlock
->lock
, abstime
);
188 ret
= _pthread_cond_wait(&prwlock
->read_signal
,
191 /* can't do a whole lot if this fails */
192 _pthread_mutex_unlock(&prwlock
->lock
);
198 curthread
->rdlock_count
++;
199 prwlock
->state
++; /* indicate we are locked for reading */
202 * Something is really wrong if this call fails. Returning
203 * error won't do because we've already obtained the read
204 * lock. Decrementing 'state' is no good because we probably
205 * don't have the monitor lock.
207 _pthread_mutex_unlock(&prwlock
->lock
);
213 _pthread_rwlock_rdlock (pthread_rwlock_t
*rwlock
)
215 return (rwlock_rdlock_common(rwlock
, NULL
));
219 _pthread_rwlock_timedrdlock (pthread_rwlock_t
*rwlock
,
220 const struct timespec
*abstime
)
222 return (rwlock_rdlock_common(rwlock
, abstime
));
226 _pthread_rwlock_tryrdlock (pthread_rwlock_t
*rwlock
)
228 struct pthread
*curthread
= tls_get_curthread();
229 pthread_rwlock_t prwlock
;
237 /* check for static initialization */
238 if (prwlock
== NULL
) {
239 if ((ret
= init_static(curthread
, rwlock
)) != 0)
245 /* grab the monitor lock */
246 if ((ret
= _pthread_mutex_lock(&prwlock
->lock
)) != 0)
249 curthread
= tls_get_curthread();
250 if (prwlock
->state
== MAX_READ_LOCKS
)
252 else if ((curthread
->rdlock_count
> 0) && (prwlock
->state
> 0)) {
253 /* see comment for pthread_rwlock_rdlock() */
254 curthread
->rdlock_count
++;
257 /* give writers priority over readers */
258 else if (prwlock
->blocked_writers
|| prwlock
->state
< 0)
261 curthread
->rdlock_count
++;
262 prwlock
->state
++; /* indicate we are locked for reading */
265 /* see the comment on this in pthread_rwlock_rdlock */
266 _pthread_mutex_unlock(&prwlock
->lock
);
272 _pthread_rwlock_trywrlock (pthread_rwlock_t
*rwlock
)
274 struct pthread
*curthread
= tls_get_curthread();
275 pthread_rwlock_t prwlock
;
283 /* check for static initialization */
284 if (prwlock
== NULL
) {
285 if ((ret
= init_static(curthread
, rwlock
)) != 0)
291 /* grab the monitor lock */
292 if ((ret
= _pthread_mutex_lock(&prwlock
->lock
)) != 0)
295 if (prwlock
->state
!= 0)
298 /* indicate we are locked for writing */
301 /* see the comment on this in pthread_rwlock_rdlock */
302 _pthread_mutex_unlock(&prwlock
->lock
);
308 _pthread_rwlock_unlock (pthread_rwlock_t
*rwlock
)
310 struct pthread
*curthread
;
311 pthread_rwlock_t prwlock
;
322 /* grab the monitor lock */
323 if ((ret
= _pthread_mutex_lock(&prwlock
->lock
)) != 0)
326 curthread
= tls_get_curthread();
327 if (prwlock
->state
> 0) {
328 curthread
->rdlock_count
--;
330 if (prwlock
->state
== 0 && prwlock
->blocked_writers
)
331 ret
= _pthread_cond_signal(&prwlock
->write_signal
);
332 } else if (prwlock
->state
< 0) {
335 if (prwlock
->blocked_writers
)
336 ret
= _pthread_cond_signal(&prwlock
->write_signal
);
338 ret
= _pthread_cond_broadcast(&prwlock
->read_signal
);
342 /* see the comment on this in pthread_rwlock_rdlock */
343 _pthread_mutex_unlock(&prwlock
->lock
);
349 rwlock_wrlock_common (pthread_rwlock_t
*rwlock
, const struct timespec
*abstime
)
351 struct pthread
*curthread
= tls_get_curthread();
352 pthread_rwlock_t prwlock
;
360 /* check for static initialization */
361 if (prwlock
== NULL
) {
362 if ((ret
= init_static(curthread
, rwlock
)) != 0)
368 /* grab the monitor lock */
369 if ((ret
= _pthread_mutex_lock(&prwlock
->lock
)) != 0)
372 while (prwlock
->state
!= 0) {
373 prwlock
->blocked_writers
++;
376 ret
= _pthread_cond_timedwait(&prwlock
->write_signal
,
377 &prwlock
->lock
, abstime
);
379 ret
= _pthread_cond_wait(&prwlock
->write_signal
,
382 prwlock
->blocked_writers
--;
383 _pthread_mutex_unlock(&prwlock
->lock
);
387 prwlock
->blocked_writers
--;
390 /* indicate we are locked for writing */
393 /* see the comment on this in pthread_rwlock_rdlock */
394 _pthread_mutex_unlock(&prwlock
->lock
);
400 _pthread_rwlock_wrlock (pthread_rwlock_t
*rwlock
)
402 return (rwlock_wrlock_common (rwlock
, NULL
));
406 _pthread_rwlock_timedwrlock (pthread_rwlock_t
*rwlock
,
407 const struct timespec
*abstime
)
409 return (rwlock_wrlock_common (rwlock
, abstime
));
412 __strong_reference(_pthread_rwlock_destroy
, pthread_rwlock_destroy
);
413 __strong_reference(_pthread_rwlock_init
, pthread_rwlock_init
);
414 __strong_reference(_pthread_rwlock_rdlock
, pthread_rwlock_rdlock
);
415 __strong_reference(_pthread_rwlock_timedrdlock
, pthread_rwlock_timedrdlock
);
416 __strong_reference(_pthread_rwlock_tryrdlock
, pthread_rwlock_tryrdlock
);
417 __strong_reference(_pthread_rwlock_trywrlock
, pthread_rwlock_trywrlock
);
418 __strong_reference(_pthread_rwlock_unlock
, pthread_rwlock_unlock
);
419 __strong_reference(_pthread_rwlock_wrlock
, pthread_rwlock_wrlock
);
420 __strong_reference(_pthread_rwlock_timedwrlock
, pthread_rwlock_timedwrlock
);