1 /* Elided pthread mutex lock.
2 Copyright (C) 2014-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <lowlevellock.h>
22 #include <htmintrin.h>
23 #include <elision-conf.h>
26 #if !defined(LLL_LOCK) && !defined(EXTRAARG)
27 /* Make sure the configuration code is always linked in for static
29 #include "elision-conf.c"
36 #define LLL_LOCK(a,b) lll_lock(a,b), 0
39 #define aconf __elision_aconf
41 /* Adaptive lock using transactions.
42 By default the lock region is run as a transaction, and when it
43 aborts or the lock is busy the lock adapts itself. */
46 __lll_lock_elision (int *futex
, short *adapt_count
, EXTRAARG
int private)
48 /* adapt_count can be accessed concurrently; these accesses can be both
49 inside of transactions (if critical sections are nested and the outer
50 critical section uses lock elision) and outside of transactions. Thus,
51 we need to use atomic accesses to avoid data races. However, the
52 value of adapt_count is just a hint, so relaxed MO accesses are
54 if (atomic_load_relaxed (adapt_count
) > 0)
56 /* Lost updates are possible, but harmless. Due to races this might lead
57 to *adapt_count becoming less than zero. */
58 atomic_store_relaxed (adapt_count
,
59 atomic_load_relaxed (adapt_count
) - 1);
63 __asm__
volatile (".machinemode \"zarch_nohighgprs\"\n\t"
68 for (try_tbegin
= aconf
.try_tbegin
;
74 ((status
= __builtin_tbegin((void *)0)) == _HTM_TBEGIN_STARTED
, 1))
78 /* Lock was busy. Fall back to normal locking. */
79 if (__builtin_expect (__builtin_tx_nesting_depth (), 1))
81 /* In a non-nested transaction there is no need to abort,
82 which is expensive. */
84 /* Don't try to use transactions for the next couple of times.
85 See above for why relaxed MO is sufficient. */
86 if (aconf
.skip_lock_busy
> 0)
87 atomic_store_relaxed (adapt_count
, aconf
.skip_lock_busy
);
90 else /* nesting depth is > 1 */
92 /* A nested transaction will abort eventually because it
93 cannot make any progress before *futex changes back to 0.
94 So we may as well abort immediately.
95 This persistently aborts the outer transaction to force
96 the outer mutex use the default lock instead of retrying
97 with transactions until the try_tbegin of the outer mutex
99 The adapt_count of this inner mutex is not changed,
100 because using the default lock with the inner mutex
101 would abort the outer transaction.
103 __builtin_tabort (_HTM_FIRST_USER_ABORT_CODE
| 1);
108 if (status
!= _HTM_TBEGIN_TRANSIENT
)
110 /* A persistent abort (cc 1 or 3) indicates that a retry is
111 probably futile. Use the normal locking now and for the
112 next couple of calls.
113 Be careful to avoid writing to the lock. See above for why
114 relaxed MO is sufficient. */
115 if (aconf
.skip_lock_internal_abort
> 0)
116 atomic_store_relaxed (adapt_count
,
117 aconf
.skip_lock_internal_abort
);
123 /* Same logic as above, but for for a number of temporary failures in a
124 row. See above for why relaxed MO is sufficient. */
125 if (aconf
.skip_lock_out_of_tbegin_retries
> 0 && aconf
.try_tbegin
> 0)
126 atomic_store_relaxed (adapt_count
, aconf
.skip_lock_out_of_tbegin_retries
);
129 return LLL_LOCK ((*futex
), private);