blob: eaff5368e02058150b8cc5a313e6bdcdb2ee182c [file] [log] [blame]
Howard Hinnant71be7292010-09-27 21:17:38 +00001// -*- C++ -*-
2//===--------------------------- atomic -----------------------------------===//
3//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant71be7292010-09-27 21:17:38 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ATOMIC
11#define _LIBCPP_ATOMIC
12
13/*
14 atomic synopsis
15
16namespace std
17{
18
Olivier Girouxdf5bfa32020-09-09 10:00:09 -070019// feature test macro [version.syn]
JF Bastienfdb42c22016-03-25 15:48:21 +000020
Olivier Girouxdf5bfa32020-09-09 10:00:09 -070021#define __cpp_lib_atomic_is_always_lock_free
22#define __cpp_lib_atomic_flag_test
23#define __cpp_lib_atomic_lock_free_type_aliases
24#define __cpp_lib_atomic_wait
JF Bastienfdb42c22016-03-25 15:48:21 +000025
Davide Italiano011f80a2019-03-05 18:40:49 +000026 // order and consistency
Howard Hinnant71be7292010-09-27 21:17:38 +000027
Davide Italiano011f80a2019-03-05 18:40:49 +000028 enum memory_order: unspecified // enum class in C++20
29 {
30 relaxed,
31 consume, // load-consume
32 acquire, // load-acquire
33 release, // store-release
34 acq_rel, // store-release load-acquire
35 seq_cst // store-release load-acquire
36 };
37
38 inline constexpr auto memory_order_relaxed = memory_order::relaxed;
39 inline constexpr auto memory_order_consume = memory_order::consume;
40 inline constexpr auto memory_order_acquire = memory_order::acquire;
41 inline constexpr auto memory_order_release = memory_order::release;
42 inline constexpr auto memory_order_acq_rel = memory_order::acq_rel;
43 inline constexpr auto memory_order_seq_cst = memory_order::seq_cst;
Howard Hinnant71be7292010-09-27 21:17:38 +000044
Howard Hinnanteee2c142012-04-11 20:14:21 +000045template <class T> T kill_dependency(T y) noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +000046
47// lock-free property
48
Howard Hinnant931e3402013-01-21 20:39:41 +000049#define ATOMIC_BOOL_LOCK_FREE unspecified
Howard Hinnant71be7292010-09-27 21:17:38 +000050#define ATOMIC_CHAR_LOCK_FREE unspecified
Marek Kurdej91bebaa2020-11-24 21:07:06 +010051#define ATOMIC_CHAR8_T_LOCK_FREE unspecified // C++20
Howard Hinnant71be7292010-09-27 21:17:38 +000052#define ATOMIC_CHAR16_T_LOCK_FREE unspecified
53#define ATOMIC_CHAR32_T_LOCK_FREE unspecified
54#define ATOMIC_WCHAR_T_LOCK_FREE unspecified
55#define ATOMIC_SHORT_LOCK_FREE unspecified
56#define ATOMIC_INT_LOCK_FREE unspecified
57#define ATOMIC_LONG_LOCK_FREE unspecified
58#define ATOMIC_LLONG_LOCK_FREE unspecified
Howard Hinnant931e3402013-01-21 20:39:41 +000059#define ATOMIC_POINTER_LOCK_FREE unspecified
Howard Hinnant71be7292010-09-27 21:17:38 +000060
Howard Hinnant71be7292010-09-27 21:17:38 +000061template <class T>
62struct atomic
63{
Olivier Giroux6031a712020-06-01 14:30:13 -070064 using value_type = T;
65
JF Bastienfdb42c22016-03-25 15:48:21 +000066 static constexpr bool is_always_lock_free;
Howard Hinnanteee2c142012-04-11 20:14:21 +000067 bool is_lock_free() const volatile noexcept;
68 bool is_lock_free() const noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -070069
70 atomic() noexcept = default;
71 constexpr atomic(T desr) noexcept;
72 atomic(const atomic&) = delete;
73 atomic& operator=(const atomic&) = delete;
74 atomic& operator=(const atomic&) volatile = delete;
75
Howard Hinnanteee2c142012-04-11 20:14:21 +000076 T load(memory_order m = memory_order_seq_cst) const volatile noexcept;
77 T load(memory_order m = memory_order_seq_cst) const noexcept;
78 operator T() const volatile noexcept;
79 operator T() const noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -070080 void store(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
81 void store(T desr, memory_order m = memory_order_seq_cst) noexcept;
82 T operator=(T) volatile noexcept;
83 T operator=(T) noexcept;
84
Howard Hinnanteee2c142012-04-11 20:14:21 +000085 T exchange(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
86 T exchange(T desr, memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +000087 bool compare_exchange_weak(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +000088 memory_order s, memory_order f) volatile noexcept;
89 bool compare_exchange_weak(T& expc, T desr, memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +000090 bool compare_exchange_strong(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +000091 memory_order s, memory_order f) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +000092 bool compare_exchange_strong(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +000093 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +000094 bool compare_exchange_weak(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +000095 memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +000096 bool compare_exchange_weak(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +000097 memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +000098 bool compare_exchange_strong(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +000099 memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000100 bool compare_exchange_strong(T& expc, T desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000101 memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000102
Olivier Giroux6031a712020-06-01 14:30:13 -0700103 void wait(T, memory_order = memory_order::seq_cst) const volatile noexcept;
104 void wait(T, memory_order = memory_order::seq_cst) const noexcept;
105 void notify_one() volatile noexcept;
106 void notify_one() noexcept;
107 void notify_all() volatile noexcept;
108 void notify_all() noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000109};
110
111template <>
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000112struct atomic<integral>
Howard Hinnant71be7292010-09-27 21:17:38 +0000113{
Olivier Giroux6031a712020-06-01 14:30:13 -0700114 using value_type = integral;
Olivier Girouxdf5bfa32020-09-09 10:00:09 -0700115 using difference_type = value_type;
Olivier Giroux6031a712020-06-01 14:30:13 -0700116
JF Bastienfdb42c22016-03-25 15:48:21 +0000117 static constexpr bool is_always_lock_free;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000118 bool is_lock_free() const volatile noexcept;
119 bool is_lock_free() const noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700120
121 atomic() noexcept = default;
122 constexpr atomic(integral desr) noexcept;
123 atomic(const atomic&) = delete;
124 atomic& operator=(const atomic&) = delete;
125 atomic& operator=(const atomic&) volatile = delete;
126
Howard Hinnanteee2c142012-04-11 20:14:21 +0000127 integral load(memory_order m = memory_order_seq_cst) const volatile noexcept;
128 integral load(memory_order m = memory_order_seq_cst) const noexcept;
129 operator integral() const volatile noexcept;
130 operator integral() const noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700131 void store(integral desr, memory_order m = memory_order_seq_cst) volatile noexcept;
132 void store(integral desr, memory_order m = memory_order_seq_cst) noexcept;
133 integral operator=(integral desr) volatile noexcept;
134 integral operator=(integral desr) noexcept;
135
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000136 integral exchange(integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000137 memory_order m = memory_order_seq_cst) volatile noexcept;
138 integral exchange(integral desr, memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000139 bool compare_exchange_weak(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000140 memory_order s, memory_order f) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000141 bool compare_exchange_weak(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000142 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000143 bool compare_exchange_strong(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000144 memory_order s, memory_order f) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000145 bool compare_exchange_strong(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000146 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000147 bool compare_exchange_weak(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000148 memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000149 bool compare_exchange_weak(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000150 memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000151 bool compare_exchange_strong(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000152 memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000153 bool compare_exchange_strong(integral& expc, integral desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000154 memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000155
Olivier Giroux6031a712020-06-01 14:30:13 -0700156 integral fetch_add(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000157 integral fetch_add(integral op, memory_order m = memory_order_seq_cst) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700158 integral fetch_sub(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000159 integral fetch_sub(integral op, memory_order m = memory_order_seq_cst) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700160 integral fetch_and(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000161 integral fetch_and(integral op, memory_order m = memory_order_seq_cst) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700162 integral fetch_or(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000163 integral fetch_or(integral op, memory_order m = memory_order_seq_cst) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700164 integral fetch_xor(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000165 integral fetch_xor(integral op, memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000166
Howard Hinnanteee2c142012-04-11 20:14:21 +0000167 integral operator++(int) volatile noexcept;
168 integral operator++(int) noexcept;
169 integral operator--(int) volatile noexcept;
170 integral operator--(int) noexcept;
171 integral operator++() volatile noexcept;
172 integral operator++() noexcept;
173 integral operator--() volatile noexcept;
174 integral operator--() noexcept;
175 integral operator+=(integral op) volatile noexcept;
176 integral operator+=(integral op) noexcept;
177 integral operator-=(integral op) volatile noexcept;
178 integral operator-=(integral op) noexcept;
179 integral operator&=(integral op) volatile noexcept;
180 integral operator&=(integral op) noexcept;
181 integral operator|=(integral op) volatile noexcept;
182 integral operator|=(integral op) noexcept;
183 integral operator^=(integral op) volatile noexcept;
184 integral operator^=(integral op) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700185
186 void wait(integral, memory_order = memory_order::seq_cst) const volatile noexcept;
187 void wait(integral, memory_order = memory_order::seq_cst) const noexcept;
188 void notify_one() volatile noexcept;
189 void notify_one() noexcept;
190 void notify_all() volatile noexcept;
191 void notify_all() noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000192};
193
194template <class T>
195struct atomic<T*>
Howard Hinnant71be7292010-09-27 21:17:38 +0000196{
Olivier Giroux6031a712020-06-01 14:30:13 -0700197 using value_type = T*;
Olivier Girouxdf5bfa32020-09-09 10:00:09 -0700198 using difference_type = ptrdiff_t;
Olivier Giroux6031a712020-06-01 14:30:13 -0700199
JF Bastienfdb42c22016-03-25 15:48:21 +0000200 static constexpr bool is_always_lock_free;
Howard Hinnanteee2c142012-04-11 20:14:21 +0000201 bool is_lock_free() const volatile noexcept;
202 bool is_lock_free() const noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700203
204 atomic() noexcept = default;
205 constexpr atomic(T* desr) noexcept;
206 atomic(const atomic&) = delete;
207 atomic& operator=(const atomic&) = delete;
208 atomic& operator=(const atomic&) volatile = delete;
209
Howard Hinnanteee2c142012-04-11 20:14:21 +0000210 T* load(memory_order m = memory_order_seq_cst) const volatile noexcept;
211 T* load(memory_order m = memory_order_seq_cst) const noexcept;
212 operator T*() const volatile noexcept;
213 operator T*() const noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700214 void store(T* desr, memory_order m = memory_order_seq_cst) volatile noexcept;
215 void store(T* desr, memory_order m = memory_order_seq_cst) noexcept;
216 T* operator=(T*) volatile noexcept;
217 T* operator=(T*) noexcept;
218
Howard Hinnanteee2c142012-04-11 20:14:21 +0000219 T* exchange(T* desr, memory_order m = memory_order_seq_cst) volatile noexcept;
220 T* exchange(T* desr, memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000221 bool compare_exchange_weak(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000222 memory_order s, memory_order f) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000223 bool compare_exchange_weak(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000224 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000225 bool compare_exchange_strong(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000226 memory_order s, memory_order f) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000227 bool compare_exchange_strong(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000228 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000229 bool compare_exchange_weak(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000230 memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000231 bool compare_exchange_weak(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000232 memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000233 bool compare_exchange_strong(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000234 memory_order m = memory_order_seq_cst) volatile noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000235 bool compare_exchange_strong(T*& expc, T* desr,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000236 memory_order m = memory_order_seq_cst) noexcept;
237 T* fetch_add(ptrdiff_t op, memory_order m = memory_order_seq_cst) volatile noexcept;
238 T* fetch_add(ptrdiff_t op, memory_order m = memory_order_seq_cst) noexcept;
239 T* fetch_sub(ptrdiff_t op, memory_order m = memory_order_seq_cst) volatile noexcept;
240 T* fetch_sub(ptrdiff_t op, memory_order m = memory_order_seq_cst) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000241
Howard Hinnanteee2c142012-04-11 20:14:21 +0000242 T* operator++(int) volatile noexcept;
243 T* operator++(int) noexcept;
244 T* operator--(int) volatile noexcept;
245 T* operator--(int) noexcept;
246 T* operator++() volatile noexcept;
247 T* operator++() noexcept;
248 T* operator--() volatile noexcept;
249 T* operator--() noexcept;
250 T* operator+=(ptrdiff_t op) volatile noexcept;
251 T* operator+=(ptrdiff_t op) noexcept;
252 T* operator-=(ptrdiff_t op) volatile noexcept;
253 T* operator-=(ptrdiff_t op) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700254
255 void wait(T*, memory_order = memory_order::seq_cst) const volatile noexcept;
256 void wait(T*, memory_order = memory_order::seq_cst) const noexcept;
257 void notify_one() volatile noexcept;
258 void notify_one() noexcept;
259 void notify_all() volatile noexcept;
260 void notify_all() noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000261};
262
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000263
264template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700265 bool atomic_is_lock_free(const volatile atomic<T>* obj) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000266
267template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700268 bool atomic_is_lock_free(const atomic<T>* obj) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000269
270template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700271 void atomic_store(volatile atomic<T>* obj, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000272
273template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700274 void atomic_store(atomic<T>* obj, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000275
276template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700277 void atomic_store_explicit(volatile atomic<T>* obj, T desr, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000278
279template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700280 void atomic_store_explicit(atomic<T>* obj, T desr, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000281
282template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700283 T atomic_load(const volatile atomic<T>* obj) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000284
285template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700286 T atomic_load(const atomic<T>* obj) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000287
288template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700289 T atomic_load_explicit(const volatile atomic<T>* obj, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000290
291template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700292 T atomic_load_explicit(const atomic<T>* obj, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000293
294template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700295 T atomic_exchange(volatile atomic<T>* obj, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000296
297template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700298 T atomic_exchange(atomic<T>* obj, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000299
300template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700301 T atomic_exchange_explicit(volatile atomic<T>* obj, T desr, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000302
303template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700304 T atomic_exchange_explicit(atomic<T>* obj, T desr, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000305
306template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700307 bool atomic_compare_exchange_weak(volatile atomic<T>* obj, T* expc, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000308
309template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700310 bool atomic_compare_exchange_weak(atomic<T>* obj, T* expc, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000311
312template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700313 bool atomic_compare_exchange_strong(volatile atomic<T>* obj, T* expc, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000314
315template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700316 bool atomic_compare_exchange_strong(atomic<T>* obj, T* expc, T desr) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000317
318template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700319 bool atomic_compare_exchange_weak_explicit(volatile atomic<T>* obj, T* expc,
320 T desr,
321 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000322
323template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700324 bool atomic_compare_exchange_weak_explicit(atomic<T>* obj, T* expc, T desr,
325 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000326
327template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700328 bool atomic_compare_exchange_strong_explicit(volatile atomic<T>* obj,
329 T* expc, T desr,
330 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000331
332template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700333 bool atomic_compare_exchange_strong_explicit(atomic<T>* obj, T* expc,
334 T desr,
335 memory_order s, memory_order f) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000336
337template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700338 void atomic_wait(const volatile atomic<T>* obj, T old) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000339
340template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700341 void atomic_wait(const atomic<T>* obj, T old) noexcept;
342
343template <class T>
344 void atomic_wait_explicit(const volatile atomic<T>* obj, T old, memory_order m) noexcept;
345
346template <class T>
347 void atomic_wait_explicit(const atomic<T>* obj, T old, memory_order m) noexcept;
348
349template <class T>
350 void atomic_one(volatile atomic<T>* obj) noexcept;
351
352template <class T>
353 void atomic_one(atomic<T>* obj) noexcept;
354
355template <class T>
356 void atomic_all(volatile atomic<T>* obj) noexcept;
357
358template <class T>
359 void atomic_all(atomic<T>* obj) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000360
361template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700362 Integral atomic_fetch_add(volatile atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000363
364template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700365 Integral atomic_fetch_add(atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000366
367template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700368 Integral atomic_fetch_add_explicit(volatile atomic<Integral>* obj, Integral op,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000369 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000370template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700371 Integral atomic_fetch_add_explicit(atomic<Integral>* obj, Integral op,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000372 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000373template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700374 Integral atomic_fetch_sub(volatile atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000375
376template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700377 Integral atomic_fetch_sub(atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000378
379template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700380 Integral atomic_fetch_sub_explicit(volatile atomic<Integral>* obj, Integral op,
381 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000382
383template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700384 Integral atomic_fetch_sub_explicit(atomic<Integral>* obj, Integral op,
385 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000386
387template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700388 Integral atomic_fetch_and(volatile atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000389
390template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700391 Integral atomic_fetch_and(atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000392
393template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700394 Integral atomic_fetch_and_explicit(volatile atomic<Integral>* obj, Integral op,
395 memory_order m) noexcept;
396
397template <class Integral>
398 Integral atomic_fetch_and_explicit(atomic<Integral>* obj, Integral op,
399 memory_order m) noexcept;
400
401template <class Integral>
402 Integral atomic_fetch_or(volatile atomic<Integral>* obj, Integral op) noexcept;
403
404template <class Integral>
405 Integral atomic_fetch_or(atomic<Integral>* obj, Integral op) noexcept;
406
407template <class Integral>
408 Integral atomic_fetch_or_explicit(volatile atomic<Integral>* obj, Integral op,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000409 memory_order m) noexcept;
Olivier Giroux6031a712020-06-01 14:30:13 -0700410
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000411template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700412 Integral atomic_fetch_or_explicit(atomic<Integral>* obj, Integral op,
Howard Hinnanteee2c142012-04-11 20:14:21 +0000413 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000414
415template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700416 Integral atomic_fetch_xor(volatile atomic<Integral>* obj, Integral op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000417
418template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700419 Integral atomic_fetch_xor(atomic<Integral>* obj, Integral op) noexcept;
420
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000421template <class Integral>
Olivier Giroux6031a712020-06-01 14:30:13 -0700422 Integral atomic_fetch_xor_explicit(volatile atomic<Integral>* obj, Integral op,
423 memory_order m) noexcept;
424
425template <class Integral>
426 Integral atomic_fetch_xor_explicit(atomic<Integral>* obj, Integral op,
427 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000428
429template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700430 T* atomic_fetch_add(volatile atomic<T*>* obj, ptrdiff_t op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000431
432template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700433 T* atomic_fetch_add(atomic<T*>* obj, ptrdiff_t op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000434
435template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700436 T* atomic_fetch_add_explicit(volatile atomic<T*>* obj, ptrdiff_t op,
437 memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000438
439template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700440 T* atomic_fetch_add_explicit(atomic<T*>* obj, ptrdiff_t op, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000441
442template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700443 T* atomic_fetch_sub(volatile atomic<T*>* obj, ptrdiff_t op) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000444
445template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700446 T* atomic_fetch_sub(atomic<T*>* obj, ptrdiff_t op) noexcept;
447
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000448template <class T>
Olivier Giroux6031a712020-06-01 14:30:13 -0700449 T* atomic_fetch_sub_explicit(volatile atomic<T*>* obj, ptrdiff_t op,
450 memory_order m) noexcept;
451
452template <class T>
453 T* atomic_fetch_sub_explicit(atomic<T*>* obj, ptrdiff_t op, memory_order m) noexcept;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000454
455// Atomics for standard typedef types
456
Howard Hinnantf0af8d92013-01-04 18:58:50 +0000457typedef atomic<bool> atomic_bool;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000458typedef atomic<char> atomic_char;
459typedef atomic<signed char> atomic_schar;
460typedef atomic<unsigned char> atomic_uchar;
461typedef atomic<short> atomic_short;
462typedef atomic<unsigned short> atomic_ushort;
463typedef atomic<int> atomic_int;
464typedef atomic<unsigned int> atomic_uint;
465typedef atomic<long> atomic_long;
466typedef atomic<unsigned long> atomic_ulong;
467typedef atomic<long long> atomic_llong;
468typedef atomic<unsigned long long> atomic_ullong;
Marek Kurdej91bebaa2020-11-24 21:07:06 +0100469typedef atomic<char8_t> atomic_char8_t; // C++20
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000470typedef atomic<char16_t> atomic_char16_t;
471typedef atomic<char32_t> atomic_char32_t;
472typedef atomic<wchar_t> atomic_wchar_t;
473
474typedef atomic<int_least8_t> atomic_int_least8_t;
475typedef atomic<uint_least8_t> atomic_uint_least8_t;
476typedef atomic<int_least16_t> atomic_int_least16_t;
477typedef atomic<uint_least16_t> atomic_uint_least16_t;
478typedef atomic<int_least32_t> atomic_int_least32_t;
479typedef atomic<uint_least32_t> atomic_uint_least32_t;
480typedef atomic<int_least64_t> atomic_int_least64_t;
481typedef atomic<uint_least64_t> atomic_uint_least64_t;
482
483typedef atomic<int_fast8_t> atomic_int_fast8_t;
484typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
485typedef atomic<int_fast16_t> atomic_int_fast16_t;
486typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
Louis Dionneb39adc52020-11-04 14:07:59 -0500487typedef atomic<int_fast32_t> atomic_int_fast32_t;
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000488typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
489typedef atomic<int_fast64_t> atomic_int_fast64_t;
490typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
491
Marshall Clowf710afc2016-06-30 15:28:38 +0000492typedef atomic<int8_t> atomic_int8_t;
493typedef atomic<uint8_t> atomic_uint8_t;
494typedef atomic<int16_t> atomic_int16_t;
495typedef atomic<uint16_t> atomic_uint16_t;
496typedef atomic<int32_t> atomic_int32_t;
497typedef atomic<uint32_t> atomic_uint32_t;
498typedef atomic<int64_t> atomic_int64_t;
499typedef atomic<uint64_t> atomic_uint64_t;
500
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000501typedef atomic<intptr_t> atomic_intptr_t;
502typedef atomic<uintptr_t> atomic_uintptr_t;
503typedef atomic<size_t> atomic_size_t;
504typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
505typedef atomic<intmax_t> atomic_intmax_t;
506typedef atomic<uintmax_t> atomic_uintmax_t;
507
Olivier Giroux6031a712020-06-01 14:30:13 -0700508// flag type and operations
509
510typedef struct atomic_flag
511{
512 atomic_flag() noexcept = default;
513 atomic_flag(const atomic_flag&) = delete;
514 atomic_flag& operator=(const atomic_flag&) = delete;
515 atomic_flag& operator=(const atomic_flag&) volatile = delete;
516
517 bool test(memory_order m = memory_order_seq_cst) volatile noexcept;
518 bool test(memory_order m = memory_order_seq_cst) noexcept;
519 bool test_and_set(memory_order m = memory_order_seq_cst) volatile noexcept;
520 bool test_and_set(memory_order m = memory_order_seq_cst) noexcept;
521 void clear(memory_order m = memory_order_seq_cst) volatile noexcept;
522 void clear(memory_order m = memory_order_seq_cst) noexcept;
523
524 void wait(bool, memory_order = memory_order::seq_cst) const volatile noexcept;
525 void wait(bool, memory_order = memory_order::seq_cst) const noexcept;
526 void notify_one() volatile noexcept;
527 void notify_one() noexcept;
528 void notify_all() volatile noexcept;
529 void notify_all() noexcept;
530} atomic_flag;
531
532bool atomic_flag_test(volatile atomic_flag* obj) noexcept;
533bool atomic_flag_test(atomic_flag* obj) noexcept;
534bool atomic_flag_test_explicit(volatile atomic_flag* obj,
535 memory_order m) noexcept;
536bool atomic_flag_test_explicit(atomic_flag* obj, memory_order m) noexcept;
537bool atomic_flag_test_and_set(volatile atomic_flag* obj) noexcept;
538bool atomic_flag_test_and_set(atomic_flag* obj) noexcept;
539bool atomic_flag_test_and_set_explicit(volatile atomic_flag* obj,
540 memory_order m) noexcept;
541bool atomic_flag_test_and_set_explicit(atomic_flag* obj, memory_order m) noexcept;
542void atomic_flag_clear(volatile atomic_flag* obj) noexcept;
543void atomic_flag_clear(atomic_flag* obj) noexcept;
544void atomic_flag_clear_explicit(volatile atomic_flag* obj, memory_order m) noexcept;
545void atomic_flag_clear_explicit(atomic_flag* obj, memory_order m) noexcept;
546
547void atomic_wait(const volatile atomic_flag* obj, T old) noexcept;
548void atomic_wait(const atomic_flag* obj, T old) noexcept;
549void atomic_wait_explicit(const volatile atomic_flag* obj, T old, memory_order m) noexcept;
550void atomic_wait_explicit(const atomic_flag* obj, T old, memory_order m) noexcept;
551void atomic_one(volatile atomic_flag* obj) noexcept;
552void atomic_one(atomic_flag* obj) noexcept;
553void atomic_all(volatile atomic_flag* obj) noexcept;
554void atomic_all(atomic_flag* obj) noexcept;
555
Howard Hinnant71be7292010-09-27 21:17:38 +0000556// fences
557
Howard Hinnanteee2c142012-04-11 20:14:21 +0000558void atomic_thread_fence(memory_order m) noexcept;
559void atomic_signal_fence(memory_order m) noexcept;
Howard Hinnant71be7292010-09-27 21:17:38 +0000560
Olivier Giroux6031a712020-06-01 14:30:13 -0700561// deprecated
562
563template <class T>
564 void atomic_init(volatile atomic<T>* obj, typename atomic<T>::value_type desr) noexcept;
565
566template <class T>
567 void atomic_init(atomic<T>* obj, typename atomic<T>::value_type desr) noexcept;
568
569#define ATOMIC_VAR_INIT(value) see below
570
571#define ATOMIC_FLAG_INIT see below
572
Howard Hinnant71be7292010-09-27 21:17:38 +0000573} // std
574
575*/
576
577#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500578#include <__availability>
Olivier Giroux161e6e82020-02-18 09:58:34 -0500579#include <__threading_support>
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000580#include <cstddef>
581#include <cstdint>
Olivier Giroux161e6e82020-02-18 09:58:34 -0500582#include <cstring>
Howard Hinnant7bfaeb82010-12-06 23:10:08 +0000583#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000584#include <version>
Howard Hinnant71be7292010-09-27 21:17:38 +0000585
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000586#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant71be7292010-09-27 21:17:38 +0000587#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000588#endif
Howard Hinnant71be7292010-09-27 21:17:38 +0000589
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000590#ifdef _LIBCPP_HAS_NO_THREADS
Davide Italiano011f80a2019-03-05 18:40:49 +0000591# error <atomic> is not supported on this single threaded system
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000592#endif
Davide Italiano011f80a2019-03-05 18:40:49 +0000593#ifdef _LIBCPP_HAS_NO_ATOMIC_HEADER
594# error <atomic> is not implemented
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000595#endif
Volodymyr Sapsaif3ed1fd2018-05-15 22:38:31 +0000596#ifdef kill_dependency
Davide Italiano011f80a2019-03-05 18:40:49 +0000597# error C++ standard library is incompatible with <stdatomic.h>
Volodymyr Sapsaif3ed1fd2018-05-15 22:38:31 +0000598#endif
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000599
Eric Fiselierb5ab51d2017-01-13 23:45:39 +0000600#define _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) \
601 _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_consume || \
602 __m == memory_order_acquire || \
603 __m == memory_order_acq_rel, \
604 "memory order argument to atomic operation is invalid")
605
606#define _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) \
607 _LIBCPP_DIAGNOSE_WARNING(__m == memory_order_release || \
608 __m == memory_order_acq_rel, \
609 "memory order argument to atomic operation is invalid")
610
611#define _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__m, __f) \
612 _LIBCPP_DIAGNOSE_WARNING(__f == memory_order_release || \
613 __f == memory_order_acq_rel, \
614 "memory order argument to atomic operation is invalid")
615
Howard Hinnant71be7292010-09-27 21:17:38 +0000616_LIBCPP_BEGIN_NAMESPACE_STD
617
Eric Fiselierd4e3e5b2019-03-08 23:15:54 +0000618// Figure out what the underlying type for `memory_order` would be if it were
619// declared as an unscoped enum (accounting for -fshort-enums). Use this result
620// to pin the underlying type in C++20.
621enum __legacy_memory_order {
622 __mo_relaxed,
623 __mo_consume,
624 __mo_acquire,
625 __mo_release,
626 __mo_acq_rel,
627 __mo_seq_cst
628};
629
630typedef underlying_type<__legacy_memory_order>::type __memory_order_underlying_t;
631
Davide Italiano011f80a2019-03-05 18:40:49 +0000632#if _LIBCPP_STD_VER > 17
633
Eric Fiselierd4e3e5b2019-03-08 23:15:54 +0000634enum class memory_order : __memory_order_underlying_t {
635 relaxed = __mo_relaxed,
636 consume = __mo_consume,
637 acquire = __mo_acquire,
638 release = __mo_release,
639 acq_rel = __mo_acq_rel,
640 seq_cst = __mo_seq_cst
Davide Italiano011f80a2019-03-05 18:40:49 +0000641};
642
643inline constexpr auto memory_order_relaxed = memory_order::relaxed;
644inline constexpr auto memory_order_consume = memory_order::consume;
645inline constexpr auto memory_order_acquire = memory_order::acquire;
646inline constexpr auto memory_order_release = memory_order::release;
647inline constexpr auto memory_order_acq_rel = memory_order::acq_rel;
648inline constexpr auto memory_order_seq_cst = memory_order::seq_cst;
649
Davide Italiano011f80a2019-03-05 18:40:49 +0000650#else
651
652typedef enum memory_order {
Eric Fiselierd4e3e5b2019-03-08 23:15:54 +0000653 memory_order_relaxed = __mo_relaxed,
654 memory_order_consume = __mo_consume,
655 memory_order_acquire = __mo_acquire,
656 memory_order_release = __mo_release,
657 memory_order_acq_rel = __mo_acq_rel,
658 memory_order_seq_cst = __mo_seq_cst,
Howard Hinnantdca6e712010-09-28 17:13:38 +0000659} memory_order;
660
Davide Italiano011f80a2019-03-05 18:40:49 +0000661#endif // _LIBCPP_STD_VER > 17
662
Olivier Giroux161e6e82020-02-18 09:58:34 -0500663template <typename _Tp> _LIBCPP_INLINE_VISIBILITY
664bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp const& __rhs) {
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500665 return _VSTD::memcmp(&__lhs, &__rhs, sizeof(_Tp)) == 0;
Olivier Giroux161e6e82020-02-18 09:58:34 -0500666}
667
Eric Fiselier51525172019-03-08 23:30:26 +0000668static_assert((is_same<underlying_type<memory_order>::type, __memory_order_underlying_t>::value),
Eric Fiselierd4e3e5b2019-03-08 23:15:54 +0000669 "unexpected underlying type for std::memory_order");
Davide Italiano011f80a2019-03-05 18:40:49 +0000670
671#if defined(_LIBCPP_HAS_GCC_ATOMIC_IMP) || \
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400672 defined(_LIBCPP_ATOMIC_ONLY_USE_BUILTINS)
Davide Italiano011f80a2019-03-05 18:40:49 +0000673
674// [atomics.types.generic]p1 guarantees _Tp is trivially copyable. Because
675// the default operator= in an object is not volatile, a byte-by-byte copy
676// is required.
677template <typename _Tp, typename _Tv> _LIBCPP_INLINE_VISIBILITY
678typename enable_if<is_assignable<_Tp&, _Tv>::value>::type
679__cxx_atomic_assign_volatile(_Tp& __a_value, _Tv const& __val) {
680 __a_value = __val;
681}
682template <typename _Tp, typename _Tv> _LIBCPP_INLINE_VISIBILITY
683typename enable_if<is_assignable<_Tp&, _Tv>::value>::type
684__cxx_atomic_assign_volatile(_Tp volatile& __a_value, _Tv volatile const& __val) {
685 volatile char* __to = reinterpret_cast<volatile char*>(&__a_value);
686 volatile char* __end = __to + sizeof(_Tp);
687 volatile const char* __from = reinterpret_cast<volatile const char*>(&__val);
688 while (__to != __end)
689 *__to++ = *__from++;
690}
691
Davide Italiano31f218a2019-03-05 17:38:33 +0000692#endif
Louis Dionnea1ae0032019-03-04 15:26:27 +0000693
Davide Italiano011f80a2019-03-05 18:40:49 +0000694#if defined(_LIBCPP_HAS_GCC_ATOMIC_IMP)
695
696template <typename _Tp>
697struct __cxx_atomic_base_impl {
698
Eric Fiselier684aaca2015-10-14 08:36:22 +0000699 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier07b2b552016-11-18 06:42:17 +0000700#ifndef _LIBCPP_CXX03_LANG
Davide Italiano011f80a2019-03-05 18:40:49 +0000701 __cxx_atomic_base_impl() _NOEXCEPT = default;
Eric Fiselier684aaca2015-10-14 08:36:22 +0000702#else
Davide Italiano011f80a2019-03-05 18:40:49 +0000703 __cxx_atomic_base_impl() _NOEXCEPT : __a_value() {}
Eric Fiselier07b2b552016-11-18 06:42:17 +0000704#endif // _LIBCPP_CXX03_LANG
Davide Italiano011f80a2019-03-05 18:40:49 +0000705 _LIBCPP_CONSTEXPR explicit __cxx_atomic_base_impl(_Tp value) _NOEXCEPT
Eric Fiselier719e0442015-07-14 17:50:27 +0000706 : __a_value(value) {}
Marshall Clow290eb3f2015-01-11 06:15:59 +0000707 _Tp __a_value;
Dan Albert7b65ace2014-08-09 23:51:51 +0000708};
Dan Albert7b65ace2014-08-09 23:51:51 +0000709
Davide Italiano011f80a2019-03-05 18:40:49 +0000710_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR int __to_gcc_order(memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000711 // Avoid switch statement to make this a constexpr.
712 return __order == memory_order_relaxed ? __ATOMIC_RELAXED:
713 (__order == memory_order_acquire ? __ATOMIC_ACQUIRE:
714 (__order == memory_order_release ? __ATOMIC_RELEASE:
715 (__order == memory_order_seq_cst ? __ATOMIC_SEQ_CST:
716 (__order == memory_order_acq_rel ? __ATOMIC_ACQ_REL:
717 __ATOMIC_CONSUME))));
718}
719
Davide Italiano011f80a2019-03-05 18:40:49 +0000720_LIBCPP_INLINE_VISIBILITY inline _LIBCPP_CONSTEXPR int __to_gcc_failure_order(memory_order __order) {
Dan Albert48815f22015-01-06 18:39:37 +0000721 // Avoid switch statement to make this a constexpr.
722 return __order == memory_order_relaxed ? __ATOMIC_RELAXED:
723 (__order == memory_order_acquire ? __ATOMIC_ACQUIRE:
724 (__order == memory_order_release ? __ATOMIC_RELAXED:
725 (__order == memory_order_seq_cst ? __ATOMIC_SEQ_CST:
726 (__order == memory_order_acq_rel ? __ATOMIC_ACQUIRE:
727 __ATOMIC_CONSUME))));
728}
729
Davide Italiano011f80a2019-03-05 18:40:49 +0000730template <typename _Tp>
731_LIBCPP_INLINE_VISIBILITY
732void __cxx_atomic_init(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __val) {
733 __cxx_atomic_assign_volatile(__a->__a_value, __val);
734}
Dan Albert7b65ace2014-08-09 23:51:51 +0000735
736template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000737_LIBCPP_INLINE_VISIBILITY
738void __cxx_atomic_init(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000739 __a->__a_value = __val;
740}
741
Davide Italiano011f80a2019-03-05 18:40:49 +0000742_LIBCPP_INLINE_VISIBILITY inline
743void __cxx_atomic_thread_fence(memory_order __order) {
744 __atomic_thread_fence(__to_gcc_order(__order));
745}
746
747_LIBCPP_INLINE_VISIBILITY inline
748void __cxx_atomic_signal_fence(memory_order __order) {
749 __atomic_signal_fence(__to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000750}
751
752template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000753_LIBCPP_INLINE_VISIBILITY
754void __cxx_atomic_store(volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp __val,
755 memory_order __order) {
Dan Albert48815f22015-01-06 18:39:37 +0000756 __atomic_store(&__a->__a_value, &__val,
Davide Italiano011f80a2019-03-05 18:40:49 +0000757 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000758}
759
760template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000761_LIBCPP_INLINE_VISIBILITY
762void __cxx_atomic_store(__cxx_atomic_base_impl<_Tp>* __a, _Tp __val,
763 memory_order __order) {
764 __atomic_store(&__a->__a_value, &__val,
765 __to_gcc_order(__order));
766}
767
768template <typename _Tp>
769_LIBCPP_INLINE_VISIBILITY
770_Tp __cxx_atomic_load(const volatile __cxx_atomic_base_impl<_Tp>* __a,
771 memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000772 _Tp __ret;
773 __atomic_load(&__a->__a_value, &__ret,
Davide Italiano011f80a2019-03-05 18:40:49 +0000774 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000775 return __ret;
776}
777
778template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000779_LIBCPP_INLINE_VISIBILITY
780_Tp __cxx_atomic_load(const __cxx_atomic_base_impl<_Tp>* __a, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000781 _Tp __ret;
782 __atomic_load(&__a->__a_value, &__ret,
Davide Italiano011f80a2019-03-05 18:40:49 +0000783 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000784 return __ret;
785}
786
787template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000788_LIBCPP_INLINE_VISIBILITY
789_Tp __cxx_atomic_exchange(volatile __cxx_atomic_base_impl<_Tp>* __a,
790 _Tp __value, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000791 _Tp __ret;
792 __atomic_exchange(&__a->__a_value, &__value, &__ret,
Davide Italiano011f80a2019-03-05 18:40:49 +0000793 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000794 return __ret;
795}
796
797template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000798_LIBCPP_INLINE_VISIBILITY
799_Tp __cxx_atomic_exchange(__cxx_atomic_base_impl<_Tp>* __a, _Tp __value,
800 memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000801 _Tp __ret;
802 __atomic_exchange(&__a->__a_value, &__value, &__ret,
Davide Italiano011f80a2019-03-05 18:40:49 +0000803 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000804 return __ret;
805}
806
807template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000808_LIBCPP_INLINE_VISIBILITY
809bool __cxx_atomic_compare_exchange_strong(
810 volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp* __expected, _Tp __value,
JF Bastien6e412d92018-05-26 19:44:45 +0000811 memory_order __success, memory_order __failure) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000812 return __atomic_compare_exchange(&__a->__a_value, __expected, &__value,
813 false,
Davide Italiano011f80a2019-03-05 18:40:49 +0000814 __to_gcc_order(__success),
815 __to_gcc_failure_order(__failure));
Dan Albert7b65ace2014-08-09 23:51:51 +0000816}
817
818template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000819_LIBCPP_INLINE_VISIBILITY
820bool __cxx_atomic_compare_exchange_strong(
821 __cxx_atomic_base_impl<_Tp>* __a, _Tp* __expected, _Tp __value, memory_order __success,
JF Bastien6e412d92018-05-26 19:44:45 +0000822 memory_order __failure) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000823 return __atomic_compare_exchange(&__a->__a_value, __expected, &__value,
824 false,
Davide Italiano011f80a2019-03-05 18:40:49 +0000825 __to_gcc_order(__success),
826 __to_gcc_failure_order(__failure));
Dan Albert7b65ace2014-08-09 23:51:51 +0000827}
828
829template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000830_LIBCPP_INLINE_VISIBILITY
831bool __cxx_atomic_compare_exchange_weak(
832 volatile __cxx_atomic_base_impl<_Tp>* __a, _Tp* __expected, _Tp __value,
JF Bastien6e412d92018-05-26 19:44:45 +0000833 memory_order __success, memory_order __failure) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000834 return __atomic_compare_exchange(&__a->__a_value, __expected, &__value,
835 true,
Davide Italiano011f80a2019-03-05 18:40:49 +0000836 __to_gcc_order(__success),
837 __to_gcc_failure_order(__failure));
Dan Albert7b65ace2014-08-09 23:51:51 +0000838}
839
840template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000841_LIBCPP_INLINE_VISIBILITY
842bool __cxx_atomic_compare_exchange_weak(
843 __cxx_atomic_base_impl<_Tp>* __a, _Tp* __expected, _Tp __value, memory_order __success,
JF Bastien6e412d92018-05-26 19:44:45 +0000844 memory_order __failure) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000845 return __atomic_compare_exchange(&__a->__a_value, __expected, &__value,
846 true,
Davide Italiano011f80a2019-03-05 18:40:49 +0000847 __to_gcc_order(__success),
848 __to_gcc_failure_order(__failure));
Dan Albert7b65ace2014-08-09 23:51:51 +0000849}
850
851template <typename _Tp>
852struct __skip_amt { enum {value = 1}; };
853
854template <typename _Tp>
855struct __skip_amt<_Tp*> { enum {value = sizeof(_Tp)}; };
856
857// FIXME: Haven't figured out what the spec says about using arrays with
858// atomic_fetch_add. Force a failure rather than creating bad behavior.
859template <typename _Tp>
860struct __skip_amt<_Tp[]> { };
861template <typename _Tp, int n>
862struct __skip_amt<_Tp[n]> { };
863
864template <typename _Tp, typename _Td>
Davide Italiano011f80a2019-03-05 18:40:49 +0000865_LIBCPP_INLINE_VISIBILITY
866_Tp __cxx_atomic_fetch_add(volatile __cxx_atomic_base_impl<_Tp>* __a,
867 _Td __delta, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000868 return __atomic_fetch_add(&__a->__a_value, __delta * __skip_amt<_Tp>::value,
Davide Italiano011f80a2019-03-05 18:40:49 +0000869 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000870}
871
872template <typename _Tp, typename _Td>
Davide Italiano011f80a2019-03-05 18:40:49 +0000873_LIBCPP_INLINE_VISIBILITY
874_Tp __cxx_atomic_fetch_add(__cxx_atomic_base_impl<_Tp>* __a, _Td __delta,
875 memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000876 return __atomic_fetch_add(&__a->__a_value, __delta * __skip_amt<_Tp>::value,
Davide Italiano011f80a2019-03-05 18:40:49 +0000877 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000878}
879
880template <typename _Tp, typename _Td>
Davide Italiano011f80a2019-03-05 18:40:49 +0000881_LIBCPP_INLINE_VISIBILITY
882_Tp __cxx_atomic_fetch_sub(volatile __cxx_atomic_base_impl<_Tp>* __a,
883 _Td __delta, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000884 return __atomic_fetch_sub(&__a->__a_value, __delta * __skip_amt<_Tp>::value,
Davide Italiano011f80a2019-03-05 18:40:49 +0000885 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000886}
887
888template <typename _Tp, typename _Td>
Davide Italiano011f80a2019-03-05 18:40:49 +0000889_LIBCPP_INLINE_VISIBILITY
890_Tp __cxx_atomic_fetch_sub(__cxx_atomic_base_impl<_Tp>* __a, _Td __delta,
891 memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000892 return __atomic_fetch_sub(&__a->__a_value, __delta * __skip_amt<_Tp>::value,
Davide Italiano011f80a2019-03-05 18:40:49 +0000893 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000894}
895
896template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000897_LIBCPP_INLINE_VISIBILITY
898_Tp __cxx_atomic_fetch_and(volatile __cxx_atomic_base_impl<_Tp>* __a,
899 _Tp __pattern, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000900 return __atomic_fetch_and(&__a->__a_value, __pattern,
Davide Italiano011f80a2019-03-05 18:40:49 +0000901 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000902}
903
904template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000905_LIBCPP_INLINE_VISIBILITY
906_Tp __cxx_atomic_fetch_and(__cxx_atomic_base_impl<_Tp>* __a,
907 _Tp __pattern, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000908 return __atomic_fetch_and(&__a->__a_value, __pattern,
Davide Italiano011f80a2019-03-05 18:40:49 +0000909 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000910}
911
912template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000913_LIBCPP_INLINE_VISIBILITY
914_Tp __cxx_atomic_fetch_or(volatile __cxx_atomic_base_impl<_Tp>* __a,
915 _Tp __pattern, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000916 return __atomic_fetch_or(&__a->__a_value, __pattern,
Davide Italiano011f80a2019-03-05 18:40:49 +0000917 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000918}
919
920template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000921_LIBCPP_INLINE_VISIBILITY
922_Tp __cxx_atomic_fetch_or(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern,
923 memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000924 return __atomic_fetch_or(&__a->__a_value, __pattern,
Davide Italiano011f80a2019-03-05 18:40:49 +0000925 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000926}
927
928template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000929_LIBCPP_INLINE_VISIBILITY
930_Tp __cxx_atomic_fetch_xor(volatile __cxx_atomic_base_impl<_Tp>* __a,
931 _Tp __pattern, memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000932 return __atomic_fetch_xor(&__a->__a_value, __pattern,
Davide Italiano011f80a2019-03-05 18:40:49 +0000933 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000934}
935
936template <typename _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +0000937_LIBCPP_INLINE_VISIBILITY
938_Tp __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp>* __a, _Tp __pattern,
939 memory_order __order) {
Dan Albert7b65ace2014-08-09 23:51:51 +0000940 return __atomic_fetch_xor(&__a->__a_value, __pattern,
Davide Italiano011f80a2019-03-05 18:40:49 +0000941 __to_gcc_order(__order));
Dan Albert7b65ace2014-08-09 23:51:51 +0000942}
Davide Italiano011f80a2019-03-05 18:40:49 +0000943
944#define __cxx_atomic_is_lock_free(__s) __atomic_is_lock_free(__s, 0)
945
946#elif defined(_LIBCPP_HAS_C_ATOMIC_IMP)
947
948template <typename _Tp>
949struct __cxx_atomic_base_impl {
950
951 _LIBCPP_INLINE_VISIBILITY
952#ifndef _LIBCPP_CXX03_LANG
953 __cxx_atomic_base_impl() _NOEXCEPT = default;
954#else
955 __cxx_atomic_base_impl() _NOEXCEPT : __a_value() {}
956#endif // _LIBCPP_CXX03_LANG
957 _LIBCPP_CONSTEXPR explicit __cxx_atomic_base_impl(_Tp value) _NOEXCEPT
958 : __a_value(value) {}
Louis Dionnefaa17452019-09-04 12:44:19 +0000959 _LIBCPP_DISABLE_EXTENSION_WARNING _Atomic(_Tp) __a_value;
Davide Italiano011f80a2019-03-05 18:40:49 +0000960};
961
962#define __cxx_atomic_is_lock_free(__s) __c11_atomic_is_lock_free(__s)
963
964_LIBCPP_INLINE_VISIBILITY inline
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000965void __cxx_atomic_thread_fence(memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000966 __c11_atomic_thread_fence(static_cast<__memory_order_underlying_t>(__order));
967}
968
969_LIBCPP_INLINE_VISIBILITY inline
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000970void __cxx_atomic_signal_fence(memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000971 __c11_atomic_signal_fence(static_cast<__memory_order_underlying_t>(__order));
972}
973
974template<class _Tp>
975_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000976void __cxx_atomic_init(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __val) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000977 __c11_atomic_init(&__a->__a_value, __val);
978}
979template<class _Tp>
980_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000981void __cxx_atomic_init(__cxx_atomic_base_impl<_Tp> * __a, _Tp __val) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000982 __c11_atomic_init(&__a->__a_value, __val);
983}
984
985template<class _Tp>
986_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000987void __cxx_atomic_store(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __val, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000988 __c11_atomic_store(&__a->__a_value, __val, static_cast<__memory_order_underlying_t>(__order));
989}
990template<class _Tp>
991_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000992void __cxx_atomic_store(__cxx_atomic_base_impl<_Tp> * __a, _Tp __val, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000993 __c11_atomic_store(&__a->__a_value, __val, static_cast<__memory_order_underlying_t>(__order));
994}
995
996template<class _Tp>
997_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +0000998_Tp __cxx_atomic_load(__cxx_atomic_base_impl<_Tp> const volatile* __a, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +0000999 using __ptr_type = typename remove_const<decltype(__a->__a_value)>::type*;
1000 return __c11_atomic_load(const_cast<__ptr_type>(&__a->__a_value), static_cast<__memory_order_underlying_t>(__order));
1001}
1002template<class _Tp>
1003_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001004_Tp __cxx_atomic_load(__cxx_atomic_base_impl<_Tp> const* __a, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001005 using __ptr_type = typename remove_const<decltype(__a->__a_value)>::type*;
1006 return __c11_atomic_load(const_cast<__ptr_type>(&__a->__a_value), static_cast<__memory_order_underlying_t>(__order));
1007}
1008
1009template<class _Tp>
1010_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001011_Tp __cxx_atomic_exchange(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __value, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001012 return __c11_atomic_exchange(&__a->__a_value, __value, static_cast<__memory_order_underlying_t>(__order));
1013}
1014template<class _Tp>
1015_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001016_Tp __cxx_atomic_exchange(__cxx_atomic_base_impl<_Tp> * __a, _Tp __value, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001017 return __c11_atomic_exchange(&__a->__a_value, __value, static_cast<__memory_order_underlying_t>(__order));
1018}
1019
1020template<class _Tp>
1021_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001022bool __cxx_atomic_compare_exchange_strong(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp* __expected, _Tp __value, memory_order __success, memory_order __failure) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001023 return __c11_atomic_compare_exchange_strong(&__a->__a_value, __expected, __value, static_cast<__memory_order_underlying_t>(__success), static_cast<__memory_order_underlying_t>(__failure));
1024}
1025template<class _Tp>
1026_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001027bool __cxx_atomic_compare_exchange_strong(__cxx_atomic_base_impl<_Tp> * __a, _Tp* __expected, _Tp __value, memory_order __success, memory_order __failure) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001028 return __c11_atomic_compare_exchange_strong(&__a->__a_value, __expected, __value, static_cast<__memory_order_underlying_t>(__success), static_cast<__memory_order_underlying_t>(__failure));
1029}
1030
1031template<class _Tp>
1032_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001033bool __cxx_atomic_compare_exchange_weak(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp* __expected, _Tp __value, memory_order __success, memory_order __failure) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001034 return __c11_atomic_compare_exchange_weak(&__a->__a_value, __expected, __value, static_cast<__memory_order_underlying_t>(__success), static_cast<__memory_order_underlying_t>(__failure));
1035}
1036template<class _Tp>
1037_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001038bool __cxx_atomic_compare_exchange_weak(__cxx_atomic_base_impl<_Tp> * __a, _Tp* __expected, _Tp __value, memory_order __success, memory_order __failure) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001039 return __c11_atomic_compare_exchange_weak(&__a->__a_value, __expected, __value, static_cast<__memory_order_underlying_t>(__success), static_cast<__memory_order_underlying_t>(__failure));
1040}
1041
1042template<class _Tp>
1043_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001044_Tp __cxx_atomic_fetch_add(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001045 return __c11_atomic_fetch_add(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1046}
1047template<class _Tp>
1048_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001049_Tp __cxx_atomic_fetch_add(__cxx_atomic_base_impl<_Tp> * __a, _Tp __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001050 return __c11_atomic_fetch_add(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1051}
1052
1053template<class _Tp>
1054_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001055_Tp* __cxx_atomic_fetch_add(__cxx_atomic_base_impl<_Tp*> volatile* __a, ptrdiff_t __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001056 return __c11_atomic_fetch_add(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1057}
1058template<class _Tp>
1059_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001060_Tp* __cxx_atomic_fetch_add(__cxx_atomic_base_impl<_Tp*> * __a, ptrdiff_t __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001061 return __c11_atomic_fetch_add(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1062}
1063
1064template<class _Tp>
1065_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001066_Tp __cxx_atomic_fetch_sub(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001067 return __c11_atomic_fetch_sub(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1068}
1069template<class _Tp>
1070_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001071_Tp __cxx_atomic_fetch_sub(__cxx_atomic_base_impl<_Tp> * __a, _Tp __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001072 return __c11_atomic_fetch_sub(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1073}
1074template<class _Tp>
1075_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001076_Tp* __cxx_atomic_fetch_sub(__cxx_atomic_base_impl<_Tp*> volatile* __a, ptrdiff_t __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001077 return __c11_atomic_fetch_sub(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1078}
1079template<class _Tp>
1080_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001081_Tp* __cxx_atomic_fetch_sub(__cxx_atomic_base_impl<_Tp*> * __a, ptrdiff_t __delta, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001082 return __c11_atomic_fetch_sub(&__a->__a_value, __delta, static_cast<__memory_order_underlying_t>(__order));
1083}
1084
1085template<class _Tp>
1086_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001087_Tp __cxx_atomic_fetch_and(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001088 return __c11_atomic_fetch_and(&__a->__a_value, __pattern, static_cast<__memory_order_underlying_t>(__order));
1089}
1090template<class _Tp>
1091_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001092_Tp __cxx_atomic_fetch_and(__cxx_atomic_base_impl<_Tp> * __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001093 return __c11_atomic_fetch_and(&__a->__a_value, __pattern, static_cast<__memory_order_underlying_t>(__order));
1094}
1095
1096template<class _Tp>
1097_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001098_Tp __cxx_atomic_fetch_or(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001099 return __c11_atomic_fetch_or(&__a->__a_value, __pattern, static_cast<__memory_order_underlying_t>(__order));
1100}
1101template<class _Tp>
1102_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001103_Tp __cxx_atomic_fetch_or(__cxx_atomic_base_impl<_Tp> * __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001104 return __c11_atomic_fetch_or(&__a->__a_value, __pattern, static_cast<__memory_order_underlying_t>(__order));
1105}
1106
1107template<class _Tp>
1108_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001109_Tp __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp> volatile* __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001110 return __c11_atomic_fetch_xor(&__a->__a_value, __pattern, static_cast<__memory_order_underlying_t>(__order));
1111}
1112template<class _Tp>
1113_LIBCPP_INLINE_VISIBILITY
Eric Fiselier8fa9b6f2019-06-23 02:49:12 +00001114_Tp __cxx_atomic_fetch_xor(__cxx_atomic_base_impl<_Tp> * __a, _Tp __pattern, memory_order __order) _NOEXCEPT {
Davide Italiano011f80a2019-03-05 18:40:49 +00001115 return __c11_atomic_fetch_xor(&__a->__a_value, __pattern, static_cast<__memory_order_underlying_t>(__order));
1116}
1117
1118#endif // _LIBCPP_HAS_GCC_ATOMIC_IMP, _LIBCPP_HAS_C_ATOMIC_IMP
Dan Albert7b65ace2014-08-09 23:51:51 +00001119
Howard Hinnantdca6e712010-09-28 17:13:38 +00001120template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001121_LIBCPP_INLINE_VISIBILITY
1122_Tp kill_dependency(_Tp __y) _NOEXCEPT
Howard Hinnantdca6e712010-09-28 17:13:38 +00001123{
1124 return __y;
1125}
Howard Hinnant71be7292010-09-27 21:17:38 +00001126
Eric Fiselierbed42df2017-04-20 23:22:46 +00001127#if defined(__CLANG_ATOMIC_BOOL_LOCK_FREE)
1128# define ATOMIC_BOOL_LOCK_FREE __CLANG_ATOMIC_BOOL_LOCK_FREE
1129# define ATOMIC_CHAR_LOCK_FREE __CLANG_ATOMIC_CHAR_LOCK_FREE
Marek Kurdej91bebaa2020-11-24 21:07:06 +01001130#ifndef _LIBCPP_NO_HAS_CHAR8_T
1131# define ATOMIC_CHAR8_T_LOCK_FREE __CLANG_ATOMIC_CHAR8_T_LOCK_FREE
1132#endif
Eric Fiselierbed42df2017-04-20 23:22:46 +00001133# define ATOMIC_CHAR16_T_LOCK_FREE __CLANG_ATOMIC_CHAR16_T_LOCK_FREE
1134# define ATOMIC_CHAR32_T_LOCK_FREE __CLANG_ATOMIC_CHAR32_T_LOCK_FREE
1135# define ATOMIC_WCHAR_T_LOCK_FREE __CLANG_ATOMIC_WCHAR_T_LOCK_FREE
1136# define ATOMIC_SHORT_LOCK_FREE __CLANG_ATOMIC_SHORT_LOCK_FREE
1137# define ATOMIC_INT_LOCK_FREE __CLANG_ATOMIC_INT_LOCK_FREE
1138# define ATOMIC_LONG_LOCK_FREE __CLANG_ATOMIC_LONG_LOCK_FREE
1139# define ATOMIC_LLONG_LOCK_FREE __CLANG_ATOMIC_LLONG_LOCK_FREE
1140# define ATOMIC_POINTER_LOCK_FREE __CLANG_ATOMIC_POINTER_LOCK_FREE
Davide Italiano011f80a2019-03-05 18:40:49 +00001141#elif defined(__GCC_ATOMIC_BOOL_LOCK_FREE)
Eric Fiselierbed42df2017-04-20 23:22:46 +00001142# define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
1143# define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
Marek Kurdej91bebaa2020-11-24 21:07:06 +01001144#ifndef _LIBCPP_NO_HAS_CHAR8_T
1145# define ATOMIC_CHAR8_T_LOCK_FREE __GCC_ATOMIC_CHAR8_T_LOCK_FREE
1146#endif
Eric Fiselierbed42df2017-04-20 23:22:46 +00001147# define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
1148# define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
1149# define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
1150# define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
1151# define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
1152# define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
1153# define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
1154# define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
1155#endif
JF Bastienfdb42c22016-03-25 15:48:21 +00001156
Davide Italiano011f80a2019-03-05 18:40:49 +00001157#ifdef _LIBCPP_ATOMIC_ONLY_USE_BUILTINS
1158
1159template<typename _Tp>
1160struct __cxx_atomic_lock_impl {
1161
1162 _LIBCPP_INLINE_VISIBILITY
1163 __cxx_atomic_lock_impl() _NOEXCEPT
1164 : __a_value(), __a_lock(0) {}
1165 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR explicit
1166 __cxx_atomic_lock_impl(_Tp value) _NOEXCEPT
1167 : __a_value(value), __a_lock(0) {}
1168
1169 _Tp __a_value;
1170 mutable __cxx_atomic_base_impl<_LIBCPP_ATOMIC_FLAG_TYPE> __a_lock;
1171
1172 _LIBCPP_INLINE_VISIBILITY void __lock() const volatile {
1173 while(1 == __cxx_atomic_exchange(&__a_lock, _LIBCPP_ATOMIC_FLAG_TYPE(true), memory_order_acquire))
1174 /*spin*/;
1175 }
1176 _LIBCPP_INLINE_VISIBILITY void __lock() const {
1177 while(1 == __cxx_atomic_exchange(&__a_lock, _LIBCPP_ATOMIC_FLAG_TYPE(true), memory_order_acquire))
1178 /*spin*/;
1179 }
1180 _LIBCPP_INLINE_VISIBILITY void __unlock() const volatile {
1181 __cxx_atomic_store(&__a_lock, _LIBCPP_ATOMIC_FLAG_TYPE(false), memory_order_release);
1182 }
1183 _LIBCPP_INLINE_VISIBILITY void __unlock() const {
1184 __cxx_atomic_store(&__a_lock, _LIBCPP_ATOMIC_FLAG_TYPE(false), memory_order_release);
1185 }
1186 _LIBCPP_INLINE_VISIBILITY _Tp __read() const volatile {
1187 __lock();
1188 _Tp __old;
1189 __cxx_atomic_assign_volatile(__old, __a_value);
1190 __unlock();
1191 return __old;
1192 }
1193 _LIBCPP_INLINE_VISIBILITY _Tp __read() const {
1194 __lock();
1195 _Tp __old = __a_value;
1196 __unlock();
1197 return __old;
1198 }
1199};
1200
1201template <typename _Tp>
1202_LIBCPP_INLINE_VISIBILITY
1203void __cxx_atomic_init(volatile __cxx_atomic_lock_impl<_Tp>* __a, _Tp __val) {
1204 __cxx_atomic_assign_volatile(__a->__a_value, __val);
1205}
1206template <typename _Tp>
1207_LIBCPP_INLINE_VISIBILITY
1208void __cxx_atomic_init(__cxx_atomic_lock_impl<_Tp>* __a, _Tp __val) {
1209 __a->__a_value = __val;
1210}
1211
1212template <typename _Tp>
1213_LIBCPP_INLINE_VISIBILITY
1214void __cxx_atomic_store(volatile __cxx_atomic_lock_impl<_Tp>* __a, _Tp __val, memory_order) {
1215 __a->__lock();
1216 __cxx_atomic_assign_volatile(__a->__a_value, __val);
1217 __a->__unlock();
1218}
1219template <typename _Tp>
1220_LIBCPP_INLINE_VISIBILITY
1221void __cxx_atomic_store(__cxx_atomic_lock_impl<_Tp>* __a, _Tp __val, memory_order) {
1222 __a->__lock();
1223 __a->__a_value = __val;
1224 __a->__unlock();
1225}
1226
1227template <typename _Tp>
1228_LIBCPP_INLINE_VISIBILITY
1229_Tp __cxx_atomic_load(const volatile __cxx_atomic_lock_impl<_Tp>* __a, memory_order) {
1230 return __a->__read();
1231}
1232template <typename _Tp>
1233_LIBCPP_INLINE_VISIBILITY
1234_Tp __cxx_atomic_load(const __cxx_atomic_lock_impl<_Tp>* __a, memory_order) {
1235 return __a->__read();
1236}
1237
1238template <typename _Tp>
1239_LIBCPP_INLINE_VISIBILITY
1240_Tp __cxx_atomic_exchange(volatile __cxx_atomic_lock_impl<_Tp>* __a, _Tp __value, memory_order) {
1241 __a->__lock();
1242 _Tp __old;
1243 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1244 __cxx_atomic_assign_volatile(__a->__a_value, __value);
1245 __a->__unlock();
1246 return __old;
1247}
1248template <typename _Tp>
1249_LIBCPP_INLINE_VISIBILITY
1250_Tp __cxx_atomic_exchange(__cxx_atomic_lock_impl<_Tp>* __a, _Tp __value, memory_order) {
1251 __a->__lock();
1252 _Tp __old = __a->__a_value;
1253 __a->__a_value = __value;
1254 __a->__unlock();
1255 return __old;
1256}
1257
1258template <typename _Tp>
1259_LIBCPP_INLINE_VISIBILITY
1260bool __cxx_atomic_compare_exchange_strong(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1261 _Tp* __expected, _Tp __value, memory_order, memory_order) {
Olivier Giroux161e6e82020-02-18 09:58:34 -05001262 _Tp __temp;
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001263 __a->__lock();
Olivier Giroux161e6e82020-02-18 09:58:34 -05001264 __cxx_atomic_assign_volatile(__temp, __a->__a_value);
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001265 bool __ret = (_VSTD::memcmp(&__temp, __expected, sizeof(_Tp)) == 0);
Davide Italiano011f80a2019-03-05 18:40:49 +00001266 if(__ret)
1267 __cxx_atomic_assign_volatile(__a->__a_value, __value);
1268 else
1269 __cxx_atomic_assign_volatile(*__expected, __a->__a_value);
1270 __a->__unlock();
1271 return __ret;
1272}
1273template <typename _Tp>
1274_LIBCPP_INLINE_VISIBILITY
1275bool __cxx_atomic_compare_exchange_strong(__cxx_atomic_lock_impl<_Tp>* __a,
1276 _Tp* __expected, _Tp __value, memory_order, memory_order) {
1277 __a->__lock();
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001278 bool __ret = (_VSTD::memcmp(&__a->__a_value, __expected, sizeof(_Tp)) == 0);
Davide Italiano011f80a2019-03-05 18:40:49 +00001279 if(__ret)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001280 _VSTD::memcpy(&__a->__a_value, &__value, sizeof(_Tp));
Davide Italiano011f80a2019-03-05 18:40:49 +00001281 else
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001282 _VSTD::memcpy(__expected, &__a->__a_value, sizeof(_Tp));
Davide Italiano011f80a2019-03-05 18:40:49 +00001283 __a->__unlock();
1284 return __ret;
1285}
1286
1287template <typename _Tp>
1288_LIBCPP_INLINE_VISIBILITY
1289bool __cxx_atomic_compare_exchange_weak(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1290 _Tp* __expected, _Tp __value, memory_order, memory_order) {
Olivier Giroux161e6e82020-02-18 09:58:34 -05001291 _Tp __temp;
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001292 __a->__lock();
Olivier Giroux161e6e82020-02-18 09:58:34 -05001293 __cxx_atomic_assign_volatile(__temp, __a->__a_value);
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001294 bool __ret = (_VSTD::memcmp(&__temp, __expected, sizeof(_Tp)) == 0);
Davide Italiano011f80a2019-03-05 18:40:49 +00001295 if(__ret)
1296 __cxx_atomic_assign_volatile(__a->__a_value, __value);
1297 else
1298 __cxx_atomic_assign_volatile(*__expected, __a->__a_value);
1299 __a->__unlock();
1300 return __ret;
1301}
1302template <typename _Tp>
1303_LIBCPP_INLINE_VISIBILITY
1304bool __cxx_atomic_compare_exchange_weak(__cxx_atomic_lock_impl<_Tp>* __a,
1305 _Tp* __expected, _Tp __value, memory_order, memory_order) {
1306 __a->__lock();
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001307 bool __ret = (_VSTD::memcmp(&__a->__a_value, __expected, sizeof(_Tp)) == 0);
Davide Italiano011f80a2019-03-05 18:40:49 +00001308 if(__ret)
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001309 _VSTD::memcpy(&__a->__a_value, &__value, sizeof(_Tp));
Davide Italiano011f80a2019-03-05 18:40:49 +00001310 else
Arthur O'Dwyer22236632020-12-07 21:50:15 -05001311 _VSTD::memcpy(__expected, &__a->__a_value, sizeof(_Tp));
Davide Italiano011f80a2019-03-05 18:40:49 +00001312 __a->__unlock();
1313 return __ret;
1314}
1315
1316template <typename _Tp, typename _Td>
1317_LIBCPP_INLINE_VISIBILITY
1318_Tp __cxx_atomic_fetch_add(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1319 _Td __delta, memory_order) {
1320 __a->__lock();
1321 _Tp __old;
1322 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1323 __cxx_atomic_assign_volatile(__a->__a_value, _Tp(__old + __delta));
1324 __a->__unlock();
1325 return __old;
1326}
1327template <typename _Tp, typename _Td>
1328_LIBCPP_INLINE_VISIBILITY
1329_Tp __cxx_atomic_fetch_add(__cxx_atomic_lock_impl<_Tp>* __a,
1330 _Td __delta, memory_order) {
1331 __a->__lock();
1332 _Tp __old = __a->__a_value;
1333 __a->__a_value += __delta;
1334 __a->__unlock();
1335 return __old;
1336}
1337
1338template <typename _Tp, typename _Td>
1339_LIBCPP_INLINE_VISIBILITY
1340_Tp* __cxx_atomic_fetch_add(volatile __cxx_atomic_lock_impl<_Tp*>* __a,
1341 ptrdiff_t __delta, memory_order) {
1342 __a->__lock();
1343 _Tp* __old;
1344 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1345 __cxx_atomic_assign_volatile(__a->__a_value, __old + __delta);
1346 __a->__unlock();
1347 return __old;
1348}
1349template <typename _Tp, typename _Td>
1350_LIBCPP_INLINE_VISIBILITY
1351_Tp* __cxx_atomic_fetch_add(__cxx_atomic_lock_impl<_Tp*>* __a,
1352 ptrdiff_t __delta, memory_order) {
1353 __a->__lock();
1354 _Tp* __old = __a->__a_value;
1355 __a->__a_value += __delta;
1356 __a->__unlock();
1357 return __old;
1358}
1359
1360template <typename _Tp, typename _Td>
1361_LIBCPP_INLINE_VISIBILITY
1362_Tp __cxx_atomic_fetch_sub(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1363 _Td __delta, memory_order) {
1364 __a->__lock();
1365 _Tp __old;
1366 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1367 __cxx_atomic_assign_volatile(__a->__a_value, _Tp(__old - __delta));
1368 __a->__unlock();
1369 return __old;
1370}
1371template <typename _Tp, typename _Td>
1372_LIBCPP_INLINE_VISIBILITY
1373_Tp __cxx_atomic_fetch_sub(__cxx_atomic_lock_impl<_Tp>* __a,
1374 _Td __delta, memory_order) {
1375 __a->__lock();
1376 _Tp __old = __a->__a_value;
1377 __a->__a_value -= __delta;
1378 __a->__unlock();
1379 return __old;
1380}
1381
1382template <typename _Tp>
1383_LIBCPP_INLINE_VISIBILITY
1384_Tp __cxx_atomic_fetch_and(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1385 _Tp __pattern, memory_order) {
1386 __a->__lock();
1387 _Tp __old;
1388 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1389 __cxx_atomic_assign_volatile(__a->__a_value, _Tp(__old & __pattern));
1390 __a->__unlock();
1391 return __old;
1392}
1393template <typename _Tp>
1394_LIBCPP_INLINE_VISIBILITY
1395_Tp __cxx_atomic_fetch_and(__cxx_atomic_lock_impl<_Tp>* __a,
1396 _Tp __pattern, memory_order) {
1397 __a->__lock();
1398 _Tp __old = __a->__a_value;
1399 __a->__a_value &= __pattern;
1400 __a->__unlock();
1401 return __old;
1402}
1403
1404template <typename _Tp>
1405_LIBCPP_INLINE_VISIBILITY
1406_Tp __cxx_atomic_fetch_or(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1407 _Tp __pattern, memory_order) {
1408 __a->__lock();
1409 _Tp __old;
1410 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1411 __cxx_atomic_assign_volatile(__a->__a_value, _Tp(__old | __pattern));
1412 __a->__unlock();
1413 return __old;
1414}
1415template <typename _Tp>
1416_LIBCPP_INLINE_VISIBILITY
1417_Tp __cxx_atomic_fetch_or(__cxx_atomic_lock_impl<_Tp>* __a,
1418 _Tp __pattern, memory_order) {
1419 __a->__lock();
1420 _Tp __old = __a->__a_value;
1421 __a->__a_value |= __pattern;
1422 __a->__unlock();
1423 return __old;
1424}
1425
1426template <typename _Tp>
1427_LIBCPP_INLINE_VISIBILITY
1428_Tp __cxx_atomic_fetch_xor(volatile __cxx_atomic_lock_impl<_Tp>* __a,
1429 _Tp __pattern, memory_order) {
1430 __a->__lock();
1431 _Tp __old;
1432 __cxx_atomic_assign_volatile(__old, __a->__a_value);
1433 __cxx_atomic_assign_volatile(__a->__a_value, _Tp(__old ^ __pattern));
1434 __a->__unlock();
1435 return __old;
1436}
1437template <typename _Tp>
1438_LIBCPP_INLINE_VISIBILITY
1439_Tp __cxx_atomic_fetch_xor(__cxx_atomic_lock_impl<_Tp>* __a,
1440 _Tp __pattern, memory_order) {
1441 __a->__lock();
1442 _Tp __old = __a->__a_value;
1443 __a->__a_value ^= __pattern;
1444 __a->__unlock();
1445 return __old;
1446}
1447
1448#ifdef __cpp_lib_atomic_is_always_lock_free
1449
1450template<typename _Tp> struct __cxx_is_always_lock_free {
1451 enum { __value = __atomic_always_lock_free(sizeof(_Tp), 0) }; };
1452
1453#else
1454
1455template<typename _Tp> struct __cxx_is_always_lock_free { enum { __value = false }; };
1456// Implementations must match the C ATOMIC_*_LOCK_FREE macro values.
1457template<> struct __cxx_is_always_lock_free<bool> { enum { __value = 2 == ATOMIC_BOOL_LOCK_FREE }; };
1458template<> struct __cxx_is_always_lock_free<char> { enum { __value = 2 == ATOMIC_CHAR_LOCK_FREE }; };
1459template<> struct __cxx_is_always_lock_free<signed char> { enum { __value = 2 == ATOMIC_CHAR_LOCK_FREE }; };
1460template<> struct __cxx_is_always_lock_free<unsigned char> { enum { __value = 2 == ATOMIC_CHAR_LOCK_FREE }; };
Marek Kurdej91bebaa2020-11-24 21:07:06 +01001461#ifndef _LIBCPP_NO_HAS_CHAR8_T
1462template<> struct __cxx_is_always_lock_free<char8_t> { enum { __value = 2 == ATOMIC_CHAR8_T_LOCK_FREE }; };
1463#endif
Davide Italiano011f80a2019-03-05 18:40:49 +00001464template<> struct __cxx_is_always_lock_free<char16_t> { enum { __value = 2 == ATOMIC_CHAR16_T_LOCK_FREE }; };
1465template<> struct __cxx_is_always_lock_free<char32_t> { enum { __value = 2 == ATOMIC_CHAR32_T_LOCK_FREE }; };
1466template<> struct __cxx_is_always_lock_free<wchar_t> { enum { __value = 2 == ATOMIC_WCHAR_T_LOCK_FREE }; };
1467template<> struct __cxx_is_always_lock_free<short> { enum { __value = 2 == ATOMIC_SHORT_LOCK_FREE }; };
1468template<> struct __cxx_is_always_lock_free<unsigned short> { enum { __value = 2 == ATOMIC_SHORT_LOCK_FREE }; };
1469template<> struct __cxx_is_always_lock_free<int> { enum { __value = 2 == ATOMIC_INT_LOCK_FREE }; };
1470template<> struct __cxx_is_always_lock_free<unsigned int> { enum { __value = 2 == ATOMIC_INT_LOCK_FREE }; };
1471template<> struct __cxx_is_always_lock_free<long> { enum { __value = 2 == ATOMIC_LONG_LOCK_FREE }; };
1472template<> struct __cxx_is_always_lock_free<unsigned long> { enum { __value = 2 == ATOMIC_LONG_LOCK_FREE }; };
1473template<> struct __cxx_is_always_lock_free<long long> { enum { __value = 2 == ATOMIC_LLONG_LOCK_FREE }; };
1474template<> struct __cxx_is_always_lock_free<unsigned long long> { enum { __value = 2 == ATOMIC_LLONG_LOCK_FREE }; };
1475template<typename _Tp> struct __cxx_is_always_lock_free<_Tp*> { enum { __value = 2 == ATOMIC_POINTER_LOCK_FREE }; };
1476template<> struct __cxx_is_always_lock_free<std::nullptr_t> { enum { __value = 2 == ATOMIC_POINTER_LOCK_FREE }; };
1477
1478#endif //__cpp_lib_atomic_is_always_lock_free
1479
1480template <typename _Tp,
1481 typename _Base = typename conditional<__cxx_is_always_lock_free<_Tp>::__value,
1482 __cxx_atomic_base_impl<_Tp>,
1483 __cxx_atomic_lock_impl<_Tp> >::type>
1484#else
1485template <typename _Tp,
1486 typename _Base = __cxx_atomic_base_impl<_Tp> >
1487#endif //_LIBCPP_ATOMIC_ONLY_USE_BUILTINS
1488struct __cxx_atomic_impl : public _Base {
1489
1490#if _GNUC_VER >= 501
1491 static_assert(is_trivially_copyable<_Tp>::value,
1492 "std::atomic<Tp> requires that 'Tp' be a trivially copyable type");
1493#endif
1494
1495 _LIBCPP_INLINE_VISIBILITY __cxx_atomic_impl() _NOEXCEPT _LIBCPP_DEFAULT
1496 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR explicit __cxx_atomic_impl(_Tp value) _NOEXCEPT
1497 : _Base(value) {}
1498};
1499
Olivier Giroux161e6e82020-02-18 09:58:34 -05001500#ifdef __linux__
1501 using __cxx_contention_t = int32_t;
1502#else
1503 using __cxx_contention_t = int64_t;
1504#endif //__linux__
1505
Olivier Giroux161e6e82020-02-18 09:58:34 -05001506using __cxx_atomic_contention_t = __cxx_atomic_impl<__cxx_contention_t>;
1507
1508#ifndef _LIBCPP_HAS_NO_PLATFORM_WAIT
1509
Louis Dionne48a828b2020-02-24 10:08:41 -05001510_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*);
1511_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile*);
1512_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile*);
1513_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(void const volatile*, __cxx_contention_t);
Olivier Giroux161e6e82020-02-18 09:58:34 -05001514
Louis Dionne48a828b2020-02-24 10:08:41 -05001515_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile*);
1516_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile*);
1517_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*);
1518_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t);
Olivier Giroux161e6e82020-02-18 09:58:34 -05001519
1520template <class _Atp, class _Fn>
Louis Dionne08cd5cb2020-02-24 11:39:48 -05001521struct __libcpp_atomic_wait_backoff_impl {
1522 _Atp* __a;
1523 _Fn __test_fn;
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001524 _LIBCPP_AVAILABILITY_SYNC
Louis Dionne08cd5cb2020-02-24 11:39:48 -05001525 _LIBCPP_INLINE_VISIBILITY bool operator()(chrono::nanoseconds __elapsed) const
1526 {
Olivier Giroux161e6e82020-02-18 09:58:34 -05001527 if(__elapsed > chrono::microseconds(64))
1528 {
1529 auto const __monitor = __libcpp_atomic_monitor(__a);
1530 if(__test_fn())
1531 return true;
1532 __libcpp_atomic_wait(__a, __monitor);
1533 }
1534 else if(__elapsed > chrono::microseconds(4))
1535 __libcpp_thread_yield();
1536 else
Marek Kurdejc79652f2020-11-15 16:17:52 +01001537 {} // poll
Olivier Giroux161e6e82020-02-18 09:58:34 -05001538 return false;
Louis Dionne08cd5cb2020-02-24 11:39:48 -05001539 }
1540};
1541
1542template <class _Atp, class _Fn>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001543_LIBCPP_AVAILABILITY_SYNC
Louis Dionne08cd5cb2020-02-24 11:39:48 -05001544_LIBCPP_INLINE_VISIBILITY bool __cxx_atomic_wait(_Atp* __a, _Fn && __test_fn)
1545{
1546 __libcpp_atomic_wait_backoff_impl<_Atp, typename decay<_Fn>::type> __backoff_fn = {__a, __test_fn};
1547 return __libcpp_thread_poll_with_backoff(__test_fn, __backoff_fn);
Olivier Giroux161e6e82020-02-18 09:58:34 -05001548}
1549
1550#else // _LIBCPP_HAS_NO_PLATFORM_WAIT
1551
1552template <class _Tp>
1553_LIBCPP_INLINE_VISIBILITY void __cxx_atomic_notify_all(__cxx_atomic_impl<_Tp> const volatile*) { }
1554template <class _Tp>
1555_LIBCPP_INLINE_VISIBILITY void __cxx_atomic_notify_one(__cxx_atomic_impl<_Tp> const volatile*) { }
1556template <class _Atp, class _Fn>
1557_LIBCPP_INLINE_VISIBILITY bool __cxx_atomic_wait(_Atp*, _Fn && __test_fn)
1558{
1559 return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy());
1560}
1561
1562#endif // _LIBCPP_HAS_NO_PLATFORM_WAIT
1563
1564template <class _Atp, class _Tp>
Louis Dionne08cd5cb2020-02-24 11:39:48 -05001565struct __cxx_atomic_wait_test_fn_impl {
1566 _Atp* __a;
1567 _Tp __val;
1568 memory_order __order;
1569 _LIBCPP_INLINE_VISIBILITY bool operator()() const
1570 {
1571 return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__a, __order), __val);
1572 }
1573};
1574
1575template <class _Atp, class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001576_LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05001577_LIBCPP_INLINE_VISIBILITY bool __cxx_atomic_wait(_Atp* __a, _Tp const __val, memory_order __order)
1578{
Louis Dionne08cd5cb2020-02-24 11:39:48 -05001579 __cxx_atomic_wait_test_fn_impl<_Atp, _Tp> __test_fn = {__a, __val, __order};
Olivier Giroux161e6e82020-02-18 09:58:34 -05001580 return __cxx_atomic_wait(__a, __test_fn);
1581}
1582
Howard Hinnant138f5922010-12-07 20:46:14 +00001583// general atomic<T>
1584
1585template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value>
1586struct __atomic_base // false
1587{
Davide Italiano011f80a2019-03-05 18:40:49 +00001588 mutable __cxx_atomic_impl<_Tp> __a_;
Howard Hinnant138f5922010-12-07 20:46:14 +00001589
JF Bastienfdb42c22016-03-25 15:48:21 +00001590#if defined(__cpp_lib_atomic_is_always_lock_free)
1591 static _LIBCPP_CONSTEXPR bool is_always_lock_free = __atomic_always_lock_free(sizeof(__a_), 0);
1592#endif
1593
Howard Hinnant138f5922010-12-07 20:46:14 +00001594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001595 bool is_lock_free() const volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001596 {return __cxx_atomic_is_lock_free(sizeof(_Tp));}
Howard Hinnant138f5922010-12-07 20:46:14 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001598 bool is_lock_free() const _NOEXCEPT
Eric Fiselier3fd22c22015-06-13 00:23:07 +00001599 {return static_cast<__atomic_base const volatile*>(this)->is_lock_free();}
Howard Hinnant138f5922010-12-07 20:46:14 +00001600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001601 void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001602 _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
Davide Italiano011f80a2019-03-05 18:40:49 +00001603 {__cxx_atomic_store(&__a_, __d, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001605 void store(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001606 _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
Davide Italiano011f80a2019-03-05 18:40:49 +00001607 {__cxx_atomic_store(&__a_, __d, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001609 _Tp load(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001610 _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
Davide Italiano011f80a2019-03-05 18:40:49 +00001611 {return __cxx_atomic_load(&__a_, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001613 _Tp load(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001614 _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
Davide Italiano011f80a2019-03-05 18:40:49 +00001615 {return __cxx_atomic_load(&__a_, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001617 operator _Tp() const volatile _NOEXCEPT {return load();}
Howard Hinnant138f5922010-12-07 20:46:14 +00001618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001619 operator _Tp() const _NOEXCEPT {return load();}
Howard Hinnant138f5922010-12-07 20:46:14 +00001620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001621 _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001622 {return __cxx_atomic_exchange(&__a_, __d, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001624 _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001625 {return __cxx_atomic_exchange(&__a_, __d, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001626 _LIBCPP_INLINE_VISIBILITY
1627 bool compare_exchange_weak(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001628 memory_order __s, memory_order __f) volatile _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001629 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Davide Italiano011f80a2019-03-05 18:40:49 +00001630 {return __cxx_atomic_compare_exchange_weak(&__a_, &__e, __d, __s, __f);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001631 _LIBCPP_INLINE_VISIBILITY
1632 bool compare_exchange_weak(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001633 memory_order __s, memory_order __f) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001634 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Davide Italiano011f80a2019-03-05 18:40:49 +00001635 {return __cxx_atomic_compare_exchange_weak(&__a_, &__e, __d, __s, __f);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001636 _LIBCPP_INLINE_VISIBILITY
1637 bool compare_exchange_strong(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001638 memory_order __s, memory_order __f) volatile _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001639 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Davide Italiano011f80a2019-03-05 18:40:49 +00001640 {return __cxx_atomic_compare_exchange_strong(&__a_, &__e, __d, __s, __f);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001641 _LIBCPP_INLINE_VISIBILITY
1642 bool compare_exchange_strong(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001643 memory_order __s, memory_order __f) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001644 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Davide Italiano011f80a2019-03-05 18:40:49 +00001645 {return __cxx_atomic_compare_exchange_strong(&__a_, &__e, __d, __s, __f);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001646 _LIBCPP_INLINE_VISIBILITY
1647 bool compare_exchange_weak(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001648 memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001649 {return __cxx_atomic_compare_exchange_weak(&__a_, &__e, __d, __m, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001650 _LIBCPP_INLINE_VISIBILITY
1651 bool compare_exchange_weak(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001652 memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001653 {return __cxx_atomic_compare_exchange_weak(&__a_, &__e, __d, __m, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001654 _LIBCPP_INLINE_VISIBILITY
1655 bool compare_exchange_strong(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001656 memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001657 {return __cxx_atomic_compare_exchange_strong(&__a_, &__e, __d, __m, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001658 _LIBCPP_INLINE_VISIBILITY
1659 bool compare_exchange_strong(_Tp& __e, _Tp __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00001660 memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001661 {return __cxx_atomic_compare_exchange_strong(&__a_, &__e, __d, __m, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001662
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001663 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
Olivier Giroux161e6e82020-02-18 09:58:34 -05001664 {__cxx_atomic_wait(&__a_, __v, __m);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001665 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT
Olivier Giroux161e6e82020-02-18 09:58:34 -05001666 {__cxx_atomic_wait(&__a_, __v, __m);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001667 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY void notify_one() volatile _NOEXCEPT
Olivier Giroux161e6e82020-02-18 09:58:34 -05001668 {__cxx_atomic_notify_one(&__a_);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001669 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY void notify_one() _NOEXCEPT
Olivier Giroux161e6e82020-02-18 09:58:34 -05001670 {__cxx_atomic_notify_one(&__a_);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001671 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY void notify_all() volatile _NOEXCEPT
Olivier Giroux161e6e82020-02-18 09:58:34 -05001672 {__cxx_atomic_notify_all(&__a_);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05001673 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY void notify_all() _NOEXCEPT
Olivier Giroux161e6e82020-02-18 09:58:34 -05001674 {__cxx_atomic_notify_all(&__a_);}
1675
Howard Hinnant138f5922010-12-07 20:46:14 +00001676 _LIBCPP_INLINE_VISIBILITY
Davide Italiano011f80a2019-03-05 18:40:49 +00001677 __atomic_base() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant3d284222013-05-02 20:18:43 +00001678
Davide Italiano011f80a2019-03-05 18:40:49 +00001679 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1680 __atomic_base(_Tp __d) _NOEXCEPT : __a_(__d) {}
1681
Eric Fiselier2d8515f2017-01-06 20:58:25 +00001682#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant138f5922010-12-07 20:46:14 +00001683 __atomic_base(const __atomic_base&) = delete;
Eric Fiselier2d8515f2017-01-06 20:58:25 +00001684#else
Howard Hinnant6ac60f82010-12-08 17:20:28 +00001685private:
Olivier Giroux161e6e82020-02-18 09:58:34 -05001686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6ac60f82010-12-08 17:20:28 +00001687 __atomic_base(const __atomic_base&);
Eric Fiselier2d8515f2017-01-06 20:58:25 +00001688#endif
Howard Hinnant138f5922010-12-07 20:46:14 +00001689};
1690
JF Bastienfdb42c22016-03-25 15:48:21 +00001691#if defined(__cpp_lib_atomic_is_always_lock_free)
1692template <class _Tp, bool __b>
1693_LIBCPP_CONSTEXPR bool __atomic_base<_Tp, __b>::is_always_lock_free;
1694#endif
1695
Howard Hinnant138f5922010-12-07 20:46:14 +00001696// atomic<Integral>
1697
1698template <class _Tp>
1699struct __atomic_base<_Tp, true>
1700 : public __atomic_base<_Tp, false>
1701{
1702 typedef __atomic_base<_Tp, false> __base;
1703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3d284222013-05-02 20:18:43 +00001704 __atomic_base() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant138f5922010-12-07 20:46:14 +00001705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001706 _LIBCPP_CONSTEXPR __atomic_base(_Tp __d) _NOEXCEPT : __base(__d) {}
Howard Hinnant138f5922010-12-07 20:46:14 +00001707
1708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001709 _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001710 {return __cxx_atomic_fetch_add(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001712 _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001713 {return __cxx_atomic_fetch_add(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001715 _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001716 {return __cxx_atomic_fetch_sub(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001718 _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001719 {return __cxx_atomic_fetch_sub(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001721 _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001722 {return __cxx_atomic_fetch_and(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001724 _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001725 {return __cxx_atomic_fetch_and(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001727 _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001728 {return __cxx_atomic_fetch_or(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001730 _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001731 {return __cxx_atomic_fetch_or(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001733 _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001734 {return __cxx_atomic_fetch_xor(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001736 _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001737 {return __cxx_atomic_fetch_xor(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001738
1739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001740 _Tp operator++(int) volatile _NOEXCEPT {return fetch_add(_Tp(1));}
Howard Hinnant138f5922010-12-07 20:46:14 +00001741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001742 _Tp operator++(int) _NOEXCEPT {return fetch_add(_Tp(1));}
Howard Hinnant138f5922010-12-07 20:46:14 +00001743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001744 _Tp operator--(int) volatile _NOEXCEPT {return fetch_sub(_Tp(1));}
Howard Hinnant138f5922010-12-07 20:46:14 +00001745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001746 _Tp operator--(int) _NOEXCEPT {return fetch_sub(_Tp(1));}
Howard Hinnant138f5922010-12-07 20:46:14 +00001747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001748 _Tp operator++() volatile _NOEXCEPT {return fetch_add(_Tp(1)) + _Tp(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001750 _Tp operator++() _NOEXCEPT {return fetch_add(_Tp(1)) + _Tp(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001752 _Tp operator--() volatile _NOEXCEPT {return fetch_sub(_Tp(1)) - _Tp(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001754 _Tp operator--() _NOEXCEPT {return fetch_sub(_Tp(1)) - _Tp(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001756 _Tp operator+=(_Tp __op) volatile _NOEXCEPT {return fetch_add(__op) + __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001758 _Tp operator+=(_Tp __op) _NOEXCEPT {return fetch_add(__op) + __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001760 _Tp operator-=(_Tp __op) volatile _NOEXCEPT {return fetch_sub(__op) - __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001762 _Tp operator-=(_Tp __op) _NOEXCEPT {return fetch_sub(__op) - __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001764 _Tp operator&=(_Tp __op) volatile _NOEXCEPT {return fetch_and(__op) & __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001766 _Tp operator&=(_Tp __op) _NOEXCEPT {return fetch_and(__op) & __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001768 _Tp operator|=(_Tp __op) volatile _NOEXCEPT {return fetch_or(__op) | __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001770 _Tp operator|=(_Tp __op) _NOEXCEPT {return fetch_or(__op) | __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001772 _Tp operator^=(_Tp __op) volatile _NOEXCEPT {return fetch_xor(__op) ^ __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001774 _Tp operator^=(_Tp __op) _NOEXCEPT {return fetch_xor(__op) ^ __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001775};
1776
1777// atomic<T>
1778
1779template <class _Tp>
1780struct atomic
1781 : public __atomic_base<_Tp>
1782{
1783 typedef __atomic_base<_Tp> __base;
Olivier Giroux161e6e82020-02-18 09:58:34 -05001784 typedef _Tp value_type;
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001785 typedef value_type difference_type;
Howard Hinnant138f5922010-12-07 20:46:14 +00001786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3d284222013-05-02 20:18:43 +00001787 atomic() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant138f5922010-12-07 20:46:14 +00001788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001789 _LIBCPP_CONSTEXPR atomic(_Tp __d) _NOEXCEPT : __base(__d) {}
Howard Hinnant96e4cd62010-12-07 23:24:41 +00001790
1791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001792 _Tp operator=(_Tp __d) volatile _NOEXCEPT
Howard Hinnant96e4cd62010-12-07 23:24:41 +00001793 {__base::store(__d); return __d;}
1794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001795 _Tp operator=(_Tp __d) _NOEXCEPT
Howard Hinnant96e4cd62010-12-07 23:24:41 +00001796 {__base::store(__d); return __d;}
Ruslan Arutyunyan033093f2021-02-01 10:12:09 -05001797
1798 atomic& operator=(const atomic&) = delete;
1799 atomic& operator=(const atomic&) volatile = delete;
Howard Hinnant138f5922010-12-07 20:46:14 +00001800};
1801
1802// atomic<T*>
1803
1804template <class _Tp>
1805struct atomic<_Tp*>
1806 : public __atomic_base<_Tp*>
1807{
Howard Hinnant96e4cd62010-12-07 23:24:41 +00001808 typedef __atomic_base<_Tp*> __base;
Olivier Giroux161e6e82020-02-18 09:58:34 -05001809 typedef _Tp* value_type;
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001810 typedef ptrdiff_t difference_type;
Howard Hinnant138f5922010-12-07 20:46:14 +00001811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3d284222013-05-02 20:18:43 +00001812 atomic() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant138f5922010-12-07 20:46:14 +00001813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001814 _LIBCPP_CONSTEXPR atomic(_Tp* __d) _NOEXCEPT : __base(__d) {}
Howard Hinnant138f5922010-12-07 20:46:14 +00001815
1816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001817 _Tp* operator=(_Tp* __d) volatile _NOEXCEPT
Howard Hinnant96e4cd62010-12-07 23:24:41 +00001818 {__base::store(__d); return __d;}
1819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001820 _Tp* operator=(_Tp* __d) _NOEXCEPT
Howard Hinnant96e4cd62010-12-07 23:24:41 +00001821 {__base::store(__d); return __d;}
1822
1823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00001824 _Tp* fetch_add(ptrdiff_t __op, memory_order __m = memory_order_seq_cst)
Howard Hinnanteee2c142012-04-11 20:14:21 +00001825 volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001826 {return __cxx_atomic_fetch_add(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001828 _Tp* fetch_add(ptrdiff_t __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001829 {return __cxx_atomic_fetch_add(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001830 _LIBCPP_INLINE_VISIBILITY
1831 _Tp* fetch_sub(ptrdiff_t __op, memory_order __m = memory_order_seq_cst)
Howard Hinnanteee2c142012-04-11 20:14:21 +00001832 volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001833 {return __cxx_atomic_fetch_sub(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001835 _Tp* fetch_sub(ptrdiff_t __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00001836 {return __cxx_atomic_fetch_sub(&this->__a_, __op, __m);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001837
1838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001839 _Tp* operator++(int) volatile _NOEXCEPT {return fetch_add(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001841 _Tp* operator++(int) _NOEXCEPT {return fetch_add(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001843 _Tp* operator--(int) volatile _NOEXCEPT {return fetch_sub(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001845 _Tp* operator--(int) _NOEXCEPT {return fetch_sub(1);}
Howard Hinnant138f5922010-12-07 20:46:14 +00001846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001847 _Tp* operator++() volatile _NOEXCEPT {return fetch_add(1) + 1;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001849 _Tp* operator++() _NOEXCEPT {return fetch_add(1) + 1;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001851 _Tp* operator--() volatile _NOEXCEPT {return fetch_sub(1) - 1;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001853 _Tp* operator--() _NOEXCEPT {return fetch_sub(1) - 1;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001855 _Tp* operator+=(ptrdiff_t __op) volatile _NOEXCEPT {return fetch_add(__op) + __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001857 _Tp* operator+=(ptrdiff_t __op) _NOEXCEPT {return fetch_add(__op) + __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001859 _Tp* operator-=(ptrdiff_t __op) volatile _NOEXCEPT {return fetch_sub(__op) - __op;}
Howard Hinnant138f5922010-12-07 20:46:14 +00001860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00001861 _Tp* operator-=(ptrdiff_t __op) _NOEXCEPT {return fetch_sub(__op) - __op;}
Ruslan Arutyunyan033093f2021-02-01 10:12:09 -05001862
1863 atomic& operator=(const atomic&) = delete;
1864 atomic& operator=(const atomic&) volatile = delete;
Howard Hinnant138f5922010-12-07 20:46:14 +00001865};
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001866
1867// atomic_is_lock_free
1868
1869template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001870_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001871bool
Howard Hinnanteee2c142012-04-11 20:14:21 +00001872atomic_is_lock_free(const volatile atomic<_Tp>* __o) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001873{
Howard Hinnant138f5922010-12-07 20:46:14 +00001874 return __o->is_lock_free();
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001875}
1876
1877template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001878_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001879bool
Howard Hinnanteee2c142012-04-11 20:14:21 +00001880atomic_is_lock_free(const atomic<_Tp>* __o) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001881{
Howard Hinnant138f5922010-12-07 20:46:14 +00001882 return __o->is_lock_free();
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001883}
1884
1885// atomic_init
1886
1887template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001888_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001889void
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001890atomic_init(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001891{
Davide Italiano011f80a2019-03-05 18:40:49 +00001892 __cxx_atomic_init(&__o->__a_, __d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001893}
1894
1895template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001896_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001897void
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001898atomic_init(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001899{
Davide Italiano011f80a2019-03-05 18:40:49 +00001900 __cxx_atomic_init(&__o->__a_, __d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001901}
1902
1903// atomic_store
1904
1905template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001906_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001907void
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001908atomic_store(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001909{
Howard Hinnant138f5922010-12-07 20:46:14 +00001910 __o->store(__d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001911}
1912
1913template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001914_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001915void
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001916atomic_store(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001917{
Howard Hinnant138f5922010-12-07 20:46:14 +00001918 __o->store(__d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001919}
1920
1921// atomic_store_explicit
1922
1923template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001924_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001925void
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001926atomic_store_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d, memory_order __m) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001927 _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001928{
Howard Hinnant138f5922010-12-07 20:46:14 +00001929 __o->store(__d, __m);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001930}
1931
1932template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001933_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001934void
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001935atomic_store_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d, memory_order __m) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001936 _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001937{
Howard Hinnant138f5922010-12-07 20:46:14 +00001938 __o->store(__d, __m);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001939}
1940
1941// atomic_load
1942
1943template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001944_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001945_Tp
Howard Hinnanteee2c142012-04-11 20:14:21 +00001946atomic_load(const volatile atomic<_Tp>* __o) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001947{
Howard Hinnant138f5922010-12-07 20:46:14 +00001948 return __o->load();
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001949}
1950
1951template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001952_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001953_Tp
Howard Hinnanteee2c142012-04-11 20:14:21 +00001954atomic_load(const atomic<_Tp>* __o) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001955{
Howard Hinnant138f5922010-12-07 20:46:14 +00001956 return __o->load();
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001957}
1958
1959// atomic_load_explicit
1960
1961template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001962_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001963_Tp
Howard Hinnanteee2c142012-04-11 20:14:21 +00001964atomic_load_explicit(const volatile atomic<_Tp>* __o, memory_order __m) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001965 _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001966{
Howard Hinnant138f5922010-12-07 20:46:14 +00001967 return __o->load(__m);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001968}
1969
1970template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001971_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001972_Tp
Howard Hinnanteee2c142012-04-11 20:14:21 +00001973atomic_load_explicit(const atomic<_Tp>* __o, memory_order __m) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00001974 _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001975{
Howard Hinnant138f5922010-12-07 20:46:14 +00001976 return __o->load(__m);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001977}
1978
1979// atomic_exchange
1980
1981template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001982_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001983_Tp
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001984atomic_exchange(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001985{
Howard Hinnant138f5922010-12-07 20:46:14 +00001986 return __o->exchange(__d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001987}
1988
1989template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00001990_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001991_Tp
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07001992atomic_exchange(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001993{
Howard Hinnant138f5922010-12-07 20:46:14 +00001994 return __o->exchange(__d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00001995}
1996
1997// atomic_exchange_explicit
1998
1999template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002000_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002001_Tp
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002002atomic_exchange_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d, memory_order __m) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002003{
Howard Hinnant138f5922010-12-07 20:46:14 +00002004 return __o->exchange(__d, __m);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002005}
2006
2007template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002008_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002009_Tp
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002010atomic_exchange_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __d, memory_order __m) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002011{
Howard Hinnant138f5922010-12-07 20:46:14 +00002012 return __o->exchange(__d, __m);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002013}
2014
2015// atomic_compare_exchange_weak
2016
2017template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002018_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002019bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002020atomic_compare_exchange_weak(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002021{
Howard Hinnant138f5922010-12-07 20:46:14 +00002022 return __o->compare_exchange_weak(*__e, __d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002023}
2024
2025template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002026_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002027bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002028atomic_compare_exchange_weak(atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002029{
Howard Hinnant138f5922010-12-07 20:46:14 +00002030 return __o->compare_exchange_weak(*__e, __d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002031}
2032
2033// atomic_compare_exchange_strong
2034
2035template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002036_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002037bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002038atomic_compare_exchange_strong(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002039{
Howard Hinnant138f5922010-12-07 20:46:14 +00002040 return __o->compare_exchange_strong(*__e, __d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002041}
2042
2043template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002044_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002045bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002046atomic_compare_exchange_strong(atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e, typename atomic<_Tp>::value_type __d) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002047{
Howard Hinnant138f5922010-12-07 20:46:14 +00002048 return __o->compare_exchange_strong(*__e, __d);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002049}
2050
2051// atomic_compare_exchange_weak_explicit
2052
2053template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002054_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002055bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002056atomic_compare_exchange_weak_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e,
2057 typename atomic<_Tp>::value_type __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00002058 memory_order __s, memory_order __f) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00002059 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002060{
Howard Hinnant138f5922010-12-07 20:46:14 +00002061 return __o->compare_exchange_weak(*__e, __d, __s, __f);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002062}
2063
2064template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002065_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002066bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002067atomic_compare_exchange_weak_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e, typename atomic<_Tp>::value_type __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00002068 memory_order __s, memory_order __f) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00002069 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002070{
Howard Hinnant138f5922010-12-07 20:46:14 +00002071 return __o->compare_exchange_weak(*__e, __d, __s, __f);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002072}
2073
2074// atomic_compare_exchange_strong_explicit
2075
2076template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002077_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002078bool
2079atomic_compare_exchange_strong_explicit(volatile atomic<_Tp>* __o,
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002080 typename atomic<_Tp>::value_type* __e, typename atomic<_Tp>::value_type __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00002081 memory_order __s, memory_order __f) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00002082 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002083{
Howard Hinnant138f5922010-12-07 20:46:14 +00002084 return __o->compare_exchange_strong(*__e, __d, __s, __f);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002085}
2086
2087template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002088_LIBCPP_INLINE_VISIBILITY
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002089bool
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002090atomic_compare_exchange_strong_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type* __e,
2091 typename atomic<_Tp>::value_type __d,
Howard Hinnanteee2c142012-04-11 20:14:21 +00002092 memory_order __s, memory_order __f) _NOEXCEPT
Eric Fiselierb5ab51d2017-01-13 23:45:39 +00002093 _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f)
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002094{
Howard Hinnant138f5922010-12-07 20:46:14 +00002095 return __o->compare_exchange_strong(*__e, __d, __s, __f);
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002096}
2097
Olivier Giroux161e6e82020-02-18 09:58:34 -05002098// atomic_wait
2099
2100template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002101_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002102void atomic_wait(const volatile atomic<_Tp>* __o,
2103 typename atomic<_Tp>::value_type __v) _NOEXCEPT
2104{
2105 return __o->wait(__v);
2106}
2107
2108template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002109_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002110void atomic_wait(const atomic<_Tp>* __o,
2111 typename atomic<_Tp>::value_type __v) _NOEXCEPT
2112{
2113 return __o->wait(__v);
2114}
2115
2116// atomic_wait_explicit
2117
2118template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002119_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002120void atomic_wait_explicit(const volatile atomic<_Tp>* __o,
2121 typename atomic<_Tp>::value_type __v,
2122 memory_order __m) _NOEXCEPT
2123 _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
2124{
2125 return __o->wait(__v, __m);
2126}
2127
2128template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002129_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002130void atomic_wait_explicit(const atomic<_Tp>* __o,
2131 typename atomic<_Tp>::value_type __v,
2132 memory_order __m) _NOEXCEPT
2133 _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m)
2134{
2135 return __o->wait(__v, __m);
2136}
2137
2138// atomic_notify_one
2139
2140template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002141_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002142void atomic_notify_one(volatile atomic<_Tp>* __o) _NOEXCEPT
2143{
2144 __o->notify_one();
2145}
2146template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002147_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002148void atomic_notify_one(atomic<_Tp>* __o) _NOEXCEPT
2149{
2150 __o->notify_one();
2151}
2152
2153// atomic_notify_one
2154
2155template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002156_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002157void atomic_notify_all(volatile atomic<_Tp>* __o) _NOEXCEPT
2158{
2159 __o->notify_all();
2160}
2161template <class _Tp>
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002162_LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002163void atomic_notify_all(atomic<_Tp>* __o) _NOEXCEPT
2164{
2165 __o->notify_all();
2166}
2167
Howard Hinnant138f5922010-12-07 20:46:14 +00002168// atomic_fetch_add
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002169
2170template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002171_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002172typename enable_if
2173<
Olivier Girouxdf485172020-09-11 12:13:35 -07002174 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002175 _Tp
2176>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002177atomic_fetch_add(volatile atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op) _NOEXCEPT
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002178{
Howard Hinnant138f5922010-12-07 20:46:14 +00002179 return __o->fetch_add(__op);
2180}
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002181
Howard Hinnant138f5922010-12-07 20:46:14 +00002182template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002183_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002184typename enable_if
2185<
Olivier Girouxdf485172020-09-11 12:13:35 -07002186 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002187 _Tp
2188>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002189atomic_fetch_add(atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002190{
2191 return __o->fetch_add(__op);
2192}
2193
Olivier Girouxdf485172020-09-11 12:13:35 -07002194template <class _Tp>
2195_LIBCPP_INLINE_VISIBILITY
Louis Dionneb39adc52020-11-04 14:07:59 -05002196_Tp*
Olivier Girouxdf485172020-09-11 12:13:35 -07002197atomic_fetch_add(volatile atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op) _NOEXCEPT
2198{
2199 return __o->fetch_add(__op);
2200}
2201
2202template <class _Tp>
2203_LIBCPP_INLINE_VISIBILITY
Louis Dionneb39adc52020-11-04 14:07:59 -05002204_Tp*
Olivier Girouxdf485172020-09-11 12:13:35 -07002205atomic_fetch_add(atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op) _NOEXCEPT
2206{
2207 return __o->fetch_add(__op);
2208}
2209
Howard Hinnant138f5922010-12-07 20:46:14 +00002210// atomic_fetch_add_explicit
2211
2212template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002213_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002214typename enable_if
2215<
Olivier Girouxdf485172020-09-11 12:13:35 -07002216 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002217 _Tp
2218>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002219atomic_fetch_add_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002220{
2221 return __o->fetch_add(__op, __m);
2222}
2223
2224template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002225_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002226typename enable_if
2227<
Olivier Girouxdf485172020-09-11 12:13:35 -07002228 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002229 _Tp
2230>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002231atomic_fetch_add_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002232{
2233 return __o->fetch_add(__op, __m);
2234}
2235
Olivier Girouxdf485172020-09-11 12:13:35 -07002236template <class _Tp>
2237_LIBCPP_INLINE_VISIBILITY
2238_Tp*
2239atomic_fetch_add_explicit(volatile atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op, memory_order __m) _NOEXCEPT
2240{
2241 return __o->fetch_add(__op, __m);
2242}
2243
2244template <class _Tp>
2245_LIBCPP_INLINE_VISIBILITY
2246_Tp*
2247atomic_fetch_add_explicit(atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op, memory_order __m) _NOEXCEPT
2248{
2249 return __o->fetch_add(__op, __m);
2250}
2251
Howard Hinnant138f5922010-12-07 20:46:14 +00002252// atomic_fetch_sub
2253
2254template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002255_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002256typename enable_if
2257<
Olivier Girouxdf485172020-09-11 12:13:35 -07002258 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002259 _Tp
2260>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002261atomic_fetch_sub(volatile atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002262{
2263 return __o->fetch_sub(__op);
2264}
2265
2266template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002267_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002268typename enable_if
2269<
Olivier Girouxdf485172020-09-11 12:13:35 -07002270 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002271 _Tp
2272>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002273atomic_fetch_sub(atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002274{
2275 return __o->fetch_sub(__op);
2276}
2277
Olivier Girouxdf485172020-09-11 12:13:35 -07002278template <class _Tp>
2279_LIBCPP_INLINE_VISIBILITY
2280_Tp*
2281atomic_fetch_sub(volatile atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op) _NOEXCEPT
2282{
2283 return __o->fetch_sub(__op);
2284}
2285
2286template <class _Tp>
2287_LIBCPP_INLINE_VISIBILITY
2288_Tp*
2289atomic_fetch_sub(atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op) _NOEXCEPT
2290{
2291 return __o->fetch_sub(__op);
2292}
2293
Howard Hinnant138f5922010-12-07 20:46:14 +00002294// atomic_fetch_sub_explicit
2295
2296template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002297_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002298typename enable_if
2299<
Olivier Girouxdf485172020-09-11 12:13:35 -07002300 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002301 _Tp
2302>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002303atomic_fetch_sub_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002304{
2305 return __o->fetch_sub(__op, __m);
2306}
2307
2308template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002309_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002310typename enable_if
2311<
Olivier Girouxdf485172020-09-11 12:13:35 -07002312 is_integral<_Tp>::value && !is_same<_Tp, bool>::value && !is_const<_Tp>::value,
Howard Hinnant138f5922010-12-07 20:46:14 +00002313 _Tp
2314>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002315atomic_fetch_sub_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::difference_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002316{
2317 return __o->fetch_sub(__op, __m);
2318}
2319
Olivier Girouxdf485172020-09-11 12:13:35 -07002320template <class _Tp>
2321_LIBCPP_INLINE_VISIBILITY
2322_Tp*
2323atomic_fetch_sub_explicit(volatile atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op, memory_order __m) _NOEXCEPT
2324{
2325 return __o->fetch_sub(__op, __m);
2326}
2327
2328template <class _Tp>
2329_LIBCPP_INLINE_VISIBILITY
2330_Tp*
2331atomic_fetch_sub_explicit(atomic<_Tp*>* __o, typename atomic<_Tp*>::difference_type __op, memory_order __m) _NOEXCEPT
2332{
2333 return __o->fetch_sub(__op, __m);
2334}
2335
Howard Hinnant138f5922010-12-07 20:46:14 +00002336// atomic_fetch_and
2337
2338template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002339_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002340typename enable_if
2341<
2342 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2343 _Tp
2344>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002345atomic_fetch_and(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002346{
2347 return __o->fetch_and(__op);
2348}
2349
2350template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002351_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002352typename enable_if
2353<
2354 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2355 _Tp
2356>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002357atomic_fetch_and(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002358{
2359 return __o->fetch_and(__op);
2360}
2361
2362// atomic_fetch_and_explicit
2363
2364template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002365_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002366typename enable_if
2367<
2368 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2369 _Tp
2370>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002371atomic_fetch_and_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002372{
2373 return __o->fetch_and(__op, __m);
2374}
2375
2376template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002377_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002378typename enable_if
2379<
2380 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2381 _Tp
2382>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002383atomic_fetch_and_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002384{
2385 return __o->fetch_and(__op, __m);
2386}
2387
2388// atomic_fetch_or
2389
2390template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002391_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002392typename enable_if
2393<
2394 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2395 _Tp
2396>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002397atomic_fetch_or(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002398{
2399 return __o->fetch_or(__op);
2400}
2401
2402template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002403_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002404typename enable_if
2405<
2406 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2407 _Tp
2408>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002409atomic_fetch_or(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002410{
2411 return __o->fetch_or(__op);
2412}
2413
2414// atomic_fetch_or_explicit
2415
2416template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002417_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002418typename enable_if
2419<
2420 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2421 _Tp
2422>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002423atomic_fetch_or_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002424{
2425 return __o->fetch_or(__op, __m);
2426}
2427
2428template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002429_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002430typename enable_if
2431<
2432 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2433 _Tp
2434>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002435atomic_fetch_or_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002436{
2437 return __o->fetch_or(__op, __m);
2438}
2439
2440// atomic_fetch_xor
2441
2442template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002443_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002444typename enable_if
2445<
2446 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2447 _Tp
2448>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002449atomic_fetch_xor(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002450{
2451 return __o->fetch_xor(__op);
2452}
2453
2454template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002455_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002456typename enable_if
2457<
2458 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2459 _Tp
2460>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002461atomic_fetch_xor(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002462{
2463 return __o->fetch_xor(__op);
2464}
2465
2466// atomic_fetch_xor_explicit
2467
2468template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002469_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002470typename enable_if
2471<
2472 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2473 _Tp
2474>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002475atomic_fetch_xor_explicit(volatile atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002476{
2477 return __o->fetch_xor(__op, __m);
2478}
2479
2480template <class _Tp>
Davide Italiano011f80a2019-03-05 18:40:49 +00002481_LIBCPP_INLINE_VISIBILITY
Howard Hinnant138f5922010-12-07 20:46:14 +00002482typename enable_if
2483<
2484 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
2485 _Tp
2486>::type
Olivier Girouxdf5bfa32020-09-09 10:00:09 -07002487atomic_fetch_xor_explicit(atomic<_Tp>* __o, typename atomic<_Tp>::value_type __op, memory_order __m) _NOEXCEPT
Howard Hinnant138f5922010-12-07 20:46:14 +00002488{
2489 return __o->fetch_xor(__op, __m);
2490}
Howard Hinnant7bfaeb82010-12-06 23:10:08 +00002491
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002492// flag type and operations
2493
2494typedef struct atomic_flag
2495{
Davide Italiano011f80a2019-03-05 18:40:49 +00002496 __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> __a_;
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002497
2498 _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002499 bool test(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
2500 {return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);}
2501 _LIBCPP_INLINE_VISIBILITY
2502 bool test(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
2503 {return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);}
2504
2505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00002506 bool test_and_set(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00002507 {return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);}
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00002509 bool test_and_set(memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00002510 {return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);}
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00002512 void clear(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00002513 {__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);}
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteee2c142012-04-11 20:14:21 +00002515 void clear(memory_order __m = memory_order_seq_cst) _NOEXCEPT
Davide Italiano011f80a2019-03-05 18:40:49 +00002516 {__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);}
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002517
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002518 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002519 void wait(bool __v, memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
2520 {__cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002521 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002522 void wait(bool __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT
2523 {__cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002524 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002525 void notify_one() volatile _NOEXCEPT
2526 {__cxx_atomic_notify_one(&__a_);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002527 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002528 void notify_one() _NOEXCEPT
2529 {__cxx_atomic_notify_one(&__a_);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002530 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002531 void notify_all() volatile _NOEXCEPT
2532 {__cxx_atomic_notify_all(&__a_);}
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002533 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -05002534 void notify_all() _NOEXCEPT
2535 {__cxx_atomic_notify_all(&__a_);}
2536
2537 _LIBCPP_INLINE_VISIBILITY
Davide Italiano011f80a2019-03-05 18:40:49 +00002538 atomic_flag() _NOEXCEPT _LIBCPP_DEFAULT
Howard Hinnant3d284222013-05-02 20:18:43 +00002539
Marshall Clowcf990752018-04-25 14:27:29 +00002540 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselierfec26a92016-05-03 02:12:26 +00002541 atomic_flag(bool __b) _NOEXCEPT : __a_(__b) {} // EXTENSION
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002542
Eric Fiselier2d8515f2017-01-06 20:58:25 +00002543#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002544 atomic_flag(const atomic_flag&) = delete;
2545 atomic_flag& operator=(const atomic_flag&) = delete;
2546 atomic_flag& operator=(const atomic_flag&) volatile = delete;
Eric Fiselier2d8515f2017-01-06 20:58:25 +00002547#else
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002548private:
Olivier Giroux161e6e82020-02-18 09:58:34 -05002549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002550 atomic_flag(const atomic_flag&);
Olivier Giroux161e6e82020-02-18 09:58:34 -05002551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002552 atomic_flag& operator=(const atomic_flag&);
Olivier Giroux161e6e82020-02-18 09:58:34 -05002553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002554 atomic_flag& operator=(const atomic_flag&) volatile;
Eric Fiselier2d8515f2017-01-06 20:58:25 +00002555#endif
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002556} atomic_flag;
2557
Olivier Giroux161e6e82020-02-18 09:58:34 -05002558
2559inline _LIBCPP_INLINE_VISIBILITY
2560bool
2561atomic_flag_test(const volatile atomic_flag* __o) _NOEXCEPT
2562{
2563 return __o->test();
2564}
2565
2566inline _LIBCPP_INLINE_VISIBILITY
2567bool
2568atomic_flag_test(const atomic_flag* __o) _NOEXCEPT
2569{
2570 return __o->test();
2571}
2572
2573inline _LIBCPP_INLINE_VISIBILITY
2574bool
2575atomic_flag_test_explicit(const volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
2576{
2577 return __o->test(__m);
2578}
2579
2580inline _LIBCPP_INLINE_VISIBILITY
2581bool
2582atomic_flag_test_explicit(const atomic_flag* __o, memory_order __m) _NOEXCEPT
2583{
2584 return __o->test(__m);
2585}
2586
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002587inline _LIBCPP_INLINE_VISIBILITY
2588bool
Howard Hinnanteee2c142012-04-11 20:14:21 +00002589atomic_flag_test_and_set(volatile atomic_flag* __o) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002590{
2591 return __o->test_and_set();
2592}
2593
2594inline _LIBCPP_INLINE_VISIBILITY
2595bool
Howard Hinnanteee2c142012-04-11 20:14:21 +00002596atomic_flag_test_and_set(atomic_flag* __o) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002597{
2598 return __o->test_and_set();
2599}
2600
2601inline _LIBCPP_INLINE_VISIBILITY
2602bool
Howard Hinnanteee2c142012-04-11 20:14:21 +00002603atomic_flag_test_and_set_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002604{
2605 return __o->test_and_set(__m);
2606}
2607
2608inline _LIBCPP_INLINE_VISIBILITY
2609bool
Howard Hinnanteee2c142012-04-11 20:14:21 +00002610atomic_flag_test_and_set_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002611{
2612 return __o->test_and_set(__m);
2613}
2614
2615inline _LIBCPP_INLINE_VISIBILITY
2616void
Howard Hinnanteee2c142012-04-11 20:14:21 +00002617atomic_flag_clear(volatile atomic_flag* __o) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002618{
2619 __o->clear();
2620}
2621
2622inline _LIBCPP_INLINE_VISIBILITY
2623void
Howard Hinnanteee2c142012-04-11 20:14:21 +00002624atomic_flag_clear(atomic_flag* __o) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002625{
2626 __o->clear();
2627}
2628
2629inline _LIBCPP_INLINE_VISIBILITY
2630void
Howard Hinnanteee2c142012-04-11 20:14:21 +00002631atomic_flag_clear_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002632{
2633 __o->clear(__m);
2634}
2635
2636inline _LIBCPP_INLINE_VISIBILITY
2637void
Howard Hinnanteee2c142012-04-11 20:14:21 +00002638atomic_flag_clear_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002639{
2640 __o->clear(__m);
2641}
2642
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002643inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002644void
2645atomic_flag_wait(const volatile atomic_flag* __o, bool __v) _NOEXCEPT
2646{
2647 __o->wait(__v);
2648}
2649
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002650inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002651void
2652atomic_flag_wait(const atomic_flag* __o, bool __v) _NOEXCEPT
2653{
2654 __o->wait(__v);
2655}
2656
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002657inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002658void
2659atomic_flag_wait_explicit(const volatile atomic_flag* __o,
2660 bool __v, memory_order __m) _NOEXCEPT
2661{
2662 __o->wait(__v, __m);
2663}
2664
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002665inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002666void
2667atomic_flag_wait_explicit(const atomic_flag* __o,
2668 bool __v, memory_order __m) _NOEXCEPT
2669{
2670 __o->wait(__v, __m);
2671}
2672
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002673inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002674void
2675atomic_flag_notify_one(volatile atomic_flag* __o) _NOEXCEPT
2676{
2677 __o->notify_one();
2678}
2679
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002680inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002681void
2682atomic_flag_notify_one(atomic_flag* __o) _NOEXCEPT
2683{
2684 __o->notify_one();
2685}
2686
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002687inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002688void
2689atomic_flag_notify_all(volatile atomic_flag* __o) _NOEXCEPT
2690{
2691 __o->notify_all();
2692}
2693
Louis Dionne3dde4ab2020-02-24 10:09:29 -05002694inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_SYNC
Olivier Giroux161e6e82020-02-18 09:58:34 -05002695void
2696atomic_flag_notify_all(atomic_flag* __o) _NOEXCEPT
2697{
2698 __o->notify_all();
2699}
2700
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002701// fences
2702
2703inline _LIBCPP_INLINE_VISIBILITY
2704void
Howard Hinnanteee2c142012-04-11 20:14:21 +00002705atomic_thread_fence(memory_order __m) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002706{
Davide Italiano011f80a2019-03-05 18:40:49 +00002707 __cxx_atomic_thread_fence(__m);
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002708}
2709
2710inline _LIBCPP_INLINE_VISIBILITY
2711void
Howard Hinnanteee2c142012-04-11 20:14:21 +00002712atomic_signal_fence(memory_order __m) _NOEXCEPT
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002713{
Davide Italiano011f80a2019-03-05 18:40:49 +00002714 __cxx_atomic_signal_fence(__m);
Howard Hinnant6ac60f82010-12-08 17:20:28 +00002715}
2716
Howard Hinnant96e4cd62010-12-07 23:24:41 +00002717// Atomics for standard typedef types
2718
Howard Hinnantf0af8d92013-01-04 18:58:50 +00002719typedef atomic<bool> atomic_bool;
Howard Hinnant96e4cd62010-12-07 23:24:41 +00002720typedef atomic<char> atomic_char;
2721typedef atomic<signed char> atomic_schar;
2722typedef atomic<unsigned char> atomic_uchar;
2723typedef atomic<short> atomic_short;
2724typedef atomic<unsigned short> atomic_ushort;
2725typedef atomic<int> atomic_int;
2726typedef atomic<unsigned int> atomic_uint;
2727typedef atomic<long> atomic_long;
2728typedef atomic<unsigned long> atomic_ulong;
2729typedef atomic<long long> atomic_llong;
2730typedef atomic<unsigned long long> atomic_ullong;
Marek Kurdej91bebaa2020-11-24 21:07:06 +01002731#ifndef _LIBCPP_NO_HAS_CHAR8_T
2732typedef atomic<char8_t> atomic_char8_t;
2733#endif
Howard Hinnant96e4cd62010-12-07 23:24:41 +00002734typedef atomic<char16_t> atomic_char16_t;
2735typedef atomic<char32_t> atomic_char32_t;
2736typedef atomic<wchar_t> atomic_wchar_t;
2737
2738typedef atomic<int_least8_t> atomic_int_least8_t;
2739typedef atomic<uint_least8_t> atomic_uint_least8_t;
2740typedef atomic<int_least16_t> atomic_int_least16_t;
2741typedef atomic<uint_least16_t> atomic_uint_least16_t;
2742typedef atomic<int_least32_t> atomic_int_least32_t;
2743typedef atomic<uint_least32_t> atomic_uint_least32_t;
2744typedef atomic<int_least64_t> atomic_int_least64_t;
2745typedef atomic<uint_least64_t> atomic_uint_least64_t;
2746
2747typedef atomic<int_fast8_t> atomic_int_fast8_t;
2748typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
2749typedef atomic<int_fast16_t> atomic_int_fast16_t;
2750typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
2751typedef atomic<int_fast32_t> atomic_int_fast32_t;
2752typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
2753typedef atomic<int_fast64_t> atomic_int_fast64_t;
2754typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
2755
Marshall Clowf710afc2016-06-30 15:28:38 +00002756typedef atomic< int8_t> atomic_int8_t;
2757typedef atomic<uint8_t> atomic_uint8_t;
2758typedef atomic< int16_t> atomic_int16_t;
2759typedef atomic<uint16_t> atomic_uint16_t;
2760typedef atomic< int32_t> atomic_int32_t;
2761typedef atomic<uint32_t> atomic_uint32_t;
2762typedef atomic< int64_t> atomic_int64_t;
2763typedef atomic<uint64_t> atomic_uint64_t;
2764
Howard Hinnant96e4cd62010-12-07 23:24:41 +00002765typedef atomic<intptr_t> atomic_intptr_t;
2766typedef atomic<uintptr_t> atomic_uintptr_t;
2767typedef atomic<size_t> atomic_size_t;
2768typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
2769typedef atomic<intmax_t> atomic_intmax_t;
2770typedef atomic<uintmax_t> atomic_uintmax_t;
2771
Olivier Giroux161e6e82020-02-18 09:58:34 -05002772// atomic_*_lock_free : prefer the contention type most highly, then the largest lock-free type
2773
2774#ifdef __cpp_lib_atomic_is_always_lock_free
2775# define _LIBCPP_CONTENTION_LOCK_FREE __atomic_always_lock_free(sizeof(__cxx_contention_t), 0)
2776#else
2777# define _LIBCPP_CONTENTION_LOCK_FREE false
2778#endif
2779
2780#if ATOMIC_LLONG_LOCK_FREE == 2
2781typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, long long>::type __libcpp_signed_lock_free;
2782typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned long long>::type __libcpp_unsigned_lock_free;
2783#elif ATOMIC_INT_LOCK_FREE == 2
2784typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, int>::type __libcpp_signed_lock_free;
2785typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned int>::type __libcpp_unsigned_lock_free;
2786#elif ATOMIC_SHORT_LOCK_FREE == 2
2787typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, short>::type __libcpp_signed_lock_free;
2788typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned short>::type __libcpp_unsigned_lock_free;
2789#elif ATOMIC_CHAR_LOCK_FREE == 2
2790typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, char>::type __libcpp_signed_lock_free;
2791typedef conditional<_LIBCPP_CONTENTION_LOCK_FREE, __cxx_contention_t, unsigned char>::type __libcpp_unsigned_lock_free;
2792#else
2793 // No signed/unsigned lock-free types
2794#endif
2795
2796typedef atomic<__libcpp_signed_lock_free> atomic_signed_lock_free;
2797typedef atomic<__libcpp_unsigned_lock_free> atomic_unsigned_lock_free;
2798
Howard Hinnantf1f066a2010-09-29 21:20:03 +00002799#define ATOMIC_FLAG_INIT {false}
Howard Hinnant953c31d2010-10-04 18:52:54 +00002800#define ATOMIC_VAR_INIT(__v) {__v}
2801
Howard Hinnant71be7292010-09-27 21:17:38 +00002802_LIBCPP_END_NAMESPACE_STD
2803
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002804#endif // _LIBCPP_ATOMIC