1 | /* Copyright (C) 2002-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include "pthreadP.h" |
21 | #include <atomic.h> |
22 | #include <shlib-compat.h> |
23 | |
24 | /* See pthread_rwlock_common.c for an overview. */ |
25 | int |
26 | ___pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) |
27 | { |
28 | /* When in a trywrlock, we can acquire the write lock if it is in states |
29 | #1 (idle and read phase) and #5 (idle and write phase), and also in #6 |
30 | (readers waiting, write phase) if we prefer writers. |
31 | If we observe any other state, we are allowed to fail and do not need to |
32 | "synchronize memory" as specified by POSIX (hence relaxed MO is |
33 | sufficient for the first load and the CAS failure path). |
34 | We face a similar issue as in tryrdlock in that we need to both avoid |
35 | live-locks / starvation and must not fail spuriously (see there for |
36 | further comments) -- and thus must loop until we get a definitive |
37 | observation or state change. */ |
38 | unsigned int r = atomic_load_relaxed (&rwlock->__data.__readers); |
39 | bool prefer_writer = |
40 | (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP); |
41 | while (((r & PTHREAD_RWLOCK_WRLOCKED) == 0) |
42 | && (((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0) |
43 | || (prefer_writer && ((r & PTHREAD_RWLOCK_WRPHASE) != 0)))) |
44 | { |
45 | /* Try to transition to states #7 or #8 (i.e., acquire the lock). */ |
46 | if (atomic_compare_exchange_weak_acquire ( |
47 | &rwlock->__data.__readers, &r, |
48 | r | PTHREAD_RWLOCK_WRPHASE | PTHREAD_RWLOCK_WRLOCKED)) |
49 | { |
50 | /* We have become the primary writer and we cannot have shared |
51 | the PTHREAD_RWLOCK_FUTEX_USED flag with someone else, so we |
52 | can simply enable blocking (see full wrlock code). */ |
53 | atomic_store_relaxed (&rwlock->__data.__writers_futex, 1); |
54 | /* If we started a write phase, we need to enable readers to |
55 | wait. If we did not, we must not change it because other threads |
56 | may have set the PTHREAD_RWLOCK_FUTEX_USED in the meantime. */ |
57 | if ((r & PTHREAD_RWLOCK_WRPHASE) == 0) |
58 | atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1); |
59 | atomic_store_relaxed (&rwlock->__data.__cur_writer, |
60 | THREAD_GETMEM (THREAD_SELF, tid)); |
61 | return 0; |
62 | } |
63 | /* TODO Back-off. */ |
64 | /* See above. */ |
65 | } |
66 | return EBUSY; |
67 | } |
68 | versioned_symbol (libc, ___pthread_rwlock_trywrlock, |
69 | pthread_rwlock_trywrlock, GLIBC_2_34); |
70 | libc_hidden_ver (___pthread_rwlock_trywrlock, __pthread_rwlock_trywrlock) |
71 | |
72 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1, GLIBC_2_34) |
73 | compat_symbol (libpthread, ___pthread_rwlock_trywrlock, |
74 | pthread_rwlock_trywrlock, GLIBC_2_1); |
75 | #endif |
76 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_2, GLIBC_2_34) |
77 | compat_symbol (libpthread, ___pthread_rwlock_trywrlock, |
78 | __pthread_rwlock_trywrlock, GLIBC_2_2); |
79 | #endif |
80 | |