]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/s390/elision-lock.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / s390 / elision-lock.c
1 /* Elided pthread mutex lock.
2 Copyright (C) 2014-2019 Free Software Foundation, Inc.
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 <htm.h>
23 #include <elision-conf.h>
24 #include <stdint.h>
25
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
31
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
38
39 #define aconf __elision_aconf
40
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. */
44
45 int
46 __lll_lock_elision (int *futex, short *adapt_count, EXTRAARG int private)
47 {
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 && aconf.try_tbegin > 0)
55 {
56 /* Start a transaction and retry it automatically if it aborts with
57 _HTM_TBEGIN_TRANSIENT. This macro calls tbegin at most retry_cnt
58 + 1 times. The second argument is considered as retry_cnt. */
59 int status = __libc_tbegin_retry ((void *) 0, aconf.try_tbegin - 1);
60 if (__glibc_likely (status == _HTM_TBEGIN_STARTED))
61 {
62 /* Check the futex to make sure nobody has touched it in the
63 mean time. This forces the futex into the cache and makes
64 sure the transaction aborts if another thread acquires the lock
65 concurrently. */
66 if (__glibc_likely (atomic_load_relaxed (futex) == 0))
67 /* Lock was free. Return to user code in a transaction. */
68 return 0;
69
70 /* Lock was busy. Fall back to normal locking.
71 This can be the case if e.g. adapt_count was decremented to zero
72 by a former release and another thread has been waken up and
73 acquired it. */
74 if (__glibc_likely (__libc_tx_nesting_depth () <= 1))
75 {
76 /* In a non-nested transaction there is no need to abort,
77 which is expensive. Simply end the started transaction. */
78 __libc_tend ();
79 /* Don't try to use transactions for the next couple of times.
80 See above for why relaxed MO is sufficient. */
81 if (aconf.skip_lock_busy > 0)
82 atomic_store_relaxed (adapt_count, aconf.skip_lock_busy);
83 }
84 else /* nesting depth is > 1 */
85 {
86 /* A nested transaction will abort eventually because it
87 cannot make any progress before *futex changes back to 0.
88 So we may as well abort immediately.
89 This persistently aborts the outer transaction to force
90 the outer mutex use the default lock instead of retrying
91 with transactions until the try_tbegin of the outer mutex
92 is zero.
93 The adapt_count of this inner mutex is not changed,
94 because using the default lock with the inner mutex
95 would abort the outer transaction. */
96 __libc_tabort (_HTM_FIRST_USER_ABORT_CODE | 1);
97 __builtin_unreachable ();
98 }
99 }
100 else if (status != _HTM_TBEGIN_TRANSIENT)
101 {
102 /* A persistent abort (cc 1 or 3) indicates that a retry is
103 probably futile. Use the normal locking now and for the
104 next couple of calls.
105 Be careful to avoid writing to the lock. See above for why
106 relaxed MO is sufficient. */
107 if (aconf.skip_lock_internal_abort > 0)
108 atomic_store_relaxed (adapt_count,
109 aconf.skip_lock_internal_abort);
110 }
111 else
112 {
113 /* The transaction failed for some retries with
114 _HTM_TBEGIN_TRANSIENT. Use the normal locking now and for the
115 next couple of calls. */
116 if (aconf.skip_lock_out_of_tbegin_retries > 0)
117 atomic_store_relaxed (adapt_count,
118 aconf.skip_lock_out_of_tbegin_retries);
119 }
120 }
121
122 /* Use normal locking as fallback path if the transaction does not
123 succeed. */
124 return LLL_LOCK ((*futex), private);
125 }