Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / s390 / elision-lock.c
bloba7c0fccd26be3ed9bfed98fdacbb09c94389eff3
1 /* Elided pthread mutex lock.
2 Copyright (C) 2014-2017 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 <htm.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 Do not begin a transaction if another cpu has locked the
55 futex with normal locking. If adapt_count is zero, it remains and the
56 next pthread_mutex_lock call will try to start a transaction again. */
57 if (atomic_load_relaxed (futex) == 0
58 && atomic_load_relaxed (adapt_count) <= 0 && aconf.try_tbegin > 0)
60 int status = __libc_tbegin_retry ((void *) 0, aconf.try_tbegin - 1);
61 if (__builtin_expect (status == _HTM_TBEGIN_STARTED,
62 _HTM_TBEGIN_STARTED))
64 /* Check the futex to make sure nobody has touched it in the
65 mean time. This forces the futex into the cache and makes
66 sure the transaction aborts if some other cpu uses the
67 lock (writes the futex). */
68 if (__builtin_expect (atomic_load_relaxed (futex) == 0, 1))
69 /* Lock was free. Return to user code in a transaction. */
70 return 0;
72 /* Lock was busy. Fall back to normal locking. */
73 if (__builtin_expect (__libc_tx_nesting_depth () <= 1, 1))
75 /* In a non-nested transaction there is no need to abort,
76 which is expensive. Simply end the started transaction. */
77 __libc_tend ();
78 /* Don't try to use transactions for the next couple of times.
79 See above for why relaxed MO is sufficient. */
80 if (aconf.skip_lock_busy > 0)
81 atomic_store_relaxed (adapt_count, aconf.skip_lock_busy);
83 else /* nesting depth is > 1 */
85 /* A nested transaction will abort eventually because it
86 cannot make any progress before *futex changes back to 0.
87 So we may as well abort immediately.
88 This persistently aborts the outer transaction to force
89 the outer mutex use the default lock instead of retrying
90 with transactions until the try_tbegin of the outer mutex
91 is zero.
92 The adapt_count of this inner mutex is not changed,
93 because using the default lock with the inner mutex
94 would abort the outer transaction. */
95 __libc_tabort (_HTM_FIRST_USER_ABORT_CODE | 1);
96 __builtin_unreachable ();
99 else if (status != _HTM_TBEGIN_TRANSIENT)
101 /* A persistent abort (cc 1 or 3) indicates that a retry is
102 probably futile. Use the normal locking now and for the
103 next couple of calls.
104 Be careful to avoid writing to the lock. See above for why
105 relaxed MO is sufficient. */
106 if (aconf.skip_lock_internal_abort > 0)
107 atomic_store_relaxed (adapt_count,
108 aconf.skip_lock_internal_abort);
110 else
112 /* The transaction failed for some retries with
113 _HTM_TBEGIN_TRANSIENT. Use the normal locking now and for the
114 next couple of calls. */
115 if (aconf.skip_lock_out_of_tbegin_retries > 0)
116 atomic_store_relaxed (adapt_count,
117 aconf.skip_lock_out_of_tbegin_retries);
121 /* Use normal locking as fallback path if transaction does not succeed. */
122 return LLL_LOCK ((*futex), private);