| 1 |
/* Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Richard Henderson <[email protected]>. |
| 3 |
|
| 4 |
This file is part of the GNU Atomic Library (libatomic). |
| 5 |
|
| 6 |
Libatomic is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 |
more details. |
| 15 |
|
| 16 |
Under Section 7 of GPL version 3, you are granted additional |
| 17 |
permissions described in the GCC Runtime Library Exception, version |
| 18 |
3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License and |
| 21 |
a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
<http://www.gnu.org/licenses/>. */ |
| 24 |
|
| 25 |
#define LAT_CAS_N |
| 26 |
#include "libatomic_i.h" |
| 27 |
|
| 28 |
|
| 29 |
/* If we support the builtin, just use it. */ |
| 30 |
#if !DONE && defined(atomic_compare_exchange_n) |
| 31 |
bool |
| 32 |
SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval, |
| 33 |
int smodel, int fmodel UNUSED) |
| 34 |
{ |
| 35 |
if (maybe_specialcase_relaxed(smodel)) |
| 36 |
return atomic_compare_exchange_n (mptr, eptr, newval, false, |
| 37 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED); |
| 38 |
else if (maybe_specialcase_acqrel(smodel)) |
| 39 |
return atomic_compare_exchange_n (mptr, eptr, newval, false, |
| 40 |
__ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
| 41 |
else |
| 42 |
return atomic_compare_exchange_n (mptr, eptr, newval, false, |
| 43 |
__ATOMIC_SEQ_CST, __ATOMIC_RELAXED); |
| 44 |
} |
| 45 |
|
| 46 |
#define DONE 1 |
| 47 |
#endif /* HAVE_ATOMIC_CAS */ |
| 48 |
|
| 49 |
|
| 50 |
/* If this type is not larger than word-sized, fall back to a word-sized |
| 51 |
compare-and-swap loop, possibly assisted by the OS. */ |
| 52 |
#if !DONE && N <= WORDSIZE && defined(atomic_compare_exchange_w) |
| 53 |
bool |
| 54 |
SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval, |
| 55 |
int smodel, int fmodel) |
| 56 |
{ |
| 57 |
UWORD mask, shift, weval, woldval, wnewval, t, *wptr; |
| 58 |
|
| 59 |
pre_barrier (smodel); |
| 60 |
|
| 61 |
if (N < WORDSIZE) |
| 62 |
{ |
| 63 |
wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE); |
| 64 |
shift = (((uintptr_t)mptr % WORDSIZE) * CHAR_BIT) ^ SIZE(INVERT_MASK); |
| 65 |
mask = SIZE(MASK) << shift; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
wptr = (UWORD *)mptr; |
| 70 |
shift = 0; |
| 71 |
mask = -1; |
| 72 |
} |
| 73 |
|
| 74 |
weval = (UWORD)*eptr << shift; |
| 75 |
wnewval = (UWORD)newval << shift; |
| 76 |
woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED); |
| 77 |
do |
| 78 |
{ |
| 79 |
if ((woldval & mask) != weval) |
| 80 |
goto failure; |
| 81 |
t = (woldval & ~mask) | wnewval; |
| 82 |
} |
| 83 |
while (!atomic_compare_exchange_w (wptr, &woldval, t, true, |
| 84 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED)); |
| 85 |
post_barrier (smodel); |
| 86 |
return true; |
| 87 |
|
| 88 |
failure: |
| 89 |
*eptr = woldval >> shift; |
| 90 |
post_barrier (fmodel); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
#define DONE 1 |
| 95 |
#endif /* HAVE_ATOMIC_CAS && N <= WORDSIZE */ |
| 96 |
|
| 97 |
|
| 98 |
/* Otherwise, fall back to some sort of protection mechanism. */ |
| 99 |
#if !DONE |
| 100 |
bool |
| 101 |
SIZE(libat_compare_exchange) (UTYPE *mptr, UTYPE *eptr, UTYPE newval, |
| 102 |
int smodel, int fmodel UNUSED) |
| 103 |
{ |
| 104 |
UTYPE oldval; |
| 105 |
UWORD magic; |
| 106 |
bool ret; |
| 107 |
|
| 108 |
pre_seq_barrier (smodel); |
| 109 |
magic = protect_start (mptr); |
| 110 |
|
| 111 |
oldval = *mptr; |
| 112 |
ret = (oldval == *eptr); |
| 113 |
if (ret) |
| 114 |
*mptr = newval; |
| 115 |
else |
| 116 |
*eptr = oldval; |
| 117 |
|
| 118 |
protect_end (mptr, magic); |
| 119 |
post_seq_barrier (smodel); |
| 120 |
|
| 121 |
return ret; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
EXPORT_ALIAS (SIZE(compare_exchange)); |
| 126 |
#undef LAT_CAS_N |