1 /* Acquire a rwlock for reading. Generic version.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <pt-internal.h>
25 /* Acquire the rwlock *RWLOCK for reading blocking until *ABSTIME if
26 it is already held. As a GNU extension, if TIMESPEC is NULL then
29 __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock
*rwlock
,
31 const struct timespec
*abstime
)
35 struct __pthread
*self
;
37 __pthread_spin_wait (&rwlock
->__lock
);
38 if (__pthread_spin_trylock (&rwlock
->__held
) == 0)
39 /* Successfully acquired the lock. */
41 assert (rwlock
->__readerqueue
== 0);
42 assert (rwlock
->__writerqueue
== 0);
43 assert (rwlock
->__readers
== 0);
45 rwlock
->__readers
= 1;
46 __pthread_spin_unlock (&rwlock
->__lock
);
50 /* Lock is held, but is held by a reader? */
51 if (rwlock
->__readers
> 0)
52 /* Just add ourself to number of readers. */
54 assert (rwlock
->__readerqueue
== 0);
56 __pthread_spin_unlock (&rwlock
->__lock
);
60 /* The lock is busy. */
62 /* Better be blocked by a writer. */
63 assert (rwlock
->__readers
== 0);
65 if (abstime
!= NULL
&& ! valid_nanoseconds (abstime
->tv_nsec
))
67 __pthread_spin_unlock (&rwlock
->__lock
);
71 self
= _pthread_self ();
73 /* Add ourself to the queue. */
74 __pthread_enqueue (&rwlock
->__readerqueue
, self
);
75 __pthread_spin_unlock (&rwlock
->__lock
);
77 /* Block the thread. */
79 err
= __pthread_timedblock (self
, abstime
, clockid
);
83 __pthread_block (self
);
86 __pthread_spin_wait (&rwlock
->__lock
);
87 if (self
->prevp
== NULL
)
88 /* Another thread removed us from the queue, which means a wakeup message
89 has been sent. It was either consumed while we were blocking, or
90 queued after we timed out and before we acquired the rwlock lock, in
91 which case the message queue must be drained. */
95 /* We're still in the queue. No one attempted to wake us up, i.e. we
97 __pthread_dequeue (self
);
100 __pthread_spin_unlock (&rwlock
->__lock
);
103 __pthread_block (self
);
107 assert (err
== ETIMEDOUT
);
111 /* The reader count has already been increment by whoever woke us
114 assert (rwlock
->__readers
> 0);
120 __pthread_rwlock_timedrdlock (struct __pthread_rwlock
*rwlock
,
121 const struct timespec
*abstime
)
123 return __pthread_rwlock_timedrdlock_internal (rwlock
, CLOCK_REALTIME
, abstime
);
125 weak_alias (__pthread_rwlock_timedrdlock
, pthread_rwlock_timedrdlock
)
128 __pthread_rwlock_clockrdlock (struct __pthread_rwlock
*rwlock
,
130 const struct timespec
*abstime
)
132 return __pthread_rwlock_timedrdlock_internal (rwlock
, clockid
, abstime
);
134 weak_alias (__pthread_rwlock_clockrdlock
, pthread_rwlock_clockrdlock
)