]>
Commit | Line | Data |
---|---|---|
5a414ff7 | 1 | /* Elided pthread mutex trylock. |
d614a753 | 2 | Copyright (C) 2014-2020 Free Software Foundation, Inc. |
5a414ff7 DV |
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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
5a414ff7 DV |
18 | |
19 | #include <pthread.h> | |
20 | #include <pthreadP.h> | |
21 | #include <lowlevellock.h> | |
8bfc4a2a | 22 | #include <htm.h> |
5a414ff7 DV |
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 | |
31 | __lll_trylock_elision (int *futex, short *adapt_count) | |
32 | { | |
5a414ff7 DV |
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. */ | |
8bfc4a2a | 37 | if (__libc_tx_nesting_depth () > 0) |
5a414ff7 DV |
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 | */ | |
8bfc4a2a | 45 | __libc_tabort (_HTM_FIRST_USER_ABORT_CODE | 1); |
dd037fb3 | 46 | __builtin_unreachable (); |
5a414ff7 DV |
47 | } |
48 | ||
dd037fb3 SL |
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 | |
03b00777 SL |
54 | sufficient. */ |
55 | if (atomic_load_relaxed (adapt_count) <= 0 && aconf.try_tbegin > 0) | |
5a414ff7 | 56 | { |
dd037fb3 | 57 | int status = __libc_tbegin ((void *) 0); |
03b00777 | 58 | if (__glibc_likely (status == _HTM_TBEGIN_STARTED)) |
5a414ff7 | 59 | { |
dd037fb3 SL |
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 | |
03b00777 SL |
62 | sure the transaction aborts if another thread acquires the lock |
63 | concurrently. */ | |
64 | if (__glibc_likely (atomic_load_relaxed (futex) == 0)) | |
dd037fb3 | 65 | /* Lock was free. Return to user code in a transaction. */ |
5a414ff7 | 66 | return 0; |
dd037fb3 | 67 | |
03b00777 SL |
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. */ | |
8bfc4a2a | 74 | __libc_tend (); |
5a414ff7 | 75 | /* Note: Changing the adapt_count here might abort a transaction on a |
03b00777 | 76 | different CPU, but that could happen anyway when the futex is |
c813dae5 SL |
77 | acquired, so there's no need to check the nesting depth here. |
78 | See above for why relaxed MO is sufficient. */ | |
5a414ff7 | 79 | if (aconf.skip_lock_busy > 0) |
c813dae5 | 80 | atomic_store_relaxed (adapt_count, aconf.skip_lock_busy); |
5a414ff7 | 81 | } |
dd037fb3 | 82 | else if (status != _HTM_TBEGIN_TRANSIENT) |
5a414ff7 | 83 | { |
dd037fb3 SL |
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; | |
5a414ff7 DV |
90 | } |
91 | /* Could do some retries here. */ | |
92 | } | |
5a414ff7 | 93 | |
03b00777 SL |
94 | /* Use normal locking as fallback path if the transaction does not |
95 | succeed. */ | |
5a414ff7 DV |
96 | return lll_trylock (*futex); |
97 | } |