| 1 | // Copyright (C) 2018-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 compile { target c++11 } } |
| 19 | // { dg-require-atomic-builtins "" } |
| 20 | |
| 21 | #include <atomic> |
| 22 | |
| 23 | void |
| 24 | test01() |
| 25 | { |
| 26 | volatile std::atomic<int> v; |
| 27 | std::atomic<long> a; |
| 28 | const std::memory_order mo = std::memory_order_seq_cst; |
| 29 | int i = 0; |
| 30 | long l = 0; |
| 31 | auto r1 = atomic_is_lock_free(&v); |
| 32 | static_assert( std::is_same<decltype(r1), bool>::value, "" ); |
| 33 | auto r2 = atomic_is_lock_free(&a); |
| 34 | static_assert( std::is_same<decltype(r2), bool>::value, "" ); |
| 35 | atomic_init(&v, i); |
| 36 | atomic_init(&a, l); |
| 37 | atomic_store(&v, i); |
| 38 | atomic_store(&a, l); |
| 39 | atomic_store_explicit(&v, i, mo); |
| 40 | atomic_store_explicit(&a, l, mo); |
| 41 | auto r3 = atomic_load(&v); |
| 42 | static_assert( std::is_same<decltype(r3), int>::value, "" ); |
| 43 | auto r4 = atomic_load(&a); |
| 44 | static_assert( std::is_same<decltype(r4), long>::value, "" ); |
| 45 | auto r5 = atomic_load_explicit(&v, mo); |
| 46 | static_assert( std::is_same<decltype(r5), int>::value, "" ); |
| 47 | auto r6 = atomic_load_explicit(&a, mo); |
| 48 | static_assert( std::is_same<decltype(r6), long>::value, "" ); |
| 49 | auto r7 = atomic_exchange(&v, i); |
| 50 | static_assert( std::is_same<decltype(r7), int>::value, "" ); |
| 51 | auto r8 = atomic_exchange(&a, l); |
| 52 | static_assert( std::is_same<decltype(r8), long>::value, "" ); |
| 53 | auto r9 = atomic_exchange_explicit(&v, i, mo); |
| 54 | static_assert( std::is_same<decltype(r9), int>::value, "" ); |
| 55 | auto r10 = atomic_exchange_explicit(&a, l, mo); |
| 56 | static_assert( std::is_same<decltype(r10), long>::value, "" ); |
| 57 | auto r11 = atomic_compare_exchange_weak(&v, &i, i); |
| 58 | static_assert( std::is_same<decltype(r11), bool>::value, "" ); |
| 59 | auto r12 = atomic_compare_exchange_weak(&a, &l, l); |
| 60 | static_assert( std::is_same<decltype(r12), bool>::value, "" ); |
| 61 | auto r13 = atomic_compare_exchange_strong(&v, &i, i); |
| 62 | static_assert( std::is_same<decltype(r13), bool>::value, "" ); |
| 63 | auto r14 = atomic_compare_exchange_strong(&a, &l, l); |
| 64 | static_assert( std::is_same<decltype(r14), bool>::value, "" ); |
| 65 | auto r15 = atomic_compare_exchange_weak_explicit(&v, &i, i, mo, mo); |
| 66 | static_assert( std::is_same<decltype(r15), bool>::value, "" ); |
| 67 | auto r16 = atomic_compare_exchange_weak_explicit(&a, &l, l, mo, mo); |
| 68 | static_assert( std::is_same<decltype(r16), bool>::value, "" ); |
| 69 | auto r17 = atomic_compare_exchange_strong_explicit(&v, &i, i, mo, mo); |
| 70 | static_assert( std::is_same<decltype(r17), bool>::value, "" ); |
| 71 | auto r18 = atomic_compare_exchange_strong_explicit(&a, &l, l, mo, mo); |
| 72 | static_assert( std::is_same<decltype(r18), bool>::value, "" ); |
| 73 | |
| 74 | auto r19 = atomic_fetch_add(&v, i); |
| 75 | static_assert( std::is_same<decltype(r19), int>::value, "" ); |
| 76 | auto r20 = atomic_fetch_add(&a, l); |
| 77 | static_assert( std::is_same<decltype(r20), long>::value, "" ); |
| 78 | auto r21 = atomic_fetch_add_explicit(&v, i, mo); |
| 79 | static_assert( std::is_same<decltype(r21), int>::value, "" ); |
| 80 | auto r22 = atomic_fetch_add_explicit(&a, l, mo); |
| 81 | static_assert( std::is_same<decltype(r22), long>::value, "" ); |
| 82 | auto r23 = atomic_fetch_sub(&v, i); |
| 83 | static_assert( std::is_same<decltype(r23), int>::value, "" ); |
| 84 | auto r24 = atomic_fetch_sub(&a, l); |
| 85 | static_assert( std::is_same<decltype(r24), long>::value, "" ); |
| 86 | auto r25 = atomic_fetch_sub_explicit(&v, i, mo); |
| 87 | static_assert( std::is_same<decltype(r25), int>::value, "" ); |
| 88 | auto r26 = atomic_fetch_sub_explicit(&a, l, mo); |
| 89 | static_assert( std::is_same<decltype(r26), long>::value, "" ); |
| 90 | auto r27 = atomic_fetch_and(&v, i); |
| 91 | static_assert( std::is_same<decltype(r27), int>::value, "" ); |
| 92 | auto r28 = atomic_fetch_and(&a, l); |
| 93 | static_assert( std::is_same<decltype(r28), long>::value, "" ); |
| 94 | auto r29 = atomic_fetch_and_explicit(&v, i, mo); |
| 95 | static_assert( std::is_same<decltype(r29), int>::value, "" ); |
| 96 | auto r30 = atomic_fetch_and_explicit(&a, l, mo); |
| 97 | static_assert( std::is_same<decltype(r30), long>::value, "" ); |
| 98 | auto r31 = atomic_fetch_or(&v, i); |
| 99 | static_assert( std::is_same<decltype(r31), int>::value, "" ); |
| 100 | auto r32 = atomic_fetch_or(&a, l); |
| 101 | static_assert( std::is_same<decltype(r32), long>::value, "" ); |
| 102 | auto r33 = atomic_fetch_or_explicit(&v, i, mo); |
| 103 | static_assert( std::is_same<decltype(r33), int>::value, "" ); |
| 104 | auto r34 = atomic_fetch_or_explicit(&a, l, mo); |
| 105 | static_assert( std::is_same<decltype(r34), long>::value, "" ); |
| 106 | auto r35 = atomic_fetch_xor(&v, i); |
| 107 | static_assert( std::is_same<decltype(r35), int>::value, "" ); |
| 108 | auto r36 = atomic_fetch_xor(&a, l); |
| 109 | static_assert( std::is_same<decltype(r36), long>::value, "" ); |
| 110 | auto r37 = atomic_fetch_xor_explicit(&v, i, mo); |
| 111 | static_assert( std::is_same<decltype(r37), int>::value, "" ); |
| 112 | auto r38 = atomic_fetch_xor_explicit(&a, l, mo); |
| 113 | static_assert( std::is_same<decltype(r38), long>::value, "" ); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | test02() |
| 118 | { |
| 119 | volatile std::atomic<long> v; |
| 120 | std::atomic<long> a; |
| 121 | std::memory_order mo = std::memory_order_seq_cst; |
| 122 | // Repeat tests with arguments of type different to value_type. |
| 123 | const int i = 0; |
| 124 | long l = 0; |
| 125 | atomic_init(&v, i); |
| 126 | atomic_init(&a, i); |
| 127 | atomic_store(&v, i); |
| 128 | atomic_store(&a, i); |
| 129 | atomic_store_explicit(&v, i, mo); |
| 130 | atomic_store_explicit(&a, i, mo); |
| 131 | atomic_exchange(&v, i); |
| 132 | atomic_exchange(&a, i); |
| 133 | atomic_exchange_explicit(&v, i, mo); |
| 134 | atomic_exchange_explicit(&a, i, mo); |
| 135 | atomic_compare_exchange_weak(&v, &l, i); |
| 136 | atomic_compare_exchange_weak(&a, &l, i); |
| 137 | atomic_compare_exchange_strong(&v, &l, i); |
| 138 | atomic_compare_exchange_strong(&a, &l, i); |
| 139 | atomic_compare_exchange_weak_explicit(&v, &l, i, mo, mo); |
| 140 | atomic_compare_exchange_weak_explicit(&a, &l, i, mo, mo); |
| 141 | atomic_compare_exchange_strong_explicit(&v, &l, i, mo, mo); |
| 142 | atomic_compare_exchange_strong_explicit(&a, &l, i, mo, mo); |
| 143 | atomic_fetch_add(&v, i); |
| 144 | atomic_fetch_add(&a, i); |
| 145 | atomic_fetch_add_explicit(&v, i, mo); |
| 146 | atomic_fetch_add_explicit(&a, i, mo); |
| 147 | atomic_fetch_sub(&v, i); |
| 148 | atomic_fetch_sub(&a, i); |
| 149 | atomic_fetch_sub_explicit(&v, i, mo); |
| 150 | atomic_fetch_sub_explicit(&a, i, mo); |
| 151 | atomic_fetch_and(&v, i); |
| 152 | atomic_fetch_and(&a, i); |
| 153 | atomic_fetch_and_explicit(&v, i, mo); |
| 154 | atomic_fetch_and_explicit(&a, i, mo); |
| 155 | atomic_fetch_or(&v, i); |
| 156 | atomic_fetch_or(&a, i); |
| 157 | atomic_fetch_or_explicit(&v, i, mo); |
| 158 | atomic_fetch_or_explicit(&a, i, mo); |
| 159 | atomic_fetch_xor(&v, i); |
| 160 | atomic_fetch_xor(&a, i); |
| 161 | atomic_fetch_xor_explicit(&v, i, mo); |
| 162 | atomic_fetch_xor_explicit(&a, i, mo); |
| 163 | } |