1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2010 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #include <errno.h>
30*8d67ca89SAndroid Build Coastguard Worker #include <stdatomic.h>
31*8d67ca89SAndroid Build Coastguard Worker #include <string.h>
32*8d67ca89SAndroid Build Coastguard Worker
33*8d67ca89SAndroid Build Coastguard Worker #include "pthread_internal.h"
34*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_futex.h"
35*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_lock.h"
36*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_time_conversions.h"
37*8d67ca89SAndroid Build Coastguard Worker
38*8d67ca89SAndroid Build Coastguard Worker /* Technical note:
39*8d67ca89SAndroid Build Coastguard Worker *
40*8d67ca89SAndroid Build Coastguard Worker * Possible states of a read/write lock:
41*8d67ca89SAndroid Build Coastguard Worker *
42*8d67ca89SAndroid Build Coastguard Worker * - no readers and no writer (unlocked)
43*8d67ca89SAndroid Build Coastguard Worker * - one or more readers sharing the lock at the same time (read-locked)
44*8d67ca89SAndroid Build Coastguard Worker * - one writer holding the lock (write-lock)
45*8d67ca89SAndroid Build Coastguard Worker *
46*8d67ca89SAndroid Build Coastguard Worker * Additionally:
47*8d67ca89SAndroid Build Coastguard Worker * - trying to get the write-lock while there are any readers blocks
48*8d67ca89SAndroid Build Coastguard Worker * - trying to get the read-lock while there is a writer blocks
49*8d67ca89SAndroid Build Coastguard Worker * - a single thread can acquire the lock multiple times in read mode
50*8d67ca89SAndroid Build Coastguard Worker *
51*8d67ca89SAndroid Build Coastguard Worker * - Posix states that behavior is undefined (may deadlock) if a thread tries
52*8d67ca89SAndroid Build Coastguard Worker * to acquire the lock
53*8d67ca89SAndroid Build Coastguard Worker * - in write mode while already holding the lock (whether in read or write mode)
54*8d67ca89SAndroid Build Coastguard Worker * - in read mode while already holding the lock in write mode.
55*8d67ca89SAndroid Build Coastguard Worker * - This implementation will return EDEADLK in "write after write" and "read after
56*8d67ca89SAndroid Build Coastguard Worker * write" cases and will deadlock in write after read case.
57*8d67ca89SAndroid Build Coastguard Worker *
58*8d67ca89SAndroid Build Coastguard Worker */
59*8d67ca89SAndroid Build Coastguard Worker
60*8d67ca89SAndroid Build Coastguard Worker // A rwlockattr is implemented as a 32-bit integer which has following fields:
61*8d67ca89SAndroid Build Coastguard Worker // bits name description
62*8d67ca89SAndroid Build Coastguard Worker // 1 rwlock_kind have rwlock preference like PTHREAD_RWLOCK_PREFER_READER_NP.
63*8d67ca89SAndroid Build Coastguard Worker // 0 process_shared set to 1 if the rwlock is shared between processes.
64*8d67ca89SAndroid Build Coastguard Worker
65*8d67ca89SAndroid Build Coastguard Worker #define RWLOCKATTR_PSHARED_SHIFT 0
66*8d67ca89SAndroid Build Coastguard Worker #define RWLOCKATTR_KIND_SHIFT 1
67*8d67ca89SAndroid Build Coastguard Worker
68*8d67ca89SAndroid Build Coastguard Worker #define RWLOCKATTR_PSHARED_MASK 1
69*8d67ca89SAndroid Build Coastguard Worker #define RWLOCKATTR_KIND_MASK 2
70*8d67ca89SAndroid Build Coastguard Worker #define RWLOCKATTR_RESERVED_MASK (~3)
71*8d67ca89SAndroid Build Coastguard Worker
__rwlockattr_getpshared(const pthread_rwlockattr_t * attr)72*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __rwlockattr_getpshared(const pthread_rwlockattr_t* attr) {
73*8d67ca89SAndroid Build Coastguard Worker return (*attr & RWLOCKATTR_PSHARED_MASK) >> RWLOCKATTR_PSHARED_SHIFT;
74*8d67ca89SAndroid Build Coastguard Worker }
75*8d67ca89SAndroid Build Coastguard Worker
__rwlockattr_setpshared(pthread_rwlockattr_t * attr,int pshared)76*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline void __rwlockattr_setpshared(pthread_rwlockattr_t* attr,
77*8d67ca89SAndroid Build Coastguard Worker int pshared) {
78*8d67ca89SAndroid Build Coastguard Worker *attr = (*attr & ~RWLOCKATTR_PSHARED_MASK) | (pshared << RWLOCKATTR_PSHARED_SHIFT);
79*8d67ca89SAndroid Build Coastguard Worker }
80*8d67ca89SAndroid Build Coastguard Worker
__rwlockattr_getkind(const pthread_rwlockattr_t * attr)81*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline int __rwlockattr_getkind(const pthread_rwlockattr_t* attr) {
82*8d67ca89SAndroid Build Coastguard Worker return (*attr & RWLOCKATTR_KIND_MASK) >> RWLOCKATTR_KIND_SHIFT;
83*8d67ca89SAndroid Build Coastguard Worker }
84*8d67ca89SAndroid Build Coastguard Worker
__rwlockattr_setkind(pthread_rwlockattr_t * attr,int kind)85*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline void __rwlockattr_setkind(pthread_rwlockattr_t* attr, int kind) {
86*8d67ca89SAndroid Build Coastguard Worker *attr = (*attr & ~RWLOCKATTR_KIND_MASK) | (kind << RWLOCKATTR_KIND_SHIFT);
87*8d67ca89SAndroid Build Coastguard Worker }
88*8d67ca89SAndroid Build Coastguard Worker
89*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlockattr_init(pthread_rwlockattr_t * attr)90*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlockattr_init(pthread_rwlockattr_t* attr) {
91*8d67ca89SAndroid Build Coastguard Worker *attr = 0;
92*8d67ca89SAndroid Build Coastguard Worker return 0;
93*8d67ca89SAndroid Build Coastguard Worker }
94*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlockattr_destroy(pthread_rwlockattr_t * attr)95*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlockattr_destroy(pthread_rwlockattr_t* attr) {
96*8d67ca89SAndroid Build Coastguard Worker *attr = -1;
97*8d67ca89SAndroid Build Coastguard Worker return 0;
98*8d67ca89SAndroid Build Coastguard Worker }
99*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * attr,int * pshared)100*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* attr, int* pshared) {
101*8d67ca89SAndroid Build Coastguard Worker if (__rwlockattr_getpshared(attr)) {
102*8d67ca89SAndroid Build Coastguard Worker *pshared = PTHREAD_PROCESS_SHARED;
103*8d67ca89SAndroid Build Coastguard Worker } else {
104*8d67ca89SAndroid Build Coastguard Worker *pshared = PTHREAD_PROCESS_PRIVATE;
105*8d67ca89SAndroid Build Coastguard Worker }
106*8d67ca89SAndroid Build Coastguard Worker return 0;
107*8d67ca89SAndroid Build Coastguard Worker }
108*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlockattr_setpshared(pthread_rwlockattr_t * attr,int pshared)109*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* attr, int pshared) {
110*8d67ca89SAndroid Build Coastguard Worker switch (pshared) {
111*8d67ca89SAndroid Build Coastguard Worker case PTHREAD_PROCESS_PRIVATE:
112*8d67ca89SAndroid Build Coastguard Worker __rwlockattr_setpshared(attr, 0);
113*8d67ca89SAndroid Build Coastguard Worker return 0;
114*8d67ca89SAndroid Build Coastguard Worker case PTHREAD_PROCESS_SHARED:
115*8d67ca89SAndroid Build Coastguard Worker __rwlockattr_setpshared(attr, 1);
116*8d67ca89SAndroid Build Coastguard Worker return 0;
117*8d67ca89SAndroid Build Coastguard Worker default:
118*8d67ca89SAndroid Build Coastguard Worker return EINVAL;
119*8d67ca89SAndroid Build Coastguard Worker }
120*8d67ca89SAndroid Build Coastguard Worker }
121*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t * attr,int * pref)122*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t* attr, int* pref) {
123*8d67ca89SAndroid Build Coastguard Worker *pref = __rwlockattr_getkind(attr);
124*8d67ca89SAndroid Build Coastguard Worker return 0;
125*8d67ca89SAndroid Build Coastguard Worker }
126*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlockattr_setkind_np(pthread_rwlockattr_t * attr,int pref)127*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t* attr, int pref) {
128*8d67ca89SAndroid Build Coastguard Worker switch (pref) {
129*8d67ca89SAndroid Build Coastguard Worker case PTHREAD_RWLOCK_PREFER_READER_NP: // Fall through.
130*8d67ca89SAndroid Build Coastguard Worker case PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP:
131*8d67ca89SAndroid Build Coastguard Worker __rwlockattr_setkind(attr, pref);
132*8d67ca89SAndroid Build Coastguard Worker return 0;
133*8d67ca89SAndroid Build Coastguard Worker default:
134*8d67ca89SAndroid Build Coastguard Worker return EINVAL;
135*8d67ca89SAndroid Build Coastguard Worker }
136*8d67ca89SAndroid Build Coastguard Worker }
137*8d67ca89SAndroid Build Coastguard Worker
138*8d67ca89SAndroid Build Coastguard Worker // A rwlock state is implemented as a 32-bit integer which has following rules:
139*8d67ca89SAndroid Build Coastguard Worker // bits name description
140*8d67ca89SAndroid Build Coastguard Worker // 31 owned_by_writer_flag set to 1 if the lock is owned by a writer now.
141*8d67ca89SAndroid Build Coastguard Worker // 30-2 reader_count the count of readers holding the lock.
142*8d67ca89SAndroid Build Coastguard Worker // 1 have_pending_writers set to 1 if having pending writers.
143*8d67ca89SAndroid Build Coastguard Worker // 0 have_pending_readers set to 1 if having pending readers.
144*8d67ca89SAndroid Build Coastguard Worker
145*8d67ca89SAndroid Build Coastguard Worker #define STATE_HAVE_PENDING_READERS_SHIFT 0
146*8d67ca89SAndroid Build Coastguard Worker #define STATE_HAVE_PENDING_WRITERS_SHIFT 1
147*8d67ca89SAndroid Build Coastguard Worker #define STATE_READER_COUNT_SHIFT 2
148*8d67ca89SAndroid Build Coastguard Worker #define STATE_OWNED_BY_WRITER_SHIFT 31
149*8d67ca89SAndroid Build Coastguard Worker
150*8d67ca89SAndroid Build Coastguard Worker #define STATE_HAVE_PENDING_READERS_FLAG (1 << STATE_HAVE_PENDING_READERS_SHIFT)
151*8d67ca89SAndroid Build Coastguard Worker #define STATE_HAVE_PENDING_WRITERS_FLAG (1 << STATE_HAVE_PENDING_WRITERS_SHIFT)
152*8d67ca89SAndroid Build Coastguard Worker #define STATE_READER_COUNT_CHANGE_STEP (1 << STATE_READER_COUNT_SHIFT)
153*8d67ca89SAndroid Build Coastguard Worker #define STATE_OWNED_BY_WRITER_FLAG (1 << STATE_OWNED_BY_WRITER_SHIFT)
154*8d67ca89SAndroid Build Coastguard Worker
155*8d67ca89SAndroid Build Coastguard Worker #define STATE_HAVE_PENDING_READERS_OR_WRITERS_FLAG \
156*8d67ca89SAndroid Build Coastguard Worker (STATE_HAVE_PENDING_READERS_FLAG | STATE_HAVE_PENDING_WRITERS_FLAG)
157*8d67ca89SAndroid Build Coastguard Worker
158*8d67ca89SAndroid Build Coastguard Worker struct pthread_rwlock_internal_t {
159*8d67ca89SAndroid Build Coastguard Worker atomic_int state;
160*8d67ca89SAndroid Build Coastguard Worker atomic_int writer_tid;
161*8d67ca89SAndroid Build Coastguard Worker
162*8d67ca89SAndroid Build Coastguard Worker bool pshared;
163*8d67ca89SAndroid Build Coastguard Worker bool writer_nonrecursive_preferred;
164*8d67ca89SAndroid Build Coastguard Worker uint16_t __pad;
165*8d67ca89SAndroid Build Coastguard Worker
166*8d67ca89SAndroid Build Coastguard Worker // When a reader thread plans to suspend on the rwlock, it will add STATE_HAVE_PENDING_READERS_FLAG
167*8d67ca89SAndroid Build Coastguard Worker // in state, increase pending_reader_count, and wait on pending_reader_wakeup_serial. After woken
168*8d67ca89SAndroid Build Coastguard Worker // up, the reader thread decreases pending_reader_count, and the last pending reader thread should
169*8d67ca89SAndroid Build Coastguard Worker // remove STATE_HAVE_PENDING_READERS_FLAG in state. A pending writer thread works in a similar way,
170*8d67ca89SAndroid Build Coastguard Worker // except that it uses flag and members for writer threads.
171*8d67ca89SAndroid Build Coastguard Worker
172*8d67ca89SAndroid Build Coastguard Worker Lock pending_lock; // All pending members below are protected by pending_lock.
173*8d67ca89SAndroid Build Coastguard Worker uint32_t pending_reader_count; // Count of pending reader threads.
174*8d67ca89SAndroid Build Coastguard Worker uint32_t pending_writer_count; // Count of pending writer threads.
175*8d67ca89SAndroid Build Coastguard Worker uint32_t pending_reader_wakeup_serial; // Pending reader threads wait on this address by futex_wait.
176*8d67ca89SAndroid Build Coastguard Worker uint32_t pending_writer_wakeup_serial; // Pending writer threads wait on this address by futex_wait.
177*8d67ca89SAndroid Build Coastguard Worker
178*8d67ca89SAndroid Build Coastguard Worker #if defined(__LP64__)
179*8d67ca89SAndroid Build Coastguard Worker char __reserved[20];
180*8d67ca89SAndroid Build Coastguard Worker #else
181*8d67ca89SAndroid Build Coastguard Worker char __reserved[4];
182*8d67ca89SAndroid Build Coastguard Worker #endif
183*8d67ca89SAndroid Build Coastguard Worker };
184*8d67ca89SAndroid Build Coastguard Worker
__state_owned_by_writer(int state)185*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __state_owned_by_writer(int state) {
186*8d67ca89SAndroid Build Coastguard Worker return state < 0;
187*8d67ca89SAndroid Build Coastguard Worker }
188*8d67ca89SAndroid Build Coastguard Worker
__state_owned_by_readers(int state)189*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __state_owned_by_readers(int state) {
190*8d67ca89SAndroid Build Coastguard Worker // If state >= 0, the owned_by_writer_flag is not set.
191*8d67ca89SAndroid Build Coastguard Worker // And if state >= STATE_READER_COUNT_CHANGE_STEP, the reader_count field is not empty.
192*8d67ca89SAndroid Build Coastguard Worker return state >= STATE_READER_COUNT_CHANGE_STEP;
193*8d67ca89SAndroid Build Coastguard Worker }
194*8d67ca89SAndroid Build Coastguard Worker
__state_owned_by_readers_or_writer(int state)195*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __state_owned_by_readers_or_writer(int state) {
196*8d67ca89SAndroid Build Coastguard Worker return state < 0 || state >= STATE_READER_COUNT_CHANGE_STEP;
197*8d67ca89SAndroid Build Coastguard Worker }
198*8d67ca89SAndroid Build Coastguard Worker
__state_add_writer_flag(int state)199*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline int __state_add_writer_flag(int state) {
200*8d67ca89SAndroid Build Coastguard Worker return state | STATE_OWNED_BY_WRITER_FLAG;
201*8d67ca89SAndroid Build Coastguard Worker }
202*8d67ca89SAndroid Build Coastguard Worker
__state_is_last_reader(int state)203*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __state_is_last_reader(int state) {
204*8d67ca89SAndroid Build Coastguard Worker return (state >> STATE_READER_COUNT_SHIFT) == 1;
205*8d67ca89SAndroid Build Coastguard Worker }
206*8d67ca89SAndroid Build Coastguard Worker
__state_have_pending_writers(int state)207*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __state_have_pending_writers(int state) {
208*8d67ca89SAndroid Build Coastguard Worker return state & STATE_HAVE_PENDING_WRITERS_FLAG;
209*8d67ca89SAndroid Build Coastguard Worker }
210*8d67ca89SAndroid Build Coastguard Worker
__state_have_pending_readers_or_writers(int state)211*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __state_have_pending_readers_or_writers(int state) {
212*8d67ca89SAndroid Build Coastguard Worker return state & STATE_HAVE_PENDING_READERS_OR_WRITERS_FLAG;
213*8d67ca89SAndroid Build Coastguard Worker }
214*8d67ca89SAndroid Build Coastguard Worker
215*8d67ca89SAndroid Build Coastguard Worker static_assert(sizeof(pthread_rwlock_t) == sizeof(pthread_rwlock_internal_t),
216*8d67ca89SAndroid Build Coastguard Worker "pthread_rwlock_t should actually be pthread_rwlock_internal_t in implementation.");
217*8d67ca89SAndroid Build Coastguard Worker
218*8d67ca89SAndroid Build Coastguard Worker // For binary compatibility with old version of pthread_rwlock_t, we can't use more strict
219*8d67ca89SAndroid Build Coastguard Worker // alignment than 4-byte alignment.
220*8d67ca89SAndroid Build Coastguard Worker static_assert(alignof(pthread_rwlock_t) == 4,
221*8d67ca89SAndroid Build Coastguard Worker "pthread_rwlock_t should fulfill the alignment requirement of pthread_rwlock_internal_t.");
222*8d67ca89SAndroid Build Coastguard Worker
__get_internal_rwlock(pthread_rwlock_t * rwlock_interface)223*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline pthread_rwlock_internal_t* __get_internal_rwlock(pthread_rwlock_t* rwlock_interface) {
224*8d67ca89SAndroid Build Coastguard Worker return reinterpret_cast<pthread_rwlock_internal_t*>(rwlock_interface);
225*8d67ca89SAndroid Build Coastguard Worker }
226*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_init(pthread_rwlock_t * rwlock_interface,const pthread_rwlockattr_t * attr)227*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_init(pthread_rwlock_t* rwlock_interface, const pthread_rwlockattr_t* attr) {
228*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
229*8d67ca89SAndroid Build Coastguard Worker
230*8d67ca89SAndroid Build Coastguard Worker memset(rwlock, 0, sizeof(pthread_rwlock_internal_t));
231*8d67ca89SAndroid Build Coastguard Worker
232*8d67ca89SAndroid Build Coastguard Worker if (__predict_false(attr != nullptr)) {
233*8d67ca89SAndroid Build Coastguard Worker rwlock->pshared = __rwlockattr_getpshared(attr);
234*8d67ca89SAndroid Build Coastguard Worker int kind = __rwlockattr_getkind(attr);
235*8d67ca89SAndroid Build Coastguard Worker switch (kind) {
236*8d67ca89SAndroid Build Coastguard Worker case PTHREAD_RWLOCK_PREFER_READER_NP:
237*8d67ca89SAndroid Build Coastguard Worker rwlock->writer_nonrecursive_preferred = false;
238*8d67ca89SAndroid Build Coastguard Worker break;
239*8d67ca89SAndroid Build Coastguard Worker case PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP:
240*8d67ca89SAndroid Build Coastguard Worker rwlock->writer_nonrecursive_preferred = true;
241*8d67ca89SAndroid Build Coastguard Worker break;
242*8d67ca89SAndroid Build Coastguard Worker default:
243*8d67ca89SAndroid Build Coastguard Worker return EINVAL;
244*8d67ca89SAndroid Build Coastguard Worker }
245*8d67ca89SAndroid Build Coastguard Worker if ((*attr & RWLOCKATTR_RESERVED_MASK) != 0) {
246*8d67ca89SAndroid Build Coastguard Worker return EINVAL;
247*8d67ca89SAndroid Build Coastguard Worker }
248*8d67ca89SAndroid Build Coastguard Worker }
249*8d67ca89SAndroid Build Coastguard Worker
250*8d67ca89SAndroid Build Coastguard Worker atomic_store_explicit(&rwlock->state, 0, memory_order_relaxed);
251*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.init(rwlock->pshared);
252*8d67ca89SAndroid Build Coastguard Worker return 0;
253*8d67ca89SAndroid Build Coastguard Worker }
254*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_destroy(pthread_rwlock_t * rwlock_interface)255*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_destroy(pthread_rwlock_t* rwlock_interface) {
256*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
257*8d67ca89SAndroid Build Coastguard Worker
258*8d67ca89SAndroid Build Coastguard Worker if (atomic_load_explicit(&rwlock->state, memory_order_relaxed) != 0) {
259*8d67ca89SAndroid Build Coastguard Worker return EBUSY;
260*8d67ca89SAndroid Build Coastguard Worker }
261*8d67ca89SAndroid Build Coastguard Worker return 0;
262*8d67ca89SAndroid Build Coastguard Worker }
263*8d67ca89SAndroid Build Coastguard Worker
__can_acquire_read_lock(int old_state,bool writer_nonrecursive_preferred)264*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __can_acquire_read_lock(int old_state,
265*8d67ca89SAndroid Build Coastguard Worker bool writer_nonrecursive_preferred) {
266*8d67ca89SAndroid Build Coastguard Worker // If writer is preferred with nonrecursive reader, we prevent further readers from acquiring
267*8d67ca89SAndroid Build Coastguard Worker // the lock when there are writers waiting for the lock.
268*8d67ca89SAndroid Build Coastguard Worker bool cannot_apply = __state_owned_by_writer(old_state) ||
269*8d67ca89SAndroid Build Coastguard Worker (writer_nonrecursive_preferred && __state_have_pending_writers(old_state));
270*8d67ca89SAndroid Build Coastguard Worker return !cannot_apply;
271*8d67ca89SAndroid Build Coastguard Worker }
272*8d67ca89SAndroid Build Coastguard Worker
__pthread_rwlock_tryrdlock(pthread_rwlock_internal_t * rwlock)273*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline int __pthread_rwlock_tryrdlock(pthread_rwlock_internal_t* rwlock) {
274*8d67ca89SAndroid Build Coastguard Worker int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed);
275*8d67ca89SAndroid Build Coastguard Worker
276*8d67ca89SAndroid Build Coastguard Worker while (__predict_true(__can_acquire_read_lock(old_state, rwlock->writer_nonrecursive_preferred))) {
277*8d67ca89SAndroid Build Coastguard Worker
278*8d67ca89SAndroid Build Coastguard Worker int new_state = old_state + STATE_READER_COUNT_CHANGE_STEP;
279*8d67ca89SAndroid Build Coastguard Worker if (__predict_false(!__state_owned_by_readers(new_state))) { // Happens when reader count overflows.
280*8d67ca89SAndroid Build Coastguard Worker return EAGAIN;
281*8d67ca89SAndroid Build Coastguard Worker }
282*8d67ca89SAndroid Build Coastguard Worker if (__predict_true(atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state, new_state,
283*8d67ca89SAndroid Build Coastguard Worker memory_order_acquire, memory_order_relaxed))) {
284*8d67ca89SAndroid Build Coastguard Worker return 0;
285*8d67ca89SAndroid Build Coastguard Worker }
286*8d67ca89SAndroid Build Coastguard Worker }
287*8d67ca89SAndroid Build Coastguard Worker return EBUSY;
288*8d67ca89SAndroid Build Coastguard Worker }
289*8d67ca89SAndroid Build Coastguard Worker
__pthread_rwlock_timedrdlock(pthread_rwlock_internal_t * rwlock,bool use_realtime_clock,const timespec * abs_timeout_or_null)290*8d67ca89SAndroid Build Coastguard Worker static int __pthread_rwlock_timedrdlock(pthread_rwlock_internal_t* rwlock, bool use_realtime_clock,
291*8d67ca89SAndroid Build Coastguard Worker const timespec* abs_timeout_or_null) {
292*8d67ca89SAndroid Build Coastguard Worker if (atomic_load_explicit(&rwlock->writer_tid, memory_order_relaxed) == __get_thread()->tid) {
293*8d67ca89SAndroid Build Coastguard Worker return EDEADLK;
294*8d67ca89SAndroid Build Coastguard Worker }
295*8d67ca89SAndroid Build Coastguard Worker
296*8d67ca89SAndroid Build Coastguard Worker while (true) {
297*8d67ca89SAndroid Build Coastguard Worker int result = __pthread_rwlock_tryrdlock(rwlock);
298*8d67ca89SAndroid Build Coastguard Worker if (result == 0 || result == EAGAIN) {
299*8d67ca89SAndroid Build Coastguard Worker return result;
300*8d67ca89SAndroid Build Coastguard Worker }
301*8d67ca89SAndroid Build Coastguard Worker result = check_timespec(abs_timeout_or_null, true);
302*8d67ca89SAndroid Build Coastguard Worker if (result != 0) {
303*8d67ca89SAndroid Build Coastguard Worker return result;
304*8d67ca89SAndroid Build Coastguard Worker }
305*8d67ca89SAndroid Build Coastguard Worker
306*8d67ca89SAndroid Build Coastguard Worker int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed);
307*8d67ca89SAndroid Build Coastguard Worker if (__can_acquire_read_lock(old_state, rwlock->writer_nonrecursive_preferred)) {
308*8d67ca89SAndroid Build Coastguard Worker continue;
309*8d67ca89SAndroid Build Coastguard Worker }
310*8d67ca89SAndroid Build Coastguard Worker
311*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.lock();
312*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_reader_count++;
313*8d67ca89SAndroid Build Coastguard Worker
314*8d67ca89SAndroid Build Coastguard Worker // We rely on the fact that all atomic exchange operations on the same object (here it is
315*8d67ca89SAndroid Build Coastguard Worker // rwlock->state) always appear to occur in a single total order. If the pending flag is added
316*8d67ca89SAndroid Build Coastguard Worker // before unlocking, the unlocking thread will wakeup the waiter. Otherwise, we will see the
317*8d67ca89SAndroid Build Coastguard Worker // state is unlocked and will not wait anymore.
318*8d67ca89SAndroid Build Coastguard Worker old_state = atomic_fetch_or_explicit(&rwlock->state, STATE_HAVE_PENDING_READERS_FLAG,
319*8d67ca89SAndroid Build Coastguard Worker memory_order_relaxed);
320*8d67ca89SAndroid Build Coastguard Worker
321*8d67ca89SAndroid Build Coastguard Worker int old_serial = rwlock->pending_reader_wakeup_serial;
322*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
323*8d67ca89SAndroid Build Coastguard Worker
324*8d67ca89SAndroid Build Coastguard Worker int futex_result = 0;
325*8d67ca89SAndroid Build Coastguard Worker if (!__can_acquire_read_lock(old_state, rwlock->writer_nonrecursive_preferred)) {
326*8d67ca89SAndroid Build Coastguard Worker futex_result = __futex_wait_ex(&rwlock->pending_reader_wakeup_serial, rwlock->pshared,
327*8d67ca89SAndroid Build Coastguard Worker old_serial, use_realtime_clock, abs_timeout_or_null);
328*8d67ca89SAndroid Build Coastguard Worker }
329*8d67ca89SAndroid Build Coastguard Worker
330*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.lock();
331*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_reader_count--;
332*8d67ca89SAndroid Build Coastguard Worker if (rwlock->pending_reader_count == 0) {
333*8d67ca89SAndroid Build Coastguard Worker atomic_fetch_and_explicit(&rwlock->state, ~STATE_HAVE_PENDING_READERS_FLAG,
334*8d67ca89SAndroid Build Coastguard Worker memory_order_relaxed);
335*8d67ca89SAndroid Build Coastguard Worker }
336*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
337*8d67ca89SAndroid Build Coastguard Worker
338*8d67ca89SAndroid Build Coastguard Worker if (futex_result == -ETIMEDOUT) {
339*8d67ca89SAndroid Build Coastguard Worker return ETIMEDOUT;
340*8d67ca89SAndroid Build Coastguard Worker }
341*8d67ca89SAndroid Build Coastguard Worker }
342*8d67ca89SAndroid Build Coastguard Worker }
343*8d67ca89SAndroid Build Coastguard Worker
__can_acquire_write_lock(int old_state)344*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline bool __can_acquire_write_lock(int old_state) {
345*8d67ca89SAndroid Build Coastguard Worker return !__state_owned_by_readers_or_writer(old_state);
346*8d67ca89SAndroid Build Coastguard Worker }
347*8d67ca89SAndroid Build Coastguard Worker
__pthread_rwlock_trywrlock(pthread_rwlock_internal_t * rwlock)348*8d67ca89SAndroid Build Coastguard Worker static inline __always_inline int __pthread_rwlock_trywrlock(pthread_rwlock_internal_t* rwlock) {
349*8d67ca89SAndroid Build Coastguard Worker int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed);
350*8d67ca89SAndroid Build Coastguard Worker
351*8d67ca89SAndroid Build Coastguard Worker while (__predict_true(__can_acquire_write_lock(old_state))) {
352*8d67ca89SAndroid Build Coastguard Worker if (__predict_true(atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state,
353*8d67ca89SAndroid Build Coastguard Worker __state_add_writer_flag(old_state), memory_order_acquire, memory_order_relaxed))) {
354*8d67ca89SAndroid Build Coastguard Worker
355*8d67ca89SAndroid Build Coastguard Worker atomic_store_explicit(&rwlock->writer_tid, __get_thread()->tid, memory_order_relaxed);
356*8d67ca89SAndroid Build Coastguard Worker return 0;
357*8d67ca89SAndroid Build Coastguard Worker }
358*8d67ca89SAndroid Build Coastguard Worker }
359*8d67ca89SAndroid Build Coastguard Worker return EBUSY;
360*8d67ca89SAndroid Build Coastguard Worker }
361*8d67ca89SAndroid Build Coastguard Worker
__pthread_rwlock_timedwrlock(pthread_rwlock_internal_t * rwlock,bool use_realtime_clock,const timespec * abs_timeout_or_null)362*8d67ca89SAndroid Build Coastguard Worker static int __pthread_rwlock_timedwrlock(pthread_rwlock_internal_t* rwlock, bool use_realtime_clock,
363*8d67ca89SAndroid Build Coastguard Worker const timespec* abs_timeout_or_null) {
364*8d67ca89SAndroid Build Coastguard Worker if (atomic_load_explicit(&rwlock->writer_tid, memory_order_relaxed) == __get_thread()->tid) {
365*8d67ca89SAndroid Build Coastguard Worker return EDEADLK;
366*8d67ca89SAndroid Build Coastguard Worker }
367*8d67ca89SAndroid Build Coastguard Worker while (true) {
368*8d67ca89SAndroid Build Coastguard Worker int result = __pthread_rwlock_trywrlock(rwlock);
369*8d67ca89SAndroid Build Coastguard Worker if (result == 0) {
370*8d67ca89SAndroid Build Coastguard Worker return result;
371*8d67ca89SAndroid Build Coastguard Worker }
372*8d67ca89SAndroid Build Coastguard Worker result = check_timespec(abs_timeout_or_null, true);
373*8d67ca89SAndroid Build Coastguard Worker if (result != 0) {
374*8d67ca89SAndroid Build Coastguard Worker return result;
375*8d67ca89SAndroid Build Coastguard Worker }
376*8d67ca89SAndroid Build Coastguard Worker
377*8d67ca89SAndroid Build Coastguard Worker int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed);
378*8d67ca89SAndroid Build Coastguard Worker if (__can_acquire_write_lock(old_state)) {
379*8d67ca89SAndroid Build Coastguard Worker continue;
380*8d67ca89SAndroid Build Coastguard Worker }
381*8d67ca89SAndroid Build Coastguard Worker
382*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.lock();
383*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_writer_count++;
384*8d67ca89SAndroid Build Coastguard Worker
385*8d67ca89SAndroid Build Coastguard Worker old_state = atomic_fetch_or_explicit(&rwlock->state, STATE_HAVE_PENDING_WRITERS_FLAG,
386*8d67ca89SAndroid Build Coastguard Worker memory_order_relaxed);
387*8d67ca89SAndroid Build Coastguard Worker
388*8d67ca89SAndroid Build Coastguard Worker int old_serial = rwlock->pending_writer_wakeup_serial;
389*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
390*8d67ca89SAndroid Build Coastguard Worker
391*8d67ca89SAndroid Build Coastguard Worker int futex_result = 0;
392*8d67ca89SAndroid Build Coastguard Worker if (!__can_acquire_write_lock(old_state)) {
393*8d67ca89SAndroid Build Coastguard Worker futex_result = __futex_wait_ex(&rwlock->pending_writer_wakeup_serial, rwlock->pshared,
394*8d67ca89SAndroid Build Coastguard Worker old_serial, use_realtime_clock, abs_timeout_or_null);
395*8d67ca89SAndroid Build Coastguard Worker }
396*8d67ca89SAndroid Build Coastguard Worker
397*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.lock();
398*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_writer_count--;
399*8d67ca89SAndroid Build Coastguard Worker if (rwlock->pending_writer_count == 0) {
400*8d67ca89SAndroid Build Coastguard Worker atomic_fetch_and_explicit(&rwlock->state, ~STATE_HAVE_PENDING_WRITERS_FLAG,
401*8d67ca89SAndroid Build Coastguard Worker memory_order_relaxed);
402*8d67ca89SAndroid Build Coastguard Worker }
403*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
404*8d67ca89SAndroid Build Coastguard Worker
405*8d67ca89SAndroid Build Coastguard Worker if (futex_result == -ETIMEDOUT) {
406*8d67ca89SAndroid Build Coastguard Worker return ETIMEDOUT;
407*8d67ca89SAndroid Build Coastguard Worker }
408*8d67ca89SAndroid Build Coastguard Worker }
409*8d67ca89SAndroid Build Coastguard Worker }
410*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_rdlock(pthread_rwlock_t * rwlock_interface)411*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock_interface) {
412*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
413*8d67ca89SAndroid Build Coastguard Worker // Avoid slowing down fast path of rdlock.
414*8d67ca89SAndroid Build Coastguard Worker if (__predict_true(__pthread_rwlock_tryrdlock(rwlock) == 0)) {
415*8d67ca89SAndroid Build Coastguard Worker return 0;
416*8d67ca89SAndroid Build Coastguard Worker }
417*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_timedrdlock(rwlock, false, nullptr);
418*8d67ca89SAndroid Build Coastguard Worker }
419*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_timedrdlock(pthread_rwlock_t * rwlock_interface,const timespec * abs_timeout)420*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_timedrdlock(pthread_rwlock_t* rwlock_interface, const timespec* abs_timeout) {
421*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
422*8d67ca89SAndroid Build Coastguard Worker
423*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_timedrdlock(rwlock, true, abs_timeout);
424*8d67ca89SAndroid Build Coastguard Worker }
425*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t * rwlock_interface,const timespec * abs_timeout)426*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t* rwlock_interface,
427*8d67ca89SAndroid Build Coastguard Worker const timespec* abs_timeout) {
428*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
429*8d67ca89SAndroid Build Coastguard Worker
430*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_timedrdlock(rwlock, false, abs_timeout);
431*8d67ca89SAndroid Build Coastguard Worker }
432*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_clockrdlock(pthread_rwlock_t * rwlock_interface,clockid_t clock,const struct timespec * abs_timeout)433*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_clockrdlock(pthread_rwlock_t* rwlock_interface, clockid_t clock,
434*8d67ca89SAndroid Build Coastguard Worker const struct timespec* abs_timeout) {
435*8d67ca89SAndroid Build Coastguard Worker switch (clock) {
436*8d67ca89SAndroid Build Coastguard Worker case CLOCK_MONOTONIC:
437*8d67ca89SAndroid Build Coastguard Worker return pthread_rwlock_timedrdlock_monotonic_np(rwlock_interface, abs_timeout);
438*8d67ca89SAndroid Build Coastguard Worker case CLOCK_REALTIME:
439*8d67ca89SAndroid Build Coastguard Worker return pthread_rwlock_timedrdlock(rwlock_interface, abs_timeout);
440*8d67ca89SAndroid Build Coastguard Worker default:
441*8d67ca89SAndroid Build Coastguard Worker return EINVAL;
442*8d67ca89SAndroid Build Coastguard Worker }
443*8d67ca89SAndroid Build Coastguard Worker }
444*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_tryrdlock(pthread_rwlock_t * rwlock_interface)445*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock_interface) {
446*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_tryrdlock(__get_internal_rwlock(rwlock_interface));
447*8d67ca89SAndroid Build Coastguard Worker }
448*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_wrlock(pthread_rwlock_t * rwlock_interface)449*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock_interface) {
450*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
451*8d67ca89SAndroid Build Coastguard Worker // Avoid slowing down fast path of wrlock.
452*8d67ca89SAndroid Build Coastguard Worker if (__predict_true(__pthread_rwlock_trywrlock(rwlock) == 0)) {
453*8d67ca89SAndroid Build Coastguard Worker return 0;
454*8d67ca89SAndroid Build Coastguard Worker }
455*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_timedwrlock(rwlock, false, nullptr);
456*8d67ca89SAndroid Build Coastguard Worker }
457*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_timedwrlock(pthread_rwlock_t * rwlock_interface,const timespec * abs_timeout)458*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_timedwrlock(pthread_rwlock_t* rwlock_interface, const timespec* abs_timeout) {
459*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
460*8d67ca89SAndroid Build Coastguard Worker
461*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_timedwrlock(rwlock, true, abs_timeout);
462*8d67ca89SAndroid Build Coastguard Worker }
463*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t * rwlock_interface,const timespec * abs_timeout)464*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t* rwlock_interface,
465*8d67ca89SAndroid Build Coastguard Worker const timespec* abs_timeout) {
466*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
467*8d67ca89SAndroid Build Coastguard Worker
468*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_timedwrlock(rwlock, false, abs_timeout);
469*8d67ca89SAndroid Build Coastguard Worker }
470*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_clockwrlock(pthread_rwlock_t * rwlock_interface,clockid_t clock,const struct timespec * abs_timeout)471*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_clockwrlock(pthread_rwlock_t* rwlock_interface, clockid_t clock,
472*8d67ca89SAndroid Build Coastguard Worker const struct timespec* abs_timeout) {
473*8d67ca89SAndroid Build Coastguard Worker switch (clock) {
474*8d67ca89SAndroid Build Coastguard Worker case CLOCK_MONOTONIC:
475*8d67ca89SAndroid Build Coastguard Worker return pthread_rwlock_timedwrlock_monotonic_np(rwlock_interface, abs_timeout);
476*8d67ca89SAndroid Build Coastguard Worker case CLOCK_REALTIME:
477*8d67ca89SAndroid Build Coastguard Worker return pthread_rwlock_timedwrlock(rwlock_interface, abs_timeout);
478*8d67ca89SAndroid Build Coastguard Worker default:
479*8d67ca89SAndroid Build Coastguard Worker return EINVAL;
480*8d67ca89SAndroid Build Coastguard Worker }
481*8d67ca89SAndroid Build Coastguard Worker }
482*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_trywrlock(pthread_rwlock_t * rwlock_interface)483*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock_interface) {
484*8d67ca89SAndroid Build Coastguard Worker return __pthread_rwlock_trywrlock(__get_internal_rwlock(rwlock_interface));
485*8d67ca89SAndroid Build Coastguard Worker }
486*8d67ca89SAndroid Build Coastguard Worker
pthread_rwlock_unlock(pthread_rwlock_t * rwlock_interface)487*8d67ca89SAndroid Build Coastguard Worker int pthread_rwlock_unlock(pthread_rwlock_t* rwlock_interface) {
488*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface);
489*8d67ca89SAndroid Build Coastguard Worker
490*8d67ca89SAndroid Build Coastguard Worker int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed);
491*8d67ca89SAndroid Build Coastguard Worker if (__state_owned_by_writer(old_state)) {
492*8d67ca89SAndroid Build Coastguard Worker if (atomic_load_explicit(&rwlock->writer_tid, memory_order_relaxed) != __get_thread()->tid) {
493*8d67ca89SAndroid Build Coastguard Worker return EPERM;
494*8d67ca89SAndroid Build Coastguard Worker }
495*8d67ca89SAndroid Build Coastguard Worker atomic_store_explicit(&rwlock->writer_tid, 0, memory_order_relaxed);
496*8d67ca89SAndroid Build Coastguard Worker old_state = atomic_fetch_and_explicit(&rwlock->state, ~STATE_OWNED_BY_WRITER_FLAG,
497*8d67ca89SAndroid Build Coastguard Worker memory_order_release);
498*8d67ca89SAndroid Build Coastguard Worker if (!__state_have_pending_readers_or_writers(old_state)) {
499*8d67ca89SAndroid Build Coastguard Worker return 0;
500*8d67ca89SAndroid Build Coastguard Worker }
501*8d67ca89SAndroid Build Coastguard Worker
502*8d67ca89SAndroid Build Coastguard Worker } else if (__state_owned_by_readers(old_state)) {
503*8d67ca89SAndroid Build Coastguard Worker old_state = atomic_fetch_sub_explicit(&rwlock->state, STATE_READER_COUNT_CHANGE_STEP,
504*8d67ca89SAndroid Build Coastguard Worker memory_order_release);
505*8d67ca89SAndroid Build Coastguard Worker if (!__state_is_last_reader(old_state) || !__state_have_pending_readers_or_writers(old_state)) {
506*8d67ca89SAndroid Build Coastguard Worker return 0;
507*8d67ca89SAndroid Build Coastguard Worker }
508*8d67ca89SAndroid Build Coastguard Worker
509*8d67ca89SAndroid Build Coastguard Worker } else {
510*8d67ca89SAndroid Build Coastguard Worker return EPERM;
511*8d67ca89SAndroid Build Coastguard Worker }
512*8d67ca89SAndroid Build Coastguard Worker
513*8d67ca89SAndroid Build Coastguard Worker // Wake up pending readers or writers.
514*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.lock();
515*8d67ca89SAndroid Build Coastguard Worker if (rwlock->pending_writer_count != 0) {
516*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_writer_wakeup_serial++;
517*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
518*8d67ca89SAndroid Build Coastguard Worker
519*8d67ca89SAndroid Build Coastguard Worker __futex_wake_ex(&rwlock->pending_writer_wakeup_serial, rwlock->pshared, 1);
520*8d67ca89SAndroid Build Coastguard Worker
521*8d67ca89SAndroid Build Coastguard Worker } else if (rwlock->pending_reader_count != 0) {
522*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_reader_wakeup_serial++;
523*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
524*8d67ca89SAndroid Build Coastguard Worker
525*8d67ca89SAndroid Build Coastguard Worker __futex_wake_ex(&rwlock->pending_reader_wakeup_serial, rwlock->pshared, INT_MAX);
526*8d67ca89SAndroid Build Coastguard Worker
527*8d67ca89SAndroid Build Coastguard Worker } else {
528*8d67ca89SAndroid Build Coastguard Worker // It happens when waiters are woken up by timeout.
529*8d67ca89SAndroid Build Coastguard Worker rwlock->pending_lock.unlock();
530*8d67ca89SAndroid Build Coastguard Worker }
531*8d67ca89SAndroid Build Coastguard Worker return 0;
532*8d67ca89SAndroid Build Coastguard Worker }
533