1 /* Read-write lock implementation.
2 Copyright (C) 1998, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Xavier Leroy <Xavier.Leroy@inria.fr>
5 and Ulrich Drepper <drepper@cygnus.com>, 1998.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <bits/libc-lock.h>
26 #include "internals.h"
31 /* Function called by pthread_cancel to remove the thread from
32 waiting inside pthread_rwlock_timedrdlock or pthread_rwlock_timedwrlock. */
34 static int rwlock_rd_extricate_func(void *obj
, pthread_descr th
)
36 pthread_rwlock_t
*rwlock
= obj
;
39 __pthread_lock((struct _pthread_fastlock
*) &rwlock
->__rw_lock
, NULL
);
40 did_remove
= remove_from_queue(&rwlock
->__rw_read_waiting
, th
);
41 __pthread_unlock((struct _pthread_fastlock
*) &rwlock
->__rw_lock
);
46 static int rwlock_wr_extricate_func(void *obj
, pthread_descr th
)
48 pthread_rwlock_t
*rwlock
= obj
;
51 __pthread_lock((struct _pthread_fastlock
*) &rwlock
->__rw_lock
, NULL
);
52 did_remove
= remove_from_queue(&rwlock
->__rw_write_waiting
, th
);
53 __pthread_unlock((struct _pthread_fastlock
*) &rwlock
->__rw_lock
);
59 * Check whether the calling thread already owns one or more read locks on the
60 * specified lock. If so, return a pointer to the read lock info structure
61 * corresponding to that lock.
64 static pthread_readlock_info
*
65 rwlock_is_in_list(pthread_descr self
, pthread_rwlock_t
*rwlock
)
67 pthread_readlock_info
*info
;
69 for (info
= THREAD_GETMEM (self
, p_readlock_list
); info
!= NULL
;
72 if (info
->pr_lock
== rwlock
)
80 * Add a new lock to the thread's list of locks for which it has a read lock.
81 * A new info node must be allocated for this, which is taken from the thread's
82 * free list, or by calling malloc. If malloc fails, a null pointer is
83 * returned. Otherwise the lock info structure is initialized and pushed
84 * onto the thread's list.
87 static pthread_readlock_info
*
88 rwlock_add_to_list(pthread_descr self
, pthread_rwlock_t
*rwlock
)
90 pthread_readlock_info
*info
= THREAD_GETMEM (self
, p_readlock_free
);
93 THREAD_SETMEM (self
, p_readlock_free
, info
->pr_next
);
95 info
= malloc(sizeof *info
);
100 info
->pr_lock_count
= 1;
101 info
->pr_lock
= rwlock
;
102 info
->pr_next
= THREAD_GETMEM (self
, p_readlock_list
);
103 THREAD_SETMEM (self
, p_readlock_list
, info
);
109 * If the thread owns a read lock over the given pthread_rwlock_t,
110 * and this read lock is tracked in the thread's lock list,
111 * this function returns a pointer to the info node in that list.
112 * It also decrements the lock count within that node, and if
113 * it reaches zero, it removes the node from the list.
114 * If nothing is found, it returns a null pointer.
117 static pthread_readlock_info
*
118 rwlock_remove_from_list(pthread_descr self
, pthread_rwlock_t
*rwlock
)
120 pthread_readlock_info
**pinfo
;
122 for (pinfo
= &self
->p_readlock_list
; *pinfo
!= NULL
; pinfo
= &(*pinfo
)->pr_next
)
124 if ((*pinfo
)->pr_lock
== rwlock
)
126 pthread_readlock_info
*info
= *pinfo
;
127 if (--info
->pr_lock_count
== 0)
128 *pinfo
= info
->pr_next
;
137 * This function checks whether the conditions are right to place a read lock.
138 * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
143 rwlock_can_rdlock(pthread_rwlock_t
*rwlock
, int have_lock_already
)
145 /* Can't readlock; it is write locked. */
146 if (rwlock
->__rw_writer
!= NULL
)
149 /* Lock prefers readers; get it. */
150 if (rwlock
->__rw_kind
== PTHREAD_RWLOCK_PREFER_READER_NP
)
153 /* Lock prefers writers, but none are waiting. */
154 if (queue_is_empty(&rwlock
->__rw_write_waiting
))
157 /* Writers are waiting, but this thread already has a read lock */
158 if (have_lock_already
)
161 /* Writers are waiting, and this is a new lock */
166 * This function helps support brain-damaged recursive read locking
167 * semantics required by Unix 98, while maintaining write priority.
168 * This basically determines whether this thread already holds a read lock
169 * already. It returns 1 if so, otherwise it returns 0.
171 * If the thread has any ``untracked read locks'' then it just assumes
172 * that this lock is among them, just to be safe, and returns 1.
174 * Also, if it finds the thread's lock in the list, it sets the pointer
175 * referenced by pexisting to refer to the list entry.
177 * If the thread has no untracked locks, and the lock is not found
178 * in its list, then it is added to the list. If this fails,
179 * then *pout_of_mem is set to 1.
183 rwlock_have_already(pthread_descr
*pself
, pthread_rwlock_t
*rwlock
,
184 pthread_readlock_info
**pexisting
, int *pout_of_mem
)
186 pthread_readlock_info
*existing
= NULL
;
187 int out_of_mem
= 0, have_lock_already
= 0;
188 pthread_descr self
= *pself
;
190 if (rwlock
->__rw_kind
== PTHREAD_RWLOCK_PREFER_WRITER_NP
)
193 *pself
= self
= thread_self();
195 existing
= rwlock_is_in_list(self
, rwlock
);
198 || THREAD_GETMEM (self
, p_untracked_readlock_count
) > 0)
199 have_lock_already
= 1;
202 existing
= rwlock_add_to_list(self
, rwlock
);
203 if (existing
== NULL
)
208 *pout_of_mem
= out_of_mem
;
209 *pexisting
= existing
;
211 return have_lock_already
;
215 __pthread_rwlock_init (pthread_rwlock_t
*rwlock
,
216 const pthread_rwlockattr_t
*attr
)
218 __pthread_init_lock(&rwlock
->__rw_lock
);
219 rwlock
->__rw_readers
= 0;
220 rwlock
->__rw_writer
= NULL
;
221 rwlock
->__rw_read_waiting
= NULL
;
222 rwlock
->__rw_write_waiting
= NULL
;
226 rwlock
->__rw_kind
= PTHREAD_RWLOCK_DEFAULT_NP
;
227 rwlock
->__rw_pshared
= PTHREAD_PROCESS_PRIVATE
;
231 rwlock
->__rw_kind
= attr
->__lockkind
;
232 rwlock
->__rw_pshared
= attr
->__pshared
;
237 strong_alias (__pthread_rwlock_init
, pthread_rwlock_init
)
241 __pthread_rwlock_destroy (pthread_rwlock_t
*rwlock
)
244 _pthread_descr writer
;
246 __pthread_lock (&rwlock
->__rw_lock
, NULL
);
247 readers
= rwlock
->__rw_readers
;
248 writer
= rwlock
->__rw_writer
;
249 __pthread_unlock (&rwlock
->__rw_lock
);
251 if (readers
> 0 || writer
!= NULL
)
256 strong_alias (__pthread_rwlock_destroy
, pthread_rwlock_destroy
)
259 __pthread_rwlock_rdlock (pthread_rwlock_t
*rwlock
)
261 pthread_descr self
= NULL
;
262 pthread_readlock_info
*existing
;
263 int out_of_mem
, have_lock_already
;
265 have_lock_already
= rwlock_have_already(&self
, rwlock
,
266 &existing
, &out_of_mem
);
269 self
= thread_self ();
273 __pthread_lock (&rwlock
->__rw_lock
, self
);
275 if (rwlock_can_rdlock(rwlock
, have_lock_already
))
278 enqueue (&rwlock
->__rw_read_waiting
, self
);
279 __pthread_unlock (&rwlock
->__rw_lock
);
280 suspend (self
); /* This is not a cancellation point */
283 ++rwlock
->__rw_readers
;
284 __pthread_unlock (&rwlock
->__rw_lock
);
286 if (have_lock_already
|| out_of_mem
)
288 if (existing
!= NULL
)
289 ++existing
->pr_lock_count
;
291 ++self
->p_untracked_readlock_count
;
296 strong_alias (__pthread_rwlock_rdlock
, pthread_rwlock_rdlock
)
299 __pthread_rwlock_timedrdlock (pthread_rwlock_t
*rwlock
,
300 const struct timespec
*abstime
)
302 pthread_descr self
= NULL
;
303 pthread_readlock_info
*existing
;
304 int out_of_mem
, have_lock_already
;
305 pthread_extricate_if extr
;
307 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
310 have_lock_already
= rwlock_have_already(&self
, rwlock
,
311 &existing
, &out_of_mem
);
314 self
= thread_self ();
316 /* Set up extrication interface */
317 extr
.pu_object
= rwlock
;
318 extr
.pu_extricate_func
= rwlock_rd_extricate_func
;
320 /* Register extrication interface */
321 __pthread_set_own_extricate_if (self
, &extr
);
325 __pthread_lock (&rwlock
->__rw_lock
, self
);
327 if (rwlock_can_rdlock(rwlock
, have_lock_already
))
330 enqueue (&rwlock
->__rw_read_waiting
, self
);
331 __pthread_unlock (&rwlock
->__rw_lock
);
332 /* This is not a cancellation point */
333 if (timedsuspend (self
, abstime
) == 0)
337 __pthread_lock (&rwlock
->__rw_lock
, self
);
338 was_on_queue
= remove_from_queue (&rwlock
->__rw_read_waiting
, self
);
339 __pthread_unlock (&rwlock
->__rw_lock
);
343 __pthread_set_own_extricate_if (self
, 0);
347 /* Eat the outstanding restart() from the signaller */
352 __pthread_set_own_extricate_if (self
, 0);
354 ++rwlock
->__rw_readers
;
355 __pthread_unlock (&rwlock
->__rw_lock
);
357 if (have_lock_already
|| out_of_mem
)
359 if (existing
!= NULL
)
360 ++existing
->pr_lock_count
;
362 ++self
->p_untracked_readlock_count
;
367 strong_alias (__pthread_rwlock_timedrdlock
, pthread_rwlock_timedrdlock
)
370 __pthread_rwlock_tryrdlock (pthread_rwlock_t
*rwlock
)
372 pthread_descr self
= thread_self();
373 pthread_readlock_info
*existing
;
374 int out_of_mem
, have_lock_already
;
377 have_lock_already
= rwlock_have_already(&self
, rwlock
,
378 &existing
, &out_of_mem
);
380 __pthread_lock (&rwlock
->__rw_lock
, self
);
382 /* 0 is passed to here instead of have_lock_already.
383 This is to meet Single Unix Spec requirements:
384 if writers are waiting, pthread_rwlock_tryrdlock
385 does not acquire a read lock, even if the caller has
386 one or more read locks already. */
388 if (rwlock_can_rdlock(rwlock
, 0))
390 ++rwlock
->__rw_readers
;
394 __pthread_unlock (&rwlock
->__rw_lock
);
398 if (have_lock_already
|| out_of_mem
)
400 if (existing
!= NULL
)
401 ++existing
->pr_lock_count
;
403 ++self
->p_untracked_readlock_count
;
409 strong_alias (__pthread_rwlock_tryrdlock
, pthread_rwlock_tryrdlock
)
413 __pthread_rwlock_wrlock (pthread_rwlock_t
*rwlock
)
415 pthread_descr self
= thread_self ();
419 __pthread_lock (&rwlock
->__rw_lock
, self
);
420 if (rwlock
->__rw_readers
== 0 && rwlock
->__rw_writer
== NULL
)
422 rwlock
->__rw_writer
= self
;
423 __pthread_unlock (&rwlock
->__rw_lock
);
427 /* Suspend ourselves, then try again */
428 enqueue (&rwlock
->__rw_write_waiting
, self
);
429 __pthread_unlock (&rwlock
->__rw_lock
);
430 suspend (self
); /* This is not a cancellation point */
433 strong_alias (__pthread_rwlock_wrlock
, pthread_rwlock_wrlock
)
437 __pthread_rwlock_timedwrlock (pthread_rwlock_t
*rwlock
,
438 const struct timespec
*abstime
)
441 pthread_extricate_if extr
;
443 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
446 self
= thread_self ();
448 /* Set up extrication interface */
449 extr
.pu_object
= rwlock
;
450 extr
.pu_extricate_func
= rwlock_wr_extricate_func
;
452 /* Register extrication interface */
453 __pthread_set_own_extricate_if (self
, &extr
);
457 __pthread_lock (&rwlock
->__rw_lock
, self
);
459 if (rwlock
->__rw_readers
== 0 && rwlock
->__rw_writer
== NULL
)
461 rwlock
->__rw_writer
= self
;
462 __pthread_set_own_extricate_if (self
, 0);
463 __pthread_unlock (&rwlock
->__rw_lock
);
467 /* Suspend ourselves, then try again */
468 enqueue (&rwlock
->__rw_write_waiting
, self
);
469 __pthread_unlock (&rwlock
->__rw_lock
);
470 /* This is not a cancellation point */
471 if (timedsuspend (self
, abstime
) == 0)
475 __pthread_lock (&rwlock
->__rw_lock
, self
);
476 was_on_queue
= remove_from_queue (&rwlock
->__rw_write_waiting
, self
);
477 __pthread_unlock (&rwlock
->__rw_lock
);
481 __pthread_set_own_extricate_if (self
, 0);
485 /* Eat the outstanding restart() from the signaller */
490 strong_alias (__pthread_rwlock_timedwrlock
, pthread_rwlock_timedwrlock
)
494 __pthread_rwlock_trywrlock (pthread_rwlock_t
*rwlock
)
498 __pthread_lock (&rwlock
->__rw_lock
, NULL
);
499 if (rwlock
->__rw_readers
== 0 && rwlock
->__rw_writer
== NULL
)
501 rwlock
->__rw_writer
= thread_self ();
504 __pthread_unlock (&rwlock
->__rw_lock
);
508 strong_alias (__pthread_rwlock_trywrlock
, pthread_rwlock_trywrlock
)
512 __pthread_rwlock_unlock (pthread_rwlock_t
*rwlock
)
514 pthread_descr torestart
;
517 __pthread_lock (&rwlock
->__rw_lock
, NULL
);
518 if (rwlock
->__rw_writer
!= NULL
)
520 /* Unlocking a write lock. */
521 if (rwlock
->__rw_writer
!= thread_self ())
523 __pthread_unlock (&rwlock
->__rw_lock
);
526 rwlock
->__rw_writer
= NULL
;
528 if ((rwlock
->__rw_kind
== PTHREAD_RWLOCK_PREFER_READER_NP
529 && !queue_is_empty(&rwlock
->__rw_read_waiting
))
530 || (th
= dequeue(&rwlock
->__rw_write_waiting
)) == NULL
)
532 /* Restart all waiting readers. */
533 torestart
= rwlock
->__rw_read_waiting
;
534 rwlock
->__rw_read_waiting
= NULL
;
535 __pthread_unlock (&rwlock
->__rw_lock
);
536 while ((th
= dequeue (&torestart
)) != NULL
)
541 /* Restart one waiting writer. */
542 __pthread_unlock (&rwlock
->__rw_lock
);
548 /* Unlocking a read lock. */
549 if (rwlock
->__rw_readers
== 0)
551 __pthread_unlock (&rwlock
->__rw_lock
);
555 --rwlock
->__rw_readers
;
556 if (rwlock
->__rw_readers
== 0)
557 /* Restart one waiting writer, if any. */
558 th
= dequeue (&rwlock
->__rw_write_waiting
);
562 __pthread_unlock (&rwlock
->__rw_lock
);
566 /* Recursive lock fixup */
568 if (rwlock
->__rw_kind
== PTHREAD_RWLOCK_PREFER_WRITER_NP
)
570 pthread_descr self
= thread_self();
571 pthread_readlock_info
*victim
= rwlock_remove_from_list(self
, rwlock
);
575 if (victim
->pr_lock_count
== 0)
577 victim
->pr_next
= THREAD_GETMEM (self
, p_readlock_free
);
578 THREAD_SETMEM (self
, p_readlock_free
, victim
);
583 int val
= THREAD_GETMEM (self
, p_untracked_readlock_count
);
585 THREAD_SETMEM (self
, p_untracked_readlock_count
, val
- 1);
592 strong_alias (__pthread_rwlock_unlock
, pthread_rwlock_unlock
)
597 pthread_rwlockattr_init (pthread_rwlockattr_t
*attr
)
599 attr
->__lockkind
= 0;
600 attr
->__pshared
= PTHREAD_PROCESS_PRIVATE
;
607 __pthread_rwlockattr_destroy (pthread_rwlockattr_t
*attr
)
611 strong_alias (__pthread_rwlockattr_destroy
, pthread_rwlockattr_destroy
)
615 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*attr
, int *pshared
)
617 *pshared
= attr
->__pshared
;
623 pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*attr
, int pshared
)
625 if (pshared
!= PTHREAD_PROCESS_PRIVATE
&& pshared
!= PTHREAD_PROCESS_SHARED
)
628 /* For now it is not possible to shared a conditional variable. */
629 if (pshared
!= PTHREAD_PROCESS_PRIVATE
)
632 attr
->__pshared
= pshared
;
639 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*attr
, int *pref
)
641 *pref
= attr
->__lockkind
;
647 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t
*attr
, int pref
)
649 if (pref
!= PTHREAD_RWLOCK_PREFER_READER_NP
650 && pref
!= PTHREAD_RWLOCK_PREFER_WRITER_NP
651 && pref
!= PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
652 && pref
!= PTHREAD_RWLOCK_DEFAULT_NP
)
655 attr
->__lockkind
= pref
;