atomic_flag.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ATOMIC_ATOMIC_FLAG_H
  9. #define _LIBCPP___ATOMIC_ATOMIC_FLAG_H
  10. #include <__atomic/atomic_sync.h>
  11. #include <__atomic/contention_t.h>
  12. #include <__atomic/cxx_atomic_impl.h>
  13. #include <__atomic/memory_order.h>
  14. #include <__chrono/duration.h>
  15. #include <__config>
  16. #include <__threading_support>
  17. #include <cstdint>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. struct atomic_flag
  23. {
  24. __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> __a_;
  25. _LIBCPP_HIDE_FROM_ABI
  26. bool test(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
  27. {return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);}
  28. _LIBCPP_HIDE_FROM_ABI
  29. bool test(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
  30. {return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);}
  31. _LIBCPP_HIDE_FROM_ABI
  32. bool test_and_set(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
  33. {return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);}
  34. _LIBCPP_HIDE_FROM_ABI
  35. bool test_and_set(memory_order __m = memory_order_seq_cst) _NOEXCEPT
  36. {return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);}
  37. _LIBCPP_HIDE_FROM_ABI
  38. void clear(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
  39. {__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);}
  40. _LIBCPP_HIDE_FROM_ABI
  41. void clear(memory_order __m = memory_order_seq_cst) _NOEXCEPT
  42. {__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);}
  43. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
  44. void wait(bool __v, memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
  45. {__cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);}
  46. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
  47. void wait(bool __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT
  48. {__cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);}
  49. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
  50. void notify_one() volatile _NOEXCEPT
  51. {__cxx_atomic_notify_one(&__a_);}
  52. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
  53. void notify_one() _NOEXCEPT
  54. {__cxx_atomic_notify_one(&__a_);}
  55. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
  56. void notify_all() volatile _NOEXCEPT
  57. {__cxx_atomic_notify_all(&__a_);}
  58. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
  59. void notify_all() _NOEXCEPT
  60. {__cxx_atomic_notify_all(&__a_);}
  61. #if _LIBCPP_STD_VER >= 20
  62. _LIBCPP_HIDE_FROM_ABI constexpr
  63. atomic_flag() _NOEXCEPT : __a_(false) {}
  64. #else
  65. atomic_flag() _NOEXCEPT = default;
  66. #endif
  67. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  68. atomic_flag(bool __b) _NOEXCEPT : __a_(__b) {} // EXTENSION
  69. atomic_flag(const atomic_flag&) = delete;
  70. atomic_flag& operator=(const atomic_flag&) = delete;
  71. atomic_flag& operator=(const atomic_flag&) volatile = delete;
  72. };
  73. inline _LIBCPP_HIDE_FROM_ABI
  74. bool
  75. atomic_flag_test(const volatile atomic_flag* __o) _NOEXCEPT
  76. {
  77. return __o->test();
  78. }
  79. inline _LIBCPP_HIDE_FROM_ABI
  80. bool
  81. atomic_flag_test(const atomic_flag* __o) _NOEXCEPT
  82. {
  83. return __o->test();
  84. }
  85. inline _LIBCPP_HIDE_FROM_ABI
  86. bool
  87. atomic_flag_test_explicit(const volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
  88. {
  89. return __o->test(__m);
  90. }
  91. inline _LIBCPP_HIDE_FROM_ABI
  92. bool
  93. atomic_flag_test_explicit(const atomic_flag* __o, memory_order __m) _NOEXCEPT
  94. {
  95. return __o->test(__m);
  96. }
  97. inline _LIBCPP_HIDE_FROM_ABI
  98. bool
  99. atomic_flag_test_and_set(volatile atomic_flag* __o) _NOEXCEPT
  100. {
  101. return __o->test_and_set();
  102. }
  103. inline _LIBCPP_HIDE_FROM_ABI
  104. bool
  105. atomic_flag_test_and_set(atomic_flag* __o) _NOEXCEPT
  106. {
  107. return __o->test_and_set();
  108. }
  109. inline _LIBCPP_HIDE_FROM_ABI
  110. bool
  111. atomic_flag_test_and_set_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
  112. {
  113. return __o->test_and_set(__m);
  114. }
  115. inline _LIBCPP_HIDE_FROM_ABI
  116. bool
  117. atomic_flag_test_and_set_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT
  118. {
  119. return __o->test_and_set(__m);
  120. }
  121. inline _LIBCPP_HIDE_FROM_ABI
  122. void
  123. atomic_flag_clear(volatile atomic_flag* __o) _NOEXCEPT
  124. {
  125. __o->clear();
  126. }
  127. inline _LIBCPP_HIDE_FROM_ABI
  128. void
  129. atomic_flag_clear(atomic_flag* __o) _NOEXCEPT
  130. {
  131. __o->clear();
  132. }
  133. inline _LIBCPP_HIDE_FROM_ABI
  134. void
  135. atomic_flag_clear_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
  136. {
  137. __o->clear(__m);
  138. }
  139. inline _LIBCPP_HIDE_FROM_ABI
  140. void
  141. atomic_flag_clear_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT
  142. {
  143. __o->clear(__m);
  144. }
  145. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  146. void
  147. atomic_flag_wait(const volatile atomic_flag* __o, bool __v) _NOEXCEPT
  148. {
  149. __o->wait(__v);
  150. }
  151. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  152. void
  153. atomic_flag_wait(const atomic_flag* __o, bool __v) _NOEXCEPT
  154. {
  155. __o->wait(__v);
  156. }
  157. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  158. void
  159. atomic_flag_wait_explicit(const volatile atomic_flag* __o,
  160. bool __v, memory_order __m) _NOEXCEPT
  161. {
  162. __o->wait(__v, __m);
  163. }
  164. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  165. void
  166. atomic_flag_wait_explicit(const atomic_flag* __o,
  167. bool __v, memory_order __m) _NOEXCEPT
  168. {
  169. __o->wait(__v, __m);
  170. }
  171. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  172. void
  173. atomic_flag_notify_one(volatile atomic_flag* __o) _NOEXCEPT
  174. {
  175. __o->notify_one();
  176. }
  177. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  178. void
  179. atomic_flag_notify_one(atomic_flag* __o) _NOEXCEPT
  180. {
  181. __o->notify_one();
  182. }
  183. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  184. void
  185. atomic_flag_notify_all(volatile atomic_flag* __o) _NOEXCEPT
  186. {
  187. __o->notify_all();
  188. }
  189. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
  190. void
  191. atomic_flag_notify_all(atomic_flag* __o) _NOEXCEPT
  192. {
  193. __o->notify_all();
  194. }
  195. _LIBCPP_END_NAMESPACE_STD
  196. #endif // _LIBCPP___ATOMIC_ATOMIC_FLAG_H