| 1 | // { dg-do compile { target i?86-*-* x86_64-*-* } } |
| 2 | // { dg-options "-O2 -g0 -fno-exceptions -fno-asynchronous-unwind-tables" } |
| 3 | // { dg-additional-options "-march=i486 -fcf-protection=none" { target ia32 } } |
| 4 | // { dg-require-effective-target c++11 } |
| 5 | // { dg-skip-if "scans fail with LTO" { lto } { "-flto" } } |
| 6 | // { dg-final { scan-assembler-times "xacquire\|\.byte\[^\n\r]*0xf2" 14 } } |
| 7 | // { dg-final { scan-assembler-times "xrelease\|\.byte\[^\n\r]*0xf3" 14 } } |
| 8 | |
| 9 | // Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 10 | // |
| 11 | // This file is part of the GNU ISO C++ Library. This library is free |
| 12 | // software; you can redistribute it and/or modify it under the |
| 13 | // terms of the GNU General Public License as published by the |
| 14 | // Free Software Foundation; either version 3, or (at your option) |
| 15 | // any later version. |
| 16 | |
| 17 | // This library is distributed in the hope that it will be useful, |
| 18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | // GNU General Public License for more details. |
| 21 | |
| 22 | // You should have received a copy of the GNU General Public License along |
| 23 | // with this library; see the file COPYING3. If not see |
| 24 | // <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | #include <atomic> |
| 27 | |
| 28 | const auto ACQ = std::memory_order_acquire | std::__memory_order_hle_acquire; |
| 29 | const auto REL = std::memory_order_release | std::__memory_order_hle_release; |
| 30 | |
| 31 | int main() |
| 32 | { |
| 33 | unsigned zero, one; |
| 34 | using namespace std; |
| 35 | atomic_flag af = ATOMIC_FLAG_INIT; |
| 36 | |
| 37 | if (!af.test_and_set(ACQ)) |
| 38 | af.clear(REL); |
| 39 | |
| 40 | atomic_uint au = ATOMIC_VAR_INIT(0); |
| 41 | |
| 42 | if (au.exchange(1, ACQ)) |
| 43 | au.store(0, REL); |
| 44 | |
| 45 | if (au.exchange(1, ACQ)) |
| 46 | au.exchange(0, REL); |
| 47 | |
| 48 | zero = 0; |
| 49 | one = 1; |
| 50 | if (au.compare_exchange_weak(zero, 1, ACQ, memory_order_consume)) |
| 51 | au.compare_exchange_weak(one, 0, REL, memory_order_consume); |
| 52 | |
| 53 | zero = 0; |
| 54 | one = 1; |
| 55 | if (au.compare_exchange_strong(zero, 1, ACQ, memory_order_consume)) |
| 56 | au.compare_exchange_strong(one, 0, REL, memory_order_consume); |
| 57 | |
| 58 | if (!au.fetch_add(1, ACQ)) |
| 59 | au.fetch_add(-1, REL); |
| 60 | |
| 61 | if (!au.fetch_sub(1, ACQ)) |
| 62 | au.fetch_sub(-1, REL); |
| 63 | |
| 64 | #if 0 /* broken in underlying target */ |
| 65 | if (!au.fetch_and(1, ACQ)) |
| 66 | au.fetch_and(-1, REL); |
| 67 | |
| 68 | if (!au.fetch_or(1, ACQ)) |
| 69 | au.fetch_or(-1, REL); |
| 70 | |
| 71 | if (!au.fetch_xor(1, ACQ)) |
| 72 | au.fetch_xor(-1, REL); |
| 73 | |
| 74 | if (!au.fetch_nand(1, ACQ)) |
| 75 | au.fetch_nand(-1, REL); |
| 76 | #endif |
| 77 | |
| 78 | volatile atomic_flag vaf = ATOMIC_FLAG_INIT; |
| 79 | |
| 80 | if (!vaf.test_and_set(ACQ)) |
| 81 | vaf.clear(REL); |
| 82 | |
| 83 | volatile atomic_uint vau = ATOMIC_VAR_INIT(0); |
| 84 | |
| 85 | if (!vau.exchange(1, ACQ)) |
| 86 | vau.store(0, REL); |
| 87 | |
| 88 | if (!vau.exchange(1, ACQ)) |
| 89 | vau.exchange(0, REL); |
| 90 | |
| 91 | zero = 0; |
| 92 | one = 1; |
| 93 | if (vau.compare_exchange_weak(zero, 1, ACQ, memory_order_consume)) |
| 94 | vau.compare_exchange_weak(one, 0, REL, memory_order_consume); |
| 95 | |
| 96 | zero = 0; |
| 97 | one = 1; |
| 98 | if (vau.compare_exchange_strong(zero, 1, ACQ, memory_order_consume)) |
| 99 | vau.compare_exchange_strong(one, 0, REL, memory_order_consume); |
| 100 | |
| 101 | if (!vau.fetch_add(1, ACQ)) |
| 102 | vau.fetch_add(-1, REL); |
| 103 | |
| 104 | if (!vau.fetch_sub(1, ACQ)) |
| 105 | vau.fetch_sub(-1, REL); |
| 106 | |
| 107 | #if 0 /* broken in underlying target */ |
| 108 | |
| 109 | if (!vau.fetch_and(1, ACQ)) |
| 110 | vau.fetch_and(-1, REL); |
| 111 | |
| 112 | if (!vau.fetch_or(1, ACQ)) |
| 113 | vau.fetch_or(-1, REL); |
| 114 | |
| 115 | if (!vau.fetch_xor(1, ACQ)) |
| 116 | vau.fetch_xor(-1, REL); |
| 117 | |
| 118 | if (!vau.fetch_nand(1, ACQ)) |
| 119 | vau.fetch_nand(-1, REL); |
| 120 | #endif |
| 121 | |
| 122 | return 0; |
| 123 | } |