1 /* Elided pthread mutex trylock.
2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <pthread.h>
20 #include <pthreadP.h>
21 #include <lowlevellock.h>
22 #include <htm.h>
23 #include <elision-conf.h>
24
25 #define aconf __elision_aconf
26
27 /* Try to elide a futex trylock. FUTEX is the futex variable. ADAPT_COUNT is
28 the adaptation counter in the mutex. */
29
30 int
__lll_trylock_elision(int * futex,short * adapt_count)31 __lll_trylock_elision (int *futex, short *adapt_count)
32 {
33 /* Implement POSIX semantics by forbiding nesting elided trylocks.
34 Sorry. After the abort the code is re-executed
35 non transactional and if the lock was already locked
36 return an error. */
37 if (__libc_tx_nesting_depth () > 0)
38 {
39 /* Note that this abort may terminate an outermost transaction that
40 was created outside glibc.
41 This persistently aborts the current transactions to force
42 them to use the default lock instead of retrying transactions
43 until their try_tbegin is zero.
44 */
45 __libc_tabort (_HTM_FIRST_USER_ABORT_CODE | 1);
46 __builtin_unreachable ();
47 }
48
49 /* adapt_count can be accessed concurrently; these accesses can be both
50 inside of transactions (if critical sections are nested and the outer
51 critical section uses lock elision) and outside of transactions. Thus,
52 we need to use atomic accesses to avoid data races. However, the
53 value of adapt_count is just a hint, so relaxed MO accesses are
54 sufficient. */
55 if (atomic_load_relaxed (adapt_count) <= 0 && aconf.try_tbegin > 0)
56 {
57 int status = __libc_tbegin ((void *) 0);
58 if (__glibc_likely (status == _HTM_TBEGIN_STARTED))
59 {
60 /* Check the futex to make sure nobody has touched it in the
61 mean time. This forces the futex into the cache and makes
62 sure the transaction aborts if another thread acquires the lock
63 concurrently. */
64 if (__glibc_likely (atomic_load_relaxed (futex) == 0))
65 /* Lock was free. Return to user code in a transaction. */
66 return 0;
67
68 /* Lock was busy. Fall back to normal locking.
69 This can be the case if e.g. adapt_count was decremented to zero
70 by a former release and another thread has been waken up and
71 acquired it.
72 Since we are in a non-nested transaction there is no need to abort,
73 which is expensive. Simply end the started transaction. */
74 __libc_tend ();
75 /* Note: Changing the adapt_count here might abort a transaction on a
76 different CPU, but that could happen anyway when the futex is
77 acquired, so there's no need to check the nesting depth here.
78 See above for why relaxed MO is sufficient. */
79 if (aconf.skip_lock_busy > 0)
80 atomic_store_relaxed (adapt_count, aconf.skip_lock_busy);
81 }
82 else if (status != _HTM_TBEGIN_TRANSIENT)
83 {
84 /* A persistent abort (cc 1 or 3) indicates that a retry is
85 probably futile. Use the normal locking now and for the
86 next couple of calls.
87 Be careful to avoid writing to the lock. */
88 if (aconf.skip_trylock_internal_abort > 0)
89 *adapt_count = aconf.skip_trylock_internal_abort;
90 }
91 /* Could do some retries here. */
92 }
93
94 /* Use normal locking as fallback path if the transaction does not
95 succeed. */
96 return lll_trylock (*futex);
97 }
98 libc_hidden_def (__lll_trylock_elision)
99