]>
Commit | Line | Data |
---|---|---|
5a414ff7 | 1 | /* Elided pthread mutex trylock. |
f7a9f785 | 2 | Copyright (C) 2014-2016 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 | |
17 | <http://www.gnu.org/licenses/>. */ | |
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); |
5a414ff7 DV |
46 | } |
47 | ||
c813dae5 SL |
48 | /* Only try a transaction if it's worth it. See __lll_lock_elision for |
49 | why we need atomic accesses. Relaxed MO is sufficient because this is | |
50 | just a hint. */ | |
51 | if (atomic_load_relaxed (adapt_count) <= 0) | |
5a414ff7 | 52 | { |
8bfc4a2a | 53 | int status; |
5a414ff7 DV |
54 | |
55 | if (__builtin_expect | |
8bfc4a2a | 56 | ((status = __libc_tbegin ((void *) 0)) == _HTM_TBEGIN_STARTED, 1)) |
5a414ff7 DV |
57 | { |
58 | if (*futex == 0) | |
59 | return 0; | |
60 | /* Lock was busy. Fall back to normal locking. */ | |
61 | /* Since we are in a non-nested transaction there is no need to abort, | |
62 | which is expensive. */ | |
8bfc4a2a | 63 | __libc_tend (); |
5a414ff7 DV |
64 | /* Note: Changing the adapt_count here might abort a transaction on a |
65 | different cpu, but that could happen anyway when the futex is | |
c813dae5 SL |
66 | acquired, so there's no need to check the nesting depth here. |
67 | See above for why relaxed MO is sufficient. */ | |
5a414ff7 | 68 | if (aconf.skip_lock_busy > 0) |
c813dae5 | 69 | atomic_store_relaxed (adapt_count, aconf.skip_lock_busy); |
5a414ff7 DV |
70 | } |
71 | else | |
72 | { | |
73 | if (status != _HTM_TBEGIN_TRANSIENT) | |
74 | { | |
75 | /* A persistent abort (cc 1 or 3) indicates that a retry is | |
76 | probably futile. Use the normal locking now and for the | |
77 | next couple of calls. | |
78 | Be careful to avoid writing to the lock. */ | |
79 | if (aconf.skip_trylock_internal_abort > 0) | |
80 | *adapt_count = aconf.skip_trylock_internal_abort; | |
81 | } | |
82 | } | |
83 | /* Could do some retries here. */ | |
84 | } | |
85 | else | |
86 | { | |
87 | /* Lost updates are possible, but harmless. Due to races this might lead | |
88 | to *adapt_count becoming less than zero. */ | |
c813dae5 SL |
89 | atomic_store_relaxed (adapt_count, |
90 | atomic_load_relaxed (adapt_count) - 1); | |
5a414ff7 DV |
91 | } |
92 | ||
93 | return lll_trylock (*futex); | |
94 | } |