1 /* elide.h: Generic lock elision support for powerpc.
2 Copyright (C) 2015-2024 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 <https://www.gnu.org/licenses/>. */
23 # include <elision-conf.h>
25 /* Get the new value of adapt_count according to the elision
26 configurations. Returns true if the system should retry again or false
29 __get_new_count (uint8_t *adapt_count
, int attempt
)
31 /* A persistent failure indicates that a retry will probably
32 result in another failure. Use normal locking now and
33 for the next couple of calls. */
34 if (_TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
36 if (__elision_aconf
.skip_lock_internal_abort
> 0)
37 *adapt_count
= __elision_aconf
.skip_lock_internal_abort
;
40 /* Same logic as above, but for a number of temporary failures in a
42 else if (attempt
<= 1 && __elision_aconf
.skip_lock_out_of_tbegin_retries
> 0
43 && __elision_aconf
.try_tbegin
> 0)
44 *adapt_count
= __elision_aconf
.skip_lock_out_of_tbegin_retries
;
50 The evaluation of the macro expression is_lock_free encompasses one or
51 more loads from memory locations that are concurrently modified by other
52 threads. For lock elision to work, this evaluation and the rest of the
53 critical section protected by the lock must be atomic because an
54 execution with lock elision must be equivalent to an execution in which
55 the lock would have been actually acquired and released. Therefore, we
56 evaluate is_lock_free inside of the transaction that represents the
57 critical section for which we want to use lock elision, which ensures
58 the atomicity that we require. */
60 /* Returns 0 if the lock defined by is_lock_free was elided.
61 ADAPT_COUNT is a per-lock state variable. */
62 # define ELIDE_LOCK(adapt_count, is_lock_free) \
65 if (adapt_count > 0) \
68 for (int i = __elision_aconf.try_tbegin; i > 0; i--) \
70 if (__libc_tbegin (0)) \
77 __libc_tabort (_ABORT_LOCK_BUSY); \
80 if (!__get_new_count (&adapt_count,i)) \
86 # define ELIDE_TRYLOCK(adapt_count, is_lock_free, write) \
89 if (__elision_aconf.try_tbegin > 0) \
92 __libc_tabort (_ABORT_NESTED_TRYLOCK); \
93 ret = ELIDE_LOCK (adapt_count, is_lock_free); \
100 __elide_unlock (int is_lock_free
)
104 /* This code is expected to crash when trying to unlock a lock not
105 held by this thread. More information is available in the
106 __pthread_rwlock_unlock() implementation. */
113 # define ELIDE_UNLOCK(is_lock_free) \
114 __elide_unlock (is_lock_free)