| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <atomic> |
| 10 | |
| 11 | // struct atomic_flag |
| 12 | |
| 13 | // bool atomic_flag_test_explicit(volatile atomic_flag*, memory_order); |
| 14 | // bool atomic_flag_test_explicit(atomic_flag*, memory_order); |
| 15 | |
| 16 | #include <atomic> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | |
| 21 | int main(int, char**) |
| 22 | { |
| 23 | { |
| 24 | std::atomic_flag f; |
| 25 | f.clear(); |
| 26 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_relaxed) == 0); |
| 27 | assert(f.test_and_set() == 0); |
| 28 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_relaxed) == 1); |
| 29 | } |
| 30 | { |
| 31 | std::atomic_flag f; |
| 32 | f.clear(); |
| 33 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_consume) == 0); |
| 34 | assert(f.test_and_set() == 0); |
| 35 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_consume) == 1); |
| 36 | } |
| 37 | { |
| 38 | std::atomic_flag f; |
| 39 | f.clear(); |
| 40 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acquire) == 0); |
| 41 | assert(f.test_and_set() == 0); |
| 42 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acquire) == 1); |
| 43 | } |
| 44 | #ifdef _LIBCPP_VERSION // Don't violate precondition [atomics.flag]/6 |
| 45 | { |
| 46 | std::atomic_flag f; |
| 47 | f.clear(); |
| 48 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_release) == 0); |
| 49 | assert(f.test_and_set() == 0); |
| 50 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_release) == 1); |
| 51 | } |
| 52 | { |
| 53 | std::atomic_flag f; |
| 54 | f.clear(); |
| 55 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acq_rel) == 0); |
| 56 | assert(f.test_and_set() == 0); |
| 57 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acq_rel) == 1); |
| 58 | } |
| 59 | #endif // _LIBCPP_VERSION |
| 60 | { |
| 61 | std::atomic_flag f; |
| 62 | f.clear(); |
| 63 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_seq_cst) == 0); |
| 64 | assert(f.test_and_set() == 0); |
| 65 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_seq_cst) == 1); |
| 66 | } |
| 67 | { |
| 68 | volatile std::atomic_flag f; |
| 69 | f.clear(); |
| 70 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_relaxed) == 0); |
| 71 | assert(f.test_and_set() == 0); |
| 72 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_relaxed) == 1); |
| 73 | } |
| 74 | { |
| 75 | volatile std::atomic_flag f; |
| 76 | f.clear(); |
| 77 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_consume) == 0); |
| 78 | assert(f.test_and_set() == 0); |
| 79 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_consume) == 1); |
| 80 | } |
| 81 | { |
| 82 | volatile std::atomic_flag f; |
| 83 | f.clear(); |
| 84 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acquire) == 0); |
| 85 | assert(f.test_and_set() == 0); |
| 86 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acquire) == 1); |
| 87 | } |
| 88 | #ifdef _LIBCPP_VERSION // Don't violate precondition [atomics.flag]/6 |
| 89 | { |
| 90 | volatile std::atomic_flag f; |
| 91 | f.clear(); |
| 92 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_release) == 0); |
| 93 | assert(f.test_and_set() == 0); |
| 94 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_release) == 1); |
| 95 | } |
| 96 | { |
| 97 | volatile std::atomic_flag f; |
| 98 | f.clear(); |
| 99 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acq_rel) == 0); |
| 100 | assert(f.test_and_set() == 0); |
| 101 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_acq_rel) == 1); |
| 102 | } |
| 103 | #endif // _LIBCPP_VERSION |
| 104 | { |
| 105 | volatile std::atomic_flag f; |
| 106 | f.clear(); |
| 107 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_seq_cst) == 0); |
| 108 | assert(f.test_and_set() == 0); |
| 109 | assert(std::atomic_flag_test_explicit(&f, std::memory_order_seq_cst) == 1); |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |