S390: Use C11-like atomics instead of plain memory accesses in lock elision code.
[glibc.git] / sysdeps / unix / sysv / linux / s390 / elision-trylock.c
bloba3252b83ce7b7dea06311c17a5740f8219991aee
1 /* Elided pthread mutex trylock.
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>
25 #define aconf __elision_aconf
27 /* Try to elide a futex trylock. FUTEX is the futex variable. ADAPT_COUNT is
28 the adaptation counter in the mutex. */
30 int
31 __lll_trylock_elision (int *futex, short *adapt_count)
33 __asm__ __volatile__ (".machinemode \"zarch_nohighgprs\"\n\t"
34 ".machine \"all\""
35 : : : "memory");
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)
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.
49 __builtin_tabort (_HTM_FIRST_USER_ABORT_CODE | 1);
52 /* Only try a transaction if it's worth it. See __lll_lock_elision for
53 why we need atomic accesses. Relaxed MO is sufficient because this is
54 just a hint. */
55 if (atomic_load_relaxed (adapt_count) <= 0)
57 unsigned status;
59 if (__builtin_expect
60 ((status = __builtin_tbegin ((void *)0)) == _HTM_TBEGIN_STARTED, 1))
62 if (*futex == 0)
63 return 0;
64 /* Lock was busy. Fall back to normal locking. */
65 /* Since we are in a non-nested transaction there is no need to abort,
66 which is expensive. */
67 __builtin_tend ();
68 /* Note: Changing the adapt_count here might abort a transaction on a
69 different cpu, but that could happen anyway when the futex is
70 acquired, so there's no need to check the nesting depth here.
71 See above for why relaxed MO is sufficient. */
72 if (aconf.skip_lock_busy > 0)
73 atomic_store_relaxed (adapt_count, aconf.skip_lock_busy);
75 else
77 if (status != _HTM_TBEGIN_TRANSIENT)
79 /* A persistent abort (cc 1 or 3) indicates that a retry is
80 probably futile. Use the normal locking now and for the
81 next couple of calls.
82 Be careful to avoid writing to the lock. */
83 if (aconf.skip_trylock_internal_abort > 0)
84 *adapt_count = aconf.skip_trylock_internal_abort;
87 /* Could do some retries here. */
89 else
91 /* Lost updates are possible, but harmless. Due to races this might lead
92 to *adapt_count becoming less than zero. */
93 atomic_store_relaxed (adapt_count,
94 atomic_load_relaxed (adapt_count) - 1);
97 return lll_trylock (*futex);