]>
Commit | Line | Data |
---|---|---|
5a414ff7 | 1 | /* Elided pthread mutex trylock. |
b168057a | 2 | Copyright (C) 2014-2015 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> | |
22 | #include <htmintrin.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 | |
31 | __lll_trylock_elision (int *futex, short *adapt_count) | |
32 | { | |
33 | __asm__ volatile (".machinemode \"zarch_nohighgprs\"\n\t" | |
34 | ".machine \"all\"" | |
35 | : : : "memory"); | |
36 | ||
37 | /* Implement POSIX semantics by forbiding nesting elided trylocks. | |
38 | Sorry. After the abort the code is re-executed | |
39 | non transactional and if the lock was already locked | |
40 | return an error. */ | |
41 | if (__builtin_tx_nesting_depth () > 0) | |
42 | { | |
43 | /* Note that this abort may terminate an outermost transaction that | |
44 | was created outside glibc. | |
45 | This persistently aborts the current transactions to force | |
46 | them to use the default lock instead of retrying transactions | |
47 | until their try_tbegin is zero. | |
48 | */ | |
49 | __builtin_tabort (_HTM_FIRST_USER_ABORT_CODE | 1); | |
50 | } | |
51 | ||
52 | /* Only try a transaction if it's worth it. */ | |
53 | if (*adapt_count <= 0) | |
54 | { | |
55 | unsigned status; | |
56 | ||
57 | if (__builtin_expect | |
58 | ((status = __builtin_tbegin ((void *)0)) == _HTM_TBEGIN_STARTED, 1)) | |
59 | { | |
60 | if (*futex == 0) | |
61 | return 0; | |
62 | /* Lock was busy. Fall back to normal locking. */ | |
63 | /* Since we are in a non-nested transaction there is no need to abort, | |
64 | which is expensive. */ | |
65 | __builtin_tend (); | |
66 | /* Note: Changing the adapt_count here might abort a transaction on a | |
67 | different cpu, but that could happen anyway when the futex is | |
68 | acquired, so there's no need to check the nesting depth here. */ | |
69 | if (aconf.skip_lock_busy > 0) | |
70 | *adapt_count = aconf.skip_lock_busy; | |
71 | } | |
72 | else | |
73 | { | |
74 | if (status != _HTM_TBEGIN_TRANSIENT) | |
75 | { | |
76 | /* A persistent abort (cc 1 or 3) indicates that a retry is | |
77 | probably futile. Use the normal locking now and for the | |
78 | next couple of calls. | |
79 | Be careful to avoid writing to the lock. */ | |
80 | if (aconf.skip_trylock_internal_abort > 0) | |
81 | *adapt_count = aconf.skip_trylock_internal_abort; | |
82 | } | |
83 | } | |
84 | /* Could do some retries here. */ | |
85 | } | |
86 | else | |
87 | { | |
88 | /* Lost updates are possible, but harmless. Due to races this might lead | |
89 | to *adapt_count becoming less than zero. */ | |
90 | (*adapt_count)--; | |
91 | } | |
92 | ||
93 | return lll_trylock (*futex); | |
94 | } |