| 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_TAS_N |
| 26 |
#include "libatomic_i.h" |
| 27 |
|
| 28 |
|
| 29 |
/* If we support the builtin, just use it. */ |
| 30 |
#if !DONE && SIZE(HAVE_ATOMIC_TAS) |
| 31 |
bool |
| 32 |
SIZE(libat_test_and_set) (UTYPE *mptr, int smodel) |
| 33 |
{ |
| 34 |
if (maybe_specialcase_relaxed(smodel)) |
| 35 |
return __atomic_test_and_set (mptr, __ATOMIC_RELAXED); |
| 36 |
else if (maybe_specialcase_acqrel(smodel)) |
| 37 |
return __atomic_test_and_set (mptr, __ATOMIC_ACQ_REL); |
| 38 |
else |
| 39 |
return __atomic_test_and_set (mptr, __ATOMIC_SEQ_CST); |
| 40 |
} |
| 41 |
|
| 42 |
#define DONE 1 |
| 43 |
#endif /* HAVE_ATOMIC_TAS */ |
| 44 |
|
| 45 |
|
| 46 |
/* If this type is smaller than word-sized, fall back to a word-sized |
| 47 |
compare-and-swap loop. */ |
| 48 |
#if !DONE && N <= WORDSIZE && defined(atomic_compare_exchange_w) |
| 49 |
bool |
| 50 |
SIZE(libat_test_and_set) (UTYPE *mptr, int smodel) |
| 51 |
{ |
| 52 |
UWORD wval, woldval, shift, *wptr, t; |
| 53 |
|
| 54 |
pre_barrier (smodel); |
| 55 |
|
| 56 |
if (N < WORDSIZE) |
| 57 |
{ |
| 58 |
wptr = (UWORD *)((uintptr_t)mptr & -WORDSIZE); |
| 59 |
shift = SIZE(INVERT_MASK); |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
wptr = (UWORD *)mptr; |
| 64 |
shift = 0; |
| 65 |
} |
| 66 |
|
| 67 |
wval = (UWORD)__GCC_ATOMIC_TEST_AND_SET_TRUEVAL << shift; |
| 68 |
woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED); |
| 69 |
do |
| 70 |
{ |
| 71 |
t = woldval | wval; |
| 72 |
} |
| 73 |
while (!atomic_compare_exchange_w (wptr, &woldval, t, true, |
| 74 |
__ATOMIC_RELAXED, __ATOMIC_RELAXED)); |
| 75 |
|
| 76 |
post_barrier (smodel); |
| 77 |
return (woldval & ((UTYPE) ~(UTYPE) 0 << shift)) != 0; |
| 78 |
} |
| 79 |
|
| 80 |
#define DONE 1 |
| 81 |
#endif /* HAVE_ATOMIC_CAS && N < WORDSIZE */ |
| 82 |
|
| 83 |
|
| 84 |
/* Otherwise, fall back to some sort of protection mechanism. */ |
| 85 |
#if !DONE && N == 1 |
| 86 |
bool |
| 87 |
SIZE(libat_test_and_set) (UTYPE *mptr, int smodel) |
| 88 |
{ |
| 89 |
UTYPE oldval; |
| 90 |
UWORD magic; |
| 91 |
|
| 92 |
pre_seq_barrier (smodel); |
| 93 |
magic = protect_start (mptr); |
| 94 |
|
| 95 |
oldval = *mptr; |
| 96 |
*mptr = __GCC_ATOMIC_TEST_AND_SET_TRUEVAL; |
| 97 |
|
| 98 |
protect_end (mptr, magic); |
| 99 |
post_seq_barrier (smodel); |
| 100 |
|
| 101 |
return oldval != 0; |
| 102 |
} |
| 103 |
|
| 104 |
#define DONE 1 |
| 105 |
#endif /* N == 1 */ |
| 106 |
|
| 107 |
|
| 108 |
#if !DONE |
| 109 |
bool |
| 110 |
SIZE(libat_test_and_set) (UTYPE *mptr, int smodel UNUSED) |
| 111 |
{ |
| 112 |
return libat_test_and_set_1 ((U_1 *)mptr, smodel); |
| 113 |
} |
| 114 |
#endif |
| 115 |
|
| 116 |
EXPORT_ALIAS (SIZE(test_and_set)); |
| 117 |
#undef LAT_TAS_N |