atomic 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // 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
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_ATOMIC
  10. #define _LIBCPP_ATOMIC
  11. /*
  12. atomic synopsis
  13. namespace std
  14. {
  15. // feature test macro [version.syn]
  16. #define __cpp_lib_atomic_is_always_lock_free
  17. #define __cpp_lib_atomic_flag_test
  18. #define __cpp_lib_atomic_lock_free_type_aliases
  19. #define __cpp_lib_atomic_wait
  20. // order and consistency
  21. enum memory_order: unspecified // enum class in C++20
  22. {
  23. relaxed,
  24. consume, // load-consume
  25. acquire, // load-acquire
  26. release, // store-release
  27. acq_rel, // store-release load-acquire
  28. seq_cst // store-release load-acquire
  29. };
  30. inline constexpr auto memory_order_relaxed = memory_order::relaxed;
  31. inline constexpr auto memory_order_consume = memory_order::consume;
  32. inline constexpr auto memory_order_acquire = memory_order::acquire;
  33. inline constexpr auto memory_order_release = memory_order::release;
  34. inline constexpr auto memory_order_acq_rel = memory_order::acq_rel;
  35. inline constexpr auto memory_order_seq_cst = memory_order::seq_cst;
  36. template <class T> T kill_dependency(T y) noexcept;
  37. // lock-free property
  38. #define ATOMIC_BOOL_LOCK_FREE unspecified
  39. #define ATOMIC_CHAR_LOCK_FREE unspecified
  40. #define ATOMIC_CHAR8_T_LOCK_FREE unspecified // C++20
  41. #define ATOMIC_CHAR16_T_LOCK_FREE unspecified
  42. #define ATOMIC_CHAR32_T_LOCK_FREE unspecified
  43. #define ATOMIC_WCHAR_T_LOCK_FREE unspecified
  44. #define ATOMIC_SHORT_LOCK_FREE unspecified
  45. #define ATOMIC_INT_LOCK_FREE unspecified
  46. #define ATOMIC_LONG_LOCK_FREE unspecified
  47. #define ATOMIC_LLONG_LOCK_FREE unspecified
  48. #define ATOMIC_POINTER_LOCK_FREE unspecified
  49. template <class T>
  50. struct atomic
  51. {
  52. using value_type = T;
  53. static constexpr bool is_always_lock_free;
  54. bool is_lock_free() const volatile noexcept;
  55. bool is_lock_free() const noexcept;
  56. atomic() noexcept = default; // until C++20
  57. constexpr atomic() noexcept(is_nothrow_default_constructible_v<T>); // since C++20
  58. constexpr atomic(T desr) noexcept;
  59. atomic(const atomic&) = delete;
  60. atomic& operator=(const atomic&) = delete;
  61. atomic& operator=(const atomic&) volatile = delete;
  62. T load(memory_order m = memory_order_seq_cst) const volatile noexcept;
  63. T load(memory_order m = memory_order_seq_cst) const noexcept;
  64. operator T() const volatile noexcept;
  65. operator T() const noexcept;
  66. void store(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
  67. void store(T desr, memory_order m = memory_order_seq_cst) noexcept;
  68. T operator=(T) volatile noexcept;
  69. T operator=(T) noexcept;
  70. T exchange(T desr, memory_order m = memory_order_seq_cst) volatile noexcept;
  71. T exchange(T desr, memory_order m = memory_order_seq_cst) noexcept;
  72. bool compare_exchange_weak(T& expc, T desr,
  73. memory_order s, memory_order f) volatile noexcept;
  74. bool compare_exchange_weak(T& expc, T desr, memory_order s, memory_order f) noexcept;
  75. bool compare_exchange_strong(T& expc, T desr,
  76. memory_order s, memory_order f) volatile noexcept;
  77. bool compare_exchange_strong(T& expc, T desr,
  78. memory_order s, memory_order f) noexcept;
  79. bool compare_exchange_weak(T& expc, T desr,
  80. memory_order m = memory_order_seq_cst) volatile noexcept;
  81. bool compare_exchange_weak(T& expc, T desr,
  82. memory_order m = memory_order_seq_cst) noexcept;
  83. bool compare_exchange_strong(T& expc, T desr,
  84. memory_order m = memory_order_seq_cst) volatile noexcept;
  85. bool compare_exchange_strong(T& expc, T desr,
  86. memory_order m = memory_order_seq_cst) noexcept;
  87. void wait(T, memory_order = memory_order::seq_cst) const volatile noexcept;
  88. void wait(T, memory_order = memory_order::seq_cst) const noexcept;
  89. void notify_one() volatile noexcept;
  90. void notify_one() noexcept;
  91. void notify_all() volatile noexcept;
  92. void notify_all() noexcept;
  93. };
  94. template <>
  95. struct atomic<integral>
  96. {
  97. using value_type = integral;
  98. using difference_type = value_type;
  99. static constexpr bool is_always_lock_free;
  100. bool is_lock_free() const volatile noexcept;
  101. bool is_lock_free() const noexcept;
  102. atomic() noexcept = default;
  103. constexpr atomic(integral desr) noexcept;
  104. atomic(const atomic&) = delete;
  105. atomic& operator=(const atomic&) = delete;
  106. atomic& operator=(const atomic&) volatile = delete;
  107. integral load(memory_order m = memory_order_seq_cst) const volatile noexcept;
  108. integral load(memory_order m = memory_order_seq_cst) const noexcept;
  109. operator integral() const volatile noexcept;
  110. operator integral() const noexcept;
  111. void store(integral desr, memory_order m = memory_order_seq_cst) volatile noexcept;
  112. void store(integral desr, memory_order m = memory_order_seq_cst) noexcept;
  113. integral operator=(integral desr) volatile noexcept;
  114. integral operator=(integral desr) noexcept;
  115. integral exchange(integral desr,
  116. memory_order m = memory_order_seq_cst) volatile noexcept;
  117. integral exchange(integral desr, memory_order m = memory_order_seq_cst) noexcept;
  118. bool compare_exchange_weak(integral& expc, integral desr,
  119. memory_order s, memory_order f) volatile noexcept;
  120. bool compare_exchange_weak(integral& expc, integral desr,
  121. memory_order s, memory_order f) noexcept;
  122. bool compare_exchange_strong(integral& expc, integral desr,
  123. memory_order s, memory_order f) volatile noexcept;
  124. bool compare_exchange_strong(integral& expc, integral desr,
  125. memory_order s, memory_order f) noexcept;
  126. bool compare_exchange_weak(integral& expc, integral desr,
  127. memory_order m = memory_order_seq_cst) volatile noexcept;
  128. bool compare_exchange_weak(integral& expc, integral desr,
  129. memory_order m = memory_order_seq_cst) noexcept;
  130. bool compare_exchange_strong(integral& expc, integral desr,
  131. memory_order m = memory_order_seq_cst) volatile noexcept;
  132. bool compare_exchange_strong(integral& expc, integral desr,
  133. memory_order m = memory_order_seq_cst) noexcept;
  134. integral fetch_add(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
  135. integral fetch_add(integral op, memory_order m = memory_order_seq_cst) noexcept;
  136. integral fetch_sub(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
  137. integral fetch_sub(integral op, memory_order m = memory_order_seq_cst) noexcept;
  138. integral fetch_and(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
  139. integral fetch_and(integral op, memory_order m = memory_order_seq_cst) noexcept;
  140. integral fetch_or(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
  141. integral fetch_or(integral op, memory_order m = memory_order_seq_cst) noexcept;
  142. integral fetch_xor(integral op, memory_order m = memory_order_seq_cst) volatile noexcept;
  143. integral fetch_xor(integral op, memory_order m = memory_order_seq_cst) noexcept;
  144. integral operator++(int) volatile noexcept;
  145. integral operator++(int) noexcept;
  146. integral operator--(int) volatile noexcept;
  147. integral operator--(int) noexcept;
  148. integral operator++() volatile noexcept;
  149. integral operator++() noexcept;
  150. integral operator--() volatile noexcept;
  151. integral operator--() noexcept;
  152. integral operator+=(integral op) volatile noexcept;
  153. integral operator+=(integral op) noexcept;
  154. integral operator-=(integral op) volatile noexcept;
  155. integral operator-=(integral op) noexcept;
  156. integral operator&=(integral op) volatile noexcept;
  157. integral operator&=(integral op) noexcept;
  158. integral operator|=(integral op) volatile noexcept;
  159. integral operator|=(integral op) noexcept;
  160. integral operator^=(integral op) volatile noexcept;
  161. integral operator^=(integral op) noexcept;
  162. void wait(integral, memory_order = memory_order::seq_cst) const volatile noexcept;
  163. void wait(integral, memory_order = memory_order::seq_cst) const noexcept;
  164. void notify_one() volatile noexcept;
  165. void notify_one() noexcept;
  166. void notify_all() volatile noexcept;
  167. void notify_all() noexcept;
  168. };
  169. template <class T>
  170. struct atomic<T*>
  171. {
  172. using value_type = T*;
  173. using difference_type = ptrdiff_t;
  174. static constexpr bool is_always_lock_free;
  175. bool is_lock_free() const volatile noexcept;
  176. bool is_lock_free() const noexcept;
  177. atomic() noexcept = default; // until C++20
  178. constexpr atomic() noexcept; // since C++20
  179. constexpr atomic(T* desr) noexcept;
  180. atomic(const atomic&) = delete;
  181. atomic& operator=(const atomic&) = delete;
  182. atomic& operator=(const atomic&) volatile = delete;
  183. T* load(memory_order m = memory_order_seq_cst) const volatile noexcept;
  184. T* load(memory_order m = memory_order_seq_cst) const noexcept;
  185. operator T*() const volatile noexcept;
  186. operator T*() const noexcept;
  187. void store(T* desr, memory_order m = memory_order_seq_cst) volatile noexcept;
  188. void store(T* desr, memory_order m = memory_order_seq_cst) noexcept;
  189. T* operator=(T*) volatile noexcept;
  190. T* operator=(T*) noexcept;
  191. T* exchange(T* desr, memory_order m = memory_order_seq_cst) volatile noexcept;
  192. T* exchange(T* desr, memory_order m = memory_order_seq_cst) noexcept;
  193. bool compare_exchange_weak(T*& expc, T* desr,
  194. memory_order s, memory_order f) volatile noexcept;
  195. bool compare_exchange_weak(T*& expc, T* desr,
  196. memory_order s, memory_order f) noexcept;
  197. bool compare_exchange_strong(T*& expc, T* desr,
  198. memory_order s, memory_order f) volatile noexcept;
  199. bool compare_exchange_strong(T*& expc, T* desr,
  200. memory_order s, memory_order f) noexcept;
  201. bool compare_exchange_weak(T*& expc, T* desr,
  202. memory_order m = memory_order_seq_cst) volatile noexcept;
  203. bool compare_exchange_weak(T*& expc, T* desr,
  204. memory_order m = memory_order_seq_cst) noexcept;
  205. bool compare_exchange_strong(T*& expc, T* desr,
  206. memory_order m = memory_order_seq_cst) volatile noexcept;
  207. bool compare_exchange_strong(T*& expc, T* desr,
  208. memory_order m = memory_order_seq_cst) noexcept;
  209. T* fetch_add(ptrdiff_t op, memory_order m = memory_order_seq_cst) volatile noexcept;
  210. T* fetch_add(ptrdiff_t op, memory_order m = memory_order_seq_cst) noexcept;
  211. T* fetch_sub(ptrdiff_t op, memory_order m = memory_order_seq_cst) volatile noexcept;
  212. T* fetch_sub(ptrdiff_t op, memory_order m = memory_order_seq_cst) noexcept;
  213. T* operator++(int) volatile noexcept;
  214. T* operator++(int) noexcept;
  215. T* operator--(int) volatile noexcept;
  216. T* operator--(int) noexcept;
  217. T* operator++() volatile noexcept;
  218. T* operator++() noexcept;
  219. T* operator--() volatile noexcept;
  220. T* operator--() noexcept;
  221. T* operator+=(ptrdiff_t op) volatile noexcept;
  222. T* operator+=(ptrdiff_t op) noexcept;
  223. T* operator-=(ptrdiff_t op) volatile noexcept;
  224. T* operator-=(ptrdiff_t op) noexcept;
  225. void wait(T*, memory_order = memory_order::seq_cst) const volatile noexcept;
  226. void wait(T*, memory_order = memory_order::seq_cst) const noexcept;
  227. void notify_one() volatile noexcept;
  228. void notify_one() noexcept;
  229. void notify_all() volatile noexcept;
  230. void notify_all() noexcept;
  231. };
  232. // [atomics.nonmembers], non-member functions
  233. template<class T>
  234. bool atomic_is_lock_free(const volatile atomic<T>*) noexcept;
  235. template<class T>
  236. bool atomic_is_lock_free(const atomic<T>*) noexcept;
  237. template<class T>
  238. void atomic_store(volatile atomic<T>*, atomic<T>::value_type) noexcept;
  239. template<class T>
  240. void atomic_store(atomic<T>*, atomic<T>::value_type) noexcept;
  241. template<class T>
  242. void atomic_store_explicit(volatile atomic<T>*, atomic<T>::value_type,
  243. memory_order) noexcept;
  244. template<class T>
  245. void atomic_store_explicit(atomic<T>*, atomic<T>::value_type,
  246. memory_order) noexcept;
  247. template<class T>
  248. T atomic_load(const volatile atomic<T>*) noexcept;
  249. template<class T>
  250. T atomic_load(const atomic<T>*) noexcept;
  251. template<class T>
  252. T atomic_load_explicit(const volatile atomic<T>*, memory_order) noexcept;
  253. template<class T>
  254. T atomic_load_explicit(const atomic<T>*, memory_order) noexcept;
  255. template<class T>
  256. T atomic_exchange(volatile atomic<T>*, atomic<T>::value_type) noexcept;
  257. template<class T>
  258. T atomic_exchange(atomic<T>*, atomic<T>::value_type) noexcept;
  259. template<class T>
  260. T atomic_exchange_explicit(volatile atomic<T>*, atomic<T>::value_type,
  261. memory_order) noexcept;
  262. template<class T>
  263. T atomic_exchange_explicit(atomic<T>*, atomic<T>::value_type,
  264. memory_order) noexcept;
  265. template<class T>
  266. bool atomic_compare_exchange_weak(volatile atomic<T>*, atomic<T>::value_type*,
  267. atomic<T>::value_type) noexcept;
  268. template<class T>
  269. bool atomic_compare_exchange_weak(atomic<T>*, atomic<T>::value_type*,
  270. atomic<T>::value_type) noexcept;
  271. template<class T>
  272. bool atomic_compare_exchange_strong(volatile atomic<T>*, atomic<T>::value_type*,
  273. atomic<T>::value_type) noexcept;
  274. template<class T>
  275. bool atomic_compare_exchange_strong(atomic<T>*, atomic<T>::value_type*,
  276. atomic<T>::value_type) noexcept;
  277. template<class T>
  278. bool atomic_compare_exchange_weak_explicit(volatile atomic<T>*, atomic<T>::value_type*,
  279. atomic<T>::value_type,
  280. memory_order, memory_order) noexcept;
  281. template<class T>
  282. bool atomic_compare_exchange_weak_explicit(atomic<T>*, atomic<T>::value_type*,
  283. atomic<T>::value_type,
  284. memory_order, memory_order) noexcept;
  285. template<class T>
  286. bool atomic_compare_exchange_strong_explicit(volatile atomic<T>*, atomic<T>::value_type*,
  287. atomic<T>::value_type,
  288. memory_order, memory_order) noexcept;
  289. template<class T>
  290. bool atomic_compare_exchange_strong_explicit(atomic<T>*, atomic<T>::value_type*,
  291. atomic<T>::value_type,
  292. memory_order, memory_order) noexcept;
  293. template<class T>
  294. T atomic_fetch_add(volatile atomic<T>*, atomic<T>::difference_type) noexcept;
  295. template<class T>
  296. T atomic_fetch_add(atomic<T>*, atomic<T>::difference_type) noexcept;
  297. template<class T>
  298. T atomic_fetch_add_explicit(volatile atomic<T>*, atomic<T>::difference_type,
  299. memory_order) noexcept;
  300. template<class T>
  301. T atomic_fetch_add_explicit(atomic<T>*, atomic<T>::difference_type,
  302. memory_order) noexcept;
  303. template<class T>
  304. T atomic_fetch_sub(volatile atomic<T>*, atomic<T>::difference_type) noexcept;
  305. template<class T>
  306. T atomic_fetch_sub(atomic<T>*, atomic<T>::difference_type) noexcept;
  307. template<class T>
  308. T atomic_fetch_sub_explicit(volatile atomic<T>*, atomic<T>::difference_type,
  309. memory_order) noexcept;
  310. template<class T>
  311. T atomic_fetch_sub_explicit(atomic<T>*, atomic<T>::difference_type,
  312. memory_order) noexcept;
  313. template<class T>
  314. T atomic_fetch_and(volatile atomic<T>*, atomic<T>::value_type) noexcept;
  315. template<class T>
  316. T atomic_fetch_and(atomic<T>*, atomic<T>::value_type) noexcept;
  317. template<class T>
  318. T atomic_fetch_and_explicit(volatile atomic<T>*, atomic<T>::value_type,
  319. memory_order) noexcept;
  320. template<class T>
  321. T atomic_fetch_and_explicit(atomic<T>*, atomic<T>::value_type,
  322. memory_order) noexcept;
  323. template<class T>
  324. T atomic_fetch_or(volatile atomic<T>*, atomic<T>::value_type) noexcept;
  325. template<class T>
  326. T atomic_fetch_or(atomic<T>*, atomic<T>::value_type) noexcept;
  327. template<class T>
  328. T atomic_fetch_or_explicit(volatile atomic<T>*, atomic<T>::value_type,
  329. memory_order) noexcept;
  330. template<class T>
  331. T atomic_fetch_or_explicit(atomic<T>*, atomic<T>::value_type,
  332. memory_order) noexcept;
  333. template<class T>
  334. T atomic_fetch_xor(volatile atomic<T>*, atomic<T>::value_type) noexcept;
  335. template<class T>
  336. T atomic_fetch_xor(atomic<T>*, atomic<T>::value_type) noexcept;
  337. template<class T>
  338. T atomic_fetch_xor_explicit(volatile atomic<T>*, atomic<T>::value_type,
  339. memory_order) noexcept;
  340. template<class T>
  341. T atomic_fetch_xor_explicit(atomic<T>*, atomic<T>::value_type,
  342. memory_order) noexcept;
  343. template<class T>
  344. void atomic_wait(const volatile atomic<T>*, atomic<T>::value_type) noexcept;
  345. template<class T>
  346. void atomic_wait(const atomic<T>*, atomic<T>::value_type) noexcept;
  347. template<class T>
  348. void atomic_wait_explicit(const volatile atomic<T>*, atomic<T>::value_type,
  349. memory_order) noexcept;
  350. template<class T>
  351. void atomic_wait_explicit(const atomic<T>*, atomic<T>::value_type,
  352. memory_order) noexcept;
  353. template<class T>
  354. void atomic_notify_one(volatile atomic<T>*) noexcept;
  355. template<class T>
  356. void atomic_notify_one(atomic<T>*) noexcept;
  357. template<class T>
  358. void atomic_notify_all(volatile atomic<T>*) noexcept;
  359. template<class T>
  360. void atomic_notify_all(atomic<T>*) noexcept;
  361. // Atomics for standard typedef types
  362. typedef atomic<bool> atomic_bool;
  363. typedef atomic<char> atomic_char;
  364. typedef atomic<signed char> atomic_schar;
  365. typedef atomic<unsigned char> atomic_uchar;
  366. typedef atomic<short> atomic_short;
  367. typedef atomic<unsigned short> atomic_ushort;
  368. typedef atomic<int> atomic_int;
  369. typedef atomic<unsigned int> atomic_uint;
  370. typedef atomic<long> atomic_long;
  371. typedef atomic<unsigned long> atomic_ulong;
  372. typedef atomic<long long> atomic_llong;
  373. typedef atomic<unsigned long long> atomic_ullong;
  374. typedef atomic<char8_t> atomic_char8_t; // C++20
  375. typedef atomic<char16_t> atomic_char16_t;
  376. typedef atomic<char32_t> atomic_char32_t;
  377. typedef atomic<wchar_t> atomic_wchar_t;
  378. typedef atomic<int_least8_t> atomic_int_least8_t;
  379. typedef atomic<uint_least8_t> atomic_uint_least8_t;
  380. typedef atomic<int_least16_t> atomic_int_least16_t;
  381. typedef atomic<uint_least16_t> atomic_uint_least16_t;
  382. typedef atomic<int_least32_t> atomic_int_least32_t;
  383. typedef atomic<uint_least32_t> atomic_uint_least32_t;
  384. typedef atomic<int_least64_t> atomic_int_least64_t;
  385. typedef atomic<uint_least64_t> atomic_uint_least64_t;
  386. typedef atomic<int_fast8_t> atomic_int_fast8_t;
  387. typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
  388. typedef atomic<int_fast16_t> atomic_int_fast16_t;
  389. typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
  390. typedef atomic<int_fast32_t> atomic_int_fast32_t;
  391. typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
  392. typedef atomic<int_fast64_t> atomic_int_fast64_t;
  393. typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
  394. typedef atomic<int8_t> atomic_int8_t;
  395. typedef atomic<uint8_t> atomic_uint8_t;
  396. typedef atomic<int16_t> atomic_int16_t;
  397. typedef atomic<uint16_t> atomic_uint16_t;
  398. typedef atomic<int32_t> atomic_int32_t;
  399. typedef atomic<uint32_t> atomic_uint32_t;
  400. typedef atomic<int64_t> atomic_int64_t;
  401. typedef atomic<uint64_t> atomic_uint64_t;
  402. typedef atomic<intptr_t> atomic_intptr_t;
  403. typedef atomic<uintptr_t> atomic_uintptr_t;
  404. typedef atomic<size_t> atomic_size_t;
  405. typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
  406. typedef atomic<intmax_t> atomic_intmax_t;
  407. typedef atomic<uintmax_t> atomic_uintmax_t;
  408. // flag type and operations
  409. typedef struct atomic_flag
  410. {
  411. atomic_flag() noexcept = default; // until C++20
  412. constexpr atomic_flag() noexcept; // since C++20
  413. atomic_flag(const atomic_flag&) = delete;
  414. atomic_flag& operator=(const atomic_flag&) = delete;
  415. atomic_flag& operator=(const atomic_flag&) volatile = delete;
  416. bool test(memory_order m = memory_order_seq_cst) volatile noexcept;
  417. bool test(memory_order m = memory_order_seq_cst) noexcept;
  418. bool test_and_set(memory_order m = memory_order_seq_cst) volatile noexcept;
  419. bool test_and_set(memory_order m = memory_order_seq_cst) noexcept;
  420. void clear(memory_order m = memory_order_seq_cst) volatile noexcept;
  421. void clear(memory_order m = memory_order_seq_cst) noexcept;
  422. void wait(bool, memory_order = memory_order::seq_cst) const volatile noexcept;
  423. void wait(bool, memory_order = memory_order::seq_cst) const noexcept;
  424. void notify_one() volatile noexcept;
  425. void notify_one() noexcept;
  426. void notify_all() volatile noexcept;
  427. void notify_all() noexcept;
  428. } atomic_flag;
  429. bool atomic_flag_test(volatile atomic_flag* obj) noexcept;
  430. bool atomic_flag_test(atomic_flag* obj) noexcept;
  431. bool atomic_flag_test_explicit(volatile atomic_flag* obj,
  432. memory_order m) noexcept;
  433. bool atomic_flag_test_explicit(atomic_flag* obj, memory_order m) noexcept;
  434. bool atomic_flag_test_and_set(volatile atomic_flag* obj) noexcept;
  435. bool atomic_flag_test_and_set(atomic_flag* obj) noexcept;
  436. bool atomic_flag_test_and_set_explicit(volatile atomic_flag* obj,
  437. memory_order m) noexcept;
  438. bool atomic_flag_test_and_set_explicit(atomic_flag* obj, memory_order m) noexcept;
  439. void atomic_flag_clear(volatile atomic_flag* obj) noexcept;
  440. void atomic_flag_clear(atomic_flag* obj) noexcept;
  441. void atomic_flag_clear_explicit(volatile atomic_flag* obj, memory_order m) noexcept;
  442. void atomic_flag_clear_explicit(atomic_flag* obj, memory_order m) noexcept;
  443. void atomic_wait(const volatile atomic_flag* obj, T old) noexcept;
  444. void atomic_wait(const atomic_flag* obj, T old) noexcept;
  445. void atomic_wait_explicit(const volatile atomic_flag* obj, T old, memory_order m) noexcept;
  446. void atomic_wait_explicit(const atomic_flag* obj, T old, memory_order m) noexcept;
  447. void atomic_one(volatile atomic_flag* obj) noexcept;
  448. void atomic_one(atomic_flag* obj) noexcept;
  449. void atomic_all(volatile atomic_flag* obj) noexcept;
  450. void atomic_all(atomic_flag* obj) noexcept;
  451. // fences
  452. void atomic_thread_fence(memory_order m) noexcept;
  453. void atomic_signal_fence(memory_order m) noexcept;
  454. // deprecated
  455. template <class T>
  456. void atomic_init(volatile atomic<T>* obj, atomic<T>::value_type desr) noexcept;
  457. template <class T>
  458. void atomic_init(atomic<T>* obj, atomic<T>::value_type desr) noexcept;
  459. #define ATOMIC_VAR_INIT(value) see below
  460. #define ATOMIC_FLAG_INIT see below
  461. } // std
  462. */
  463. #include <__assert> // all public C++ headers provide the assertion handler
  464. #include <__atomic/aliases.h>
  465. #include <__atomic/atomic.h>
  466. #include <__atomic/atomic_base.h>
  467. #include <__atomic/atomic_flag.h>
  468. #include <__atomic/atomic_init.h>
  469. #include <__atomic/atomic_lock_free.h>
  470. #include <__atomic/atomic_sync.h>
  471. #include <__atomic/check_memory_order.h>
  472. #include <__atomic/contention_t.h>
  473. #include <__atomic/cxx_atomic_impl.h>
  474. #include <__atomic/fence.h>
  475. #include <__atomic/is_always_lock_free.h>
  476. #include <__atomic/kill_dependency.h>
  477. #include <__atomic/memory_order.h>
  478. #include <__config>
  479. #include <version>
  480. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  481. # pragma GCC system_header
  482. #endif
  483. #ifdef _LIBCPP_HAS_NO_ATOMIC_HEADER
  484. # error <atomic> is not implemented
  485. #endif
  486. #ifdef kill_dependency
  487. # error <atomic> is incompatible with <stdatomic.h> before C++23. Please compile with -std=c++23.
  488. #endif
  489. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  490. # include <cmath>
  491. # include <compare>
  492. # include <cstring>
  493. # include <type_traits>
  494. #endif
  495. #endif // _LIBCPP_ATOMIC