1 /* Acquire a rwlock for writing. 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 RWLOCK for writing blocking until *ABSTIME if we cannot get
26 it. As a special GNU extension, if ABSTIME is NULL then the wait
27 shall not time out. */
29 __pthread_rwlock_timedwrlock_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 __pthread_spin_unlock (&rwlock
->__lock
);
49 /* The lock is busy. */
51 if (abstime
!= NULL
&& ! valid_nanoseconds (abstime
->tv_nsec
))
53 __pthread_spin_unlock (&rwlock
->__lock
);
57 self
= _pthread_self ();
59 /* Add ourselves to the queue. */
60 __pthread_enqueue (&rwlock
->__writerqueue
, self
);
61 __pthread_spin_unlock (&rwlock
->__lock
);
63 /* Block the thread. */
65 err
= __pthread_timedblock (self
, abstime
, clockid
);
69 __pthread_block (self
);
72 __pthread_spin_wait (&rwlock
->__lock
);
73 if (self
->prevp
== NULL
)
74 /* Another thread removed us from the queue, which means a wakeup message
75 has been sent. It was either consumed while we were blocking, or
76 queued after we timed out and before we acquired the rwlock lock, in
77 which case the message queue must be drained. */
81 /* We're still in the queue. Noone attempted to wake us up, i.e. we
83 __pthread_dequeue (self
);
86 __pthread_spin_unlock (&rwlock
->__lock
);
89 __pthread_block (self
);
93 assert (err
== ETIMEDOUT
);
97 assert (rwlock
->__readers
== 0);
103 __pthread_rwlock_timedwrlock (struct __pthread_rwlock
*rwlock
,
104 const struct timespec
*abstime
)
106 return __pthread_rwlock_timedwrlock_internal (rwlock
, CLOCK_REALTIME
, abstime
);
108 weak_alias (__pthread_rwlock_timedwrlock
, pthread_rwlock_timedwrlock
)
111 __pthread_rwlock_clockwrlock (struct __pthread_rwlock
*rwlock
,
113 const struct timespec
*abstime
)
115 return __pthread_rwlock_timedwrlock_internal (rwlock
, clockid
, abstime
);
117 weak_alias (__pthread_rwlock_clockwrlock
, pthread_rwlock_clockwrlock
)