xref: /llvm-project/libcxx/test/std/atomics/atomics.flag/test_and_set.pass.cpp (revision 92832e4889ae6038cc8b3e6e449af2a9b9374ab4)
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 test_and_set(memory_order = memory_order_seq_cst);
14 // bool test_and_set(memory_order = memory_order_seq_cst) volatile;
15 
16 #include <atomic>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
main(int,char **)21 int main(int, char**)
22 {
23     {
24         std::atomic_flag f;
25         f.clear();
26         assert(f.test_and_set() == 0);
27         assert(f.test_and_set() == 1);
28     }
29     {
30         std::atomic_flag f;
31         f.clear();
32         assert(f.test_and_set(std::memory_order_relaxed) == 0);
33         assert(f.test_and_set(std::memory_order_relaxed) == 1);
34     }
35     {
36         std::atomic_flag f;
37         f.clear();
38         assert(f.test_and_set(std::memory_order_consume) == 0);
39         assert(f.test_and_set(std::memory_order_consume) == 1);
40     }
41     {
42         std::atomic_flag f;
43         f.clear();
44         assert(f.test_and_set(std::memory_order_acquire) == 0);
45         assert(f.test_and_set(std::memory_order_acquire) == 1);
46     }
47     {
48         std::atomic_flag f;
49         f.clear();
50         assert(f.test_and_set(std::memory_order_release) == 0);
51         assert(f.test_and_set(std::memory_order_release) == 1);
52     }
53     {
54         std::atomic_flag f;
55         f.clear();
56         assert(f.test_and_set(std::memory_order_acq_rel) == 0);
57         assert(f.test_and_set(std::memory_order_acq_rel) == 1);
58     }
59     {
60         std::atomic_flag f;
61         f.clear();
62         assert(f.test_and_set(std::memory_order_seq_cst) == 0);
63         assert(f.test_and_set(std::memory_order_seq_cst) == 1);
64     }
65     {
66         volatile std::atomic_flag f;
67         f.clear();
68         assert(f.test_and_set() == 0);
69         assert(f.test_and_set() == 1);
70     }
71     {
72         volatile std::atomic_flag f;
73         f.clear();
74         assert(f.test_and_set(std::memory_order_relaxed) == 0);
75         assert(f.test_and_set(std::memory_order_relaxed) == 1);
76     }
77     {
78         volatile std::atomic_flag f;
79         f.clear();
80         assert(f.test_and_set(std::memory_order_consume) == 0);
81         assert(f.test_and_set(std::memory_order_consume) == 1);
82     }
83     {
84         volatile std::atomic_flag f;
85         f.clear();
86         assert(f.test_and_set(std::memory_order_acquire) == 0);
87         assert(f.test_and_set(std::memory_order_acquire) == 1);
88     }
89     {
90         volatile std::atomic_flag f;
91         f.clear();
92         assert(f.test_and_set(std::memory_order_release) == 0);
93         assert(f.test_and_set(std::memory_order_release) == 1);
94     }
95     {
96         volatile std::atomic_flag f;
97         f.clear();
98         assert(f.test_and_set(std::memory_order_acq_rel) == 0);
99         assert(f.test_and_set(std::memory_order_acq_rel) == 1);
100     }
101     {
102         volatile std::atomic_flag f;
103         f.clear();
104         assert(f.test_and_set(std::memory_order_seq_cst) == 0);
105         assert(f.test_and_set(std::memory_order_seq_cst) == 1);
106     }
107 
108   return 0;
109 }
110