| 1 |
// Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++20 } } |
| 19 |
// { dg-require-atomic-cmpxchg-word "" } |
| 20 |
// { dg-add-options libatomic } |
| 21 |
|
| 22 |
#include <atomic> |
| 23 |
#include <limits.h> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
struct X |
| 27 |
{ |
| 28 |
X() = default; |
| 29 |
X(int i) : i(i) { } |
| 30 |
bool operator==(int rhs) const { return i == rhs; } |
| 31 |
int i; |
| 32 |
}; |
| 33 |
|
| 34 |
void |
| 35 |
test01() |
| 36 |
{ |
| 37 |
X value; |
| 38 |
|
| 39 |
{ |
| 40 |
const auto mo = std::memory_order_relaxed; |
| 41 |
std::atomic_ref<X> a(value); |
| 42 |
bool ok = a.is_lock_free(); |
| 43 |
if constexpr (std::atomic_ref<X>::is_always_lock_free) |
| 44 |
VERIFY( ok ); |
| 45 |
a = X{}; |
| 46 |
VERIFY( a.load() == 0 ); |
| 47 |
VERIFY( a.load(mo) == 0 ); |
| 48 |
a.store(1); |
| 49 |
VERIFY( a.load() == 1 ); |
| 50 |
auto v = a.exchange(2); |
| 51 |
VERIFY( a.load() == 2 ); |
| 52 |
VERIFY( v == 1 ); |
| 53 |
v = a.exchange(3, mo); |
| 54 |
VERIFY( a.load() == 3 ); |
| 55 |
VERIFY( v == 2 ); |
| 56 |
|
| 57 |
auto expected = a.load(); |
| 58 |
while (!a.compare_exchange_weak(expected, 4, mo, mo)) |
| 59 |
{ /* weak form can fail spuriously */ } |
| 60 |
VERIFY( a.load() == 4 ); |
| 61 |
VERIFY( expected == 3 ); |
| 62 |
expected = 1; |
| 63 |
ok = a.compare_exchange_weak(expected, 5, mo, mo); |
| 64 |
VERIFY( !ok && a.load() == 4 && expected == 4 ); |
| 65 |
ok = a.compare_exchange_strong(expected, 5, mo, mo); |
| 66 |
VERIFY( ok && a.load() == 5 && expected == 4 ); |
| 67 |
expected = 0; |
| 68 |
ok = a.compare_exchange_strong(expected, 3, mo, mo); |
| 69 |
VERIFY( !ok && a.load() == 5 && expected == 5 ); |
| 70 |
|
| 71 |
while (!a.compare_exchange_weak(expected, 4)) |
| 72 |
{ /* weak form can fail spuriously */ } |
| 73 |
VERIFY( a.load() == 4 && expected == 5 ); |
| 74 |
expected = a.load(); |
| 75 |
while (!a.compare_exchange_weak(expected, 6, mo)) |
| 76 |
{ /* weak form can fail spuriously */ } |
| 77 |
VERIFY( a.load() == 6 && expected == 4 ); |
| 78 |
expected = a.load(); |
| 79 |
expected.i += 1; |
| 80 |
ok = a.compare_exchange_weak(expected, -8); |
| 81 |
VERIFY( !ok && a.load() == 6 && expected == 6 ); |
| 82 |
expected = a.load(); |
| 83 |
expected.i += 1; |
| 84 |
ok = a.compare_exchange_weak(expected, 8, mo); |
| 85 |
VERIFY( !ok && a.load() == 6 && expected == 6 ); |
| 86 |
|
| 87 |
ok = a.compare_exchange_strong(expected, -6); |
| 88 |
VERIFY( ok && a.load() == -6 && expected == 6 ); |
| 89 |
expected = a.load(); |
| 90 |
ok = a.compare_exchange_strong(expected, 7, mo); |
| 91 |
VERIFY( ok && a.load() == 7 && expected == -6 ); |
| 92 |
expected = a.load(); |
| 93 |
expected.i += 1; |
| 94 |
ok = a.compare_exchange_strong(expected, 2); |
| 95 |
VERIFY( !ok && a.load() == 7 && expected == 7 ); |
| 96 |
expected = a.load(); |
| 97 |
expected.i += 1; |
| 98 |
ok = a.compare_exchange_strong(expected, 2, mo); |
| 99 |
VERIFY( !ok && a.load() == 7 && expected == 7 ); |
| 100 |
} |
| 101 |
|
| 102 |
VERIFY( value == 7 ); |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
test02() |
| 107 |
{ |
| 108 |
X i; |
| 109 |
std::atomic_ref<X> a0(i); |
| 110 |
std::atomic_ref<X> a1(i); |
| 111 |
std::atomic_ref<X> a2(a0); |
| 112 |
a0 = 42; |
| 113 |
VERIFY( a1.load() == 42 ); |
| 114 |
VERIFY( a2.load() == 42 ); |
| 115 |
} |
| 116 |
|
| 117 |
int |
| 118 |
main() |
| 119 |
{ |
| 120 |
test01(); |
| 121 |
test02(); |
| 122 |
} |