| 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_GCAS |
| 26 |
#include "libatomic_i.h" |
| 27 |
|
| 28 |
|
| 29 |
/* If we natively support the cas, and if we're unconcerned with extra |
| 30 |
barriers (e.g. fully in-order cpu for which barriers are a nop), then |
| 31 |
go ahead and expand the operation inline. */ |
| 32 |
#if !defined(WANT_SPECIALCASE_RELAXED) && !defined(__OPTIMIZE_SIZE__) |
| 33 |
# define EXACT_INLINE(N) \ |
| 34 |
if (C2(HAVE_ATOMIC_CAS_,N)) \ |
| 35 |
return __atomic_compare_exchange_n \ |
| 36 |
(PTR(N,mptr), PTR(N,eptr), *PTR(N,dptr), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) |
| 37 |
#else |
| 38 |
# define EXACT_INLINE(N) |
| 39 |
#endif |
| 40 |
|
| 41 |
/* ... and if all that fails, invoke the function we generated elsewhere. |
| 42 |
Worst case, this will *also* use locks. */ |
| 43 |
#define EXACT(N) \ |
| 44 |
do { \ |
| 45 |
if (!C2(HAVE_INT,N)) break; \ |
| 46 |
if ((uintptr_t)mptr & (N - 1)) break; \ |
| 47 |
EXACT_INLINE (N); \ |
| 48 |
return C3(local_,compare_exchange_,N) \ |
| 49 |
(PTR(N,mptr), PTR(N,eptr), *PTR(N,dptr), smodel, fmodel); \ |
| 50 |
} while (0) |
| 51 |
|
| 52 |
#define LARGER(N) \ |
| 53 |
do { \ |
| 54 |
if (!C2(HAVE_INT,N)) break; \ |
| 55 |
if (!C2(HAVE_ATOMIC_LDST_,N)) break; \ |
| 56 |
if (!C2(MAYBE_HAVE_ATOMIC_CAS_,N)) break; \ |
| 57 |
r = (uintptr_t)mptr & (N - 1); \ |
| 58 |
a = (uintptr_t)mptr & -N; \ |
| 59 |
if (r + n <= N) \ |
| 60 |
{ \ |
| 61 |
pre_barrier (smodel); \ |
| 62 |
u.C2(i,N) = __atomic_load_n (PTR(N,a), __ATOMIC_RELAXED); \ |
| 63 |
do { \ |
| 64 |
if (memcmp (u.b + r, eptr, n) != 0) goto Lfail; \ |
| 65 |
v = u; memcpy (v.b + r, dptr, n); \ |
| 66 |
} while (!(C2(HAVE_ATOMIC_CAS_,N) \ |
| 67 |
? __atomic_compare_exchange_n (PTR(N,a), \ |
| 68 |
&u.C2(i,N), v.C2(i,N), true, \ |
| 69 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED) \ |
| 70 |
: C3(local_,compare_exchange_,N) (PTR(N,a), \ |
| 71 |
&u.C2(i,N), v.C2(i,N), \ |
| 72 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED))); \ |
| 73 |
goto Lsucc; \ |
| 74 |
} \ |
| 75 |
} while (0) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
bool |
| 80 |
libat_compare_exchange (size_t n, void *mptr, void *eptr, void *dptr, |
| 81 |
int smodel, int fmodel) |
| 82 |
{ |
| 83 |
union max_size_u u, v; |
| 84 |
uintptr_t r, a; |
| 85 |
bool ret; |
| 86 |
|
| 87 |
switch (n) |
| 88 |
{ |
| 89 |
case 0: return true; |
| 90 |
case 1: EXACT(1); goto L4; |
| 91 |
case 2: EXACT(2); goto L4; |
| 92 |
case 4: EXACT(4); goto L8; |
| 93 |
case 8: EXACT(8); goto L16; |
| 94 |
case 16: EXACT(16); break; |
| 95 |
|
| 96 |
case 3: L4: LARGER(4); /* FALLTHRU */ |
| 97 |
case 5 ... 7: L8: LARGER(8); /* FALLTHRU */ |
| 98 |
case 9 ... 15: L16: LARGER(16); break; |
| 99 |
|
| 100 |
Lsucc: |
| 101 |
post_barrier (smodel); |
| 102 |
return true; |
| 103 |
Lfail: |
| 104 |
post_barrier (fmodel); |
| 105 |
memcpy (eptr, u.b + r, n); |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
pre_seq_barrier (smodel); |
| 110 |
libat_lock_n (mptr, n); |
| 111 |
|
| 112 |
ret = memcmp (mptr, eptr, n) == 0; |
| 113 |
memcpy ((ret ? mptr : eptr), (ret ? dptr : mptr), n); |
| 114 |
|
| 115 |
libat_unlock_n (mptr, n); |
| 116 |
post_seq_barrier (ret ? smodel : fmodel); |
| 117 |
|
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
EXPORT_ALIAS (compare_exchange); |
| 122 |
#undef LAT_GCAS |