Debian Codegraph @

libstdc++-v3/testsuite/29_atomics/atomic/nonmembers.cc

raw
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 struct X { };
27 struct Y { };
28 // Primary template
29 volatile std::atomic<X> v;
30 std::atomic<Y> a;
31 const std::memory_order mo = std::memory_order_seq_cst;
32 X x;
33 Y y;
34 auto r1 = atomic_is_lock_free(&v);
35 static_assert( std::is_same<decltype(r1), bool>::value, "" );
36 auto r2 = atomic_is_lock_free(&a);
37 static_assert( std::is_same<decltype(r2), bool>::value, "" );
38 atomic_init(&v, x);
39 atomic_init(&a, y);
40 atomic_store(&v, x);
41 atomic_store(&a, y);
42 atomic_store_explicit(&v, x, mo);
43 atomic_store_explicit(&a, y, mo);
44 auto r3 = atomic_load(&v);
45 static_assert( std::is_same<decltype(r3), X>::value, "" );
46 auto r4 = atomic_load(&a);
47 static_assert( std::is_same<decltype(r4), Y>::value, "" );
48 auto r5 = atomic_load_explicit(&v, mo);
49 static_assert( std::is_same<decltype(r5), X>::value, "" );
50 auto r6 = atomic_load_explicit(&a, mo);
51 static_assert( std::is_same<decltype(r6), Y>::value, "" );
52 auto r7 = atomic_exchange(&v, x);
53 static_assert( std::is_same<decltype(r7), X>::value, "" );
54 auto r8 = atomic_exchange(&a, y);
55 static_assert( std::is_same<decltype(r8), Y>::value, "" );
56 auto r9 = atomic_exchange_explicit(&v, x, mo);
57 static_assert( std::is_same<decltype(r9), X>::value, "" );
58 auto r10 = atomic_exchange_explicit(&a, y, mo);
59 static_assert( std::is_same<decltype(r10), Y>::value, "" );
60 auto r11 = atomic_compare_exchange_weak(&v, &x, x);
61 static_assert( std::is_same<decltype(r11), bool>::value, "" );
62 auto r12 = atomic_compare_exchange_weak(&a, &y, y);
63 static_assert( std::is_same<decltype(r12), bool>::value, "" );
64 auto r13 = atomic_compare_exchange_strong(&v, &x, x);
65 static_assert( std::is_same<decltype(r13), bool>::value, "" );
66 auto r14 = atomic_compare_exchange_strong(&a, &y, y);
67 static_assert( std::is_same<decltype(r14), bool>::value, "" );
68 auto r15 = atomic_compare_exchange_weak_explicit(&v, &x, x, mo, mo);
69 static_assert( std::is_same<decltype(r15), bool>::value, "" );
70 auto r16 = atomic_compare_exchange_weak_explicit(&a, &y, y, mo, mo);
71 static_assert( std::is_same<decltype(r16), bool>::value, "" );
72 auto r17 = atomic_compare_exchange_strong_explicit(&v, &x, x, mo, mo);
73 static_assert( std::is_same<decltype(r17), bool>::value, "" );
74 auto r18 = atomic_compare_exchange_strong_explicit(&a, &y, y, mo, mo);
75 static_assert( std::is_same<decltype(r18), bool>::value, "" );
76 }
77
78 void
79 test02()
80 {
81 // Specialization for bool
82 volatile std::atomic<bool> v;
83 std::atomic<bool> a;
84 const std::memory_order mo = std::memory_order_seq_cst;
85 bool b = false;
86 auto r1 = atomic_is_lock_free(&v);
87 static_assert( std::is_same<decltype(r1), bool>::value, "" );
88 auto r2 = atomic_is_lock_free(&a);
89 static_assert( std::is_same<decltype(r2), bool>::value, "" );
90 atomic_init(&v, b);
91 atomic_init(&a, b);
92 atomic_store(&v, b);
93 atomic_store(&a, b);
94 atomic_store_explicit(&v, b, mo);
95 atomic_store_explicit(&a, b, mo);
96 auto r3 = atomic_load(&v);
97 static_assert( std::is_same<decltype(r3), bool>::value, "" );
98 auto r4 = atomic_load(&a);
99 static_assert( std::is_same<decltype(r4), bool>::value, "" );
100 auto r5 = atomic_load_explicit(&v, mo);
101 static_assert( std::is_same<decltype(r5), bool>::value, "" );
102 auto r6 = atomic_load_explicit(&a, mo);
103 static_assert( std::is_same<decltype(r6), bool>::value, "" );
104 auto r7 = atomic_exchange(&v, b);
105 static_assert( std::is_same<decltype(r7), bool>::value, "" );
106 auto r8 = atomic_exchange(&a, b);
107 static_assert( std::is_same<decltype(r8), bool>::value, "" );
108 auto r9 = atomic_exchange_explicit(&v, b, mo);
109 static_assert( std::is_same<decltype(r9), bool>::value, "" );
110 auto r10 = atomic_exchange_explicit(&a, b, mo);
111 static_assert( std::is_same<decltype(r10), bool>::value, "" );
112 auto r11 = atomic_compare_exchange_weak(&v, &b, b);
113 static_assert( std::is_same<decltype(r11), bool>::value, "" );
114 auto r12 = atomic_compare_exchange_weak(&a, &b, b);
115 static_assert( std::is_same<decltype(r12), bool>::value, "" );
116 auto r13 = atomic_compare_exchange_strong(&v, &b, b);
117 static_assert( std::is_same<decltype(r13), bool>::value, "" );
118 auto r14 = atomic_compare_exchange_strong(&a, &b, b);
119 static_assert( std::is_same<decltype(r14), bool>::value, "" );
120 auto r15 = atomic_compare_exchange_weak_explicit(&v, &b, b, mo, mo);
121 static_assert( std::is_same<decltype(r15), bool>::value, "" );
122 auto r16 = atomic_compare_exchange_weak_explicit(&a, &b, b, mo, mo);
123 static_assert( std::is_same<decltype(r16), bool>::value, "" );
124 auto r17 = atomic_compare_exchange_strong_explicit(&v, &b, b, mo, mo);
125 static_assert( std::is_same<decltype(r17), bool>::value, "" );
126 auto r18 = atomic_compare_exchange_strong_explicit(&a, &b, b, mo, mo);
127 static_assert( std::is_same<decltype(r18), bool>::value, "" );
128 }
129
130 void
131 test03()
132 {
133 // Partial specialization for pointers
134 volatile std::atomic<int*> v;
135 std::atomic<long*> a;
136 const std::memory_order mo = std::memory_order_seq_cst;
137 int* i = nullptr;
138 long* l = nullptr;
139 auto r1 = atomic_is_lock_free(&v);
140 static_assert( std::is_same<decltype(r1), bool>::value, "" );
141 auto r2 = atomic_is_lock_free(&a);
142 static_assert( std::is_same<decltype(r2), bool>::value, "" );
143 atomic_init(&v, i);
144 atomic_init(&a, l);
145 atomic_store(&v, i);
146 atomic_store(&a, l);
147 atomic_store_explicit(&v, i, mo);
148 atomic_store_explicit(&a, l, mo);
149 auto r3 = atomic_load(&v);
150 static_assert( std::is_same<decltype(r3), int*>::value, "" );
151 auto r4 = atomic_load(&a);
152 static_assert( std::is_same<decltype(r4), long*>::value, "" );
153 auto r5 = atomic_load_explicit(&v, mo);
154 static_assert( std::is_same<decltype(r5), int*>::value, "" );
155 auto r6 = atomic_load_explicit(&a, mo);
156 static_assert( std::is_same<decltype(r6), long*>::value, "" );
157 auto r7 = atomic_exchange(&v, i);
158 static_assert( std::is_same<decltype(r7), int*>::value, "" );
159 auto r8 = atomic_exchange(&a, l);
160 static_assert( std::is_same<decltype(r8), long*>::value, "" );
161 auto r9 = atomic_exchange_explicit(&v, i, mo);
162 static_assert( std::is_same<decltype(r9), int*>::value, "" );
163 auto r10 = atomic_exchange_explicit(&a, l, mo);
164 static_assert( std::is_same<decltype(r10), long*>::value, "" );
165 auto r11 = atomic_compare_exchange_weak(&v, &i, i);
166 static_assert( std::is_same<decltype(r11), bool>::value, "" );
167 auto r12 = atomic_compare_exchange_weak(&a, &l, l);
168 static_assert( std::is_same<decltype(r12), bool>::value, "" );
169 auto r13 = atomic_compare_exchange_strong(&v, &i, i);
170 static_assert( std::is_same<decltype(r13), bool>::value, "" );
171 auto r14 = atomic_compare_exchange_strong(&a, &l, l);
172 static_assert( std::is_same<decltype(r14), bool>::value, "" );
173 auto r15 = atomic_compare_exchange_weak_explicit(&v, &i, i, mo, mo);
174 static_assert( std::is_same<decltype(r15), bool>::value, "" );
175 auto r16 = atomic_compare_exchange_weak_explicit(&a, &l, l, mo, mo);
176 static_assert( std::is_same<decltype(r16), bool>::value, "" );
177 auto r17 = atomic_compare_exchange_strong_explicit(&v, &i, i, mo, mo);
178 static_assert( std::is_same<decltype(r17), bool>::value, "" );
179 auto r18 = atomic_compare_exchange_strong_explicit(&a, &l, l, mo, mo);
180 static_assert( std::is_same<decltype(r18), bool>::value, "" );
181
182 auto r19 = atomic_fetch_add(&v, 1);
183 static_assert( std::is_same<decltype(r19), int*>::value, "" );
184 auto r20 = atomic_fetch_add(&a, 1);
185 static_assert( std::is_same<decltype(r20), long*>::value, "" );
186 auto r21 = atomic_fetch_add_explicit(&v, 1, mo);
187 static_assert( std::is_same<decltype(r21), int*>::value, "" );
188 auto r22 = atomic_fetch_add_explicit(&a, 1, mo);
189 static_assert( std::is_same<decltype(r22), long*>::value, "" );
190 auto r23 = atomic_fetch_sub(&v, 1);
191 static_assert( std::is_same<decltype(r23), int*>::value, "" );
192 auto r24 = atomic_fetch_sub(&a, 1);
193 static_assert( std::is_same<decltype(r24), long*>::value, "" );
194 auto r25 = atomic_fetch_sub_explicit(&v, 1, mo);
195 static_assert( std::is_same<decltype(r25), int*>::value, "" );
196 auto r26 = atomic_fetch_sub_explicit(&a, 1, mo);
197 static_assert( std::is_same<decltype(r26), long*>::value, "" );
198 }
199
200 void
201 test04()
202 {
203 struct base { };
204 struct derived : base { };
205 // Partial specialization for pointers
206 volatile std::atomic<base*> v;
207 std::atomic<base*> a;
208 const std::memory_order mo = std::memory_order_seq_cst;
209 // Repeat tests with arguments of type different to value_type.
210 derived* const p = nullptr;
211 base* b = nullptr;
212 atomic_init(&v, p);
213 atomic_init(&a, p);
214 atomic_store(&v, p);
215 atomic_store(&a, p);
216 atomic_store_explicit(&v, p, mo);
217 atomic_store_explicit(&a, p, mo);
218 atomic_exchange(&v, p);
219 atomic_exchange(&a, p);
220 atomic_exchange_explicit(&v, p, mo);
221 atomic_exchange_explicit(&a, p, mo);
222 atomic_compare_exchange_weak(&v, &b, p);
223 atomic_compare_exchange_weak(&a, &b, p);
224 atomic_compare_exchange_strong(&v, &b, p);
225 atomic_compare_exchange_strong(&a, &b, p);
226 atomic_compare_exchange_weak_explicit(&v, &b, p, mo, mo);
227 atomic_compare_exchange_weak_explicit(&a, &b, p, mo, mo);
228 atomic_compare_exchange_strong_explicit(&v, &b, p, mo, mo);
229 atomic_compare_exchange_strong_explicit(&a, &b, p, mo, mo);
230 }