S390: Use C11-like atomics instead of plain memory accesses in lock elision code.
[glibc.git] / sysdeps / unix / sysv / linux / s390 / elision-lock.c
blob1876d2128d3973512338b7c6f8a0a88410a4b392
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/>. */
19 #include <pthread.h>
20 #include <pthreadP.h>
21 #include <lowlevellock.h>
22 #include <htmintrin.h>
23 #include <elision-conf.h>
24 #include <stdint.h>
26 #if !defined(LLL_LOCK) && !defined(EXTRAARG)
27 /* Make sure the configuration code is always linked in for static
28 libraries. */
29 #include "elision-conf.c"
30 #endif
32 #ifndef EXTRAARG
33 #define EXTRAARG
34 #endif
35 #ifndef LLL_LOCK
36 #define LLL_LOCK(a,b) lll_lock(a,b), 0
37 #endif
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. */
45 int
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
53 sufficient. */
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);
60 goto use_lock;
63 __asm__ volatile (".machinemode \"zarch_nohighgprs\"\n\t"
64 ".machine \"all\""
65 : : : "memory");
67 int try_tbegin;
68 for (try_tbegin = aconf.try_tbegin;
69 try_tbegin > 0;
70 try_tbegin--)
72 unsigned status;
73 if (__builtin_expect
74 ((status = __builtin_tbegin((void *)0)) == _HTM_TBEGIN_STARTED, 1))
76 if (*futex == 0)
77 return 0;
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. */
83 __builtin_tend ();
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);
88 goto use_lock;
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
98 is zero.
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);
106 else
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);
118 goto use_lock;
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);
128 use_lock:
129 return LLL_LOCK ((*futex), private);