atomic_base.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_BASE_H
  9. #define _LIBCPP___ATOMIC_ATOMIC_BASE_H
  10. #include <__atomic/atomic_sync.h>
  11. #include <__atomic/check_memory_order.h>
  12. #include <__atomic/cxx_atomic_impl.h>
  13. #include <__atomic/is_always_lock_free.h>
  14. #include <__atomic/memory_order.h>
  15. #include <__availability>
  16. #include <__config>
  17. #include <__memory/addressof.h>
  18. #include <__type_traits/is_integral.h>
  19. #include <__type_traits/is_nothrow_default_constructible.h>
  20. #include <__type_traits/is_same.h>
  21. #include <version>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value>
  27. struct __atomic_base // false
  28. {
  29. mutable __cxx_atomic_impl<_Tp> __a_;
  30. #if _LIBCPP_STD_VER >= 17
  31. static _LIBCPP_CONSTEXPR bool is_always_lock_free = __libcpp_is_always_lock_free<__cxx_atomic_impl<_Tp> >::__value;
  32. #endif
  33. _LIBCPP_HIDE_FROM_ABI
  34. bool is_lock_free() const volatile _NOEXCEPT
  35. {return __cxx_atomic_is_lock_free(sizeof(_Tp));}
  36. _LIBCPP_HIDE_FROM_ABI
  37. bool is_lock_free() const _NOEXCEPT
  38. {return static_cast<__atomic_base const volatile*>(this)->is_lock_free();}
  39. _LIBCPP_HIDE_FROM_ABI void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
  40. _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) {
  41. std::__cxx_atomic_store(std::addressof(__a_), __d, __m);
  42. }
  43. _LIBCPP_HIDE_FROM_ABI void store(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT
  44. _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) {
  45. std::__cxx_atomic_store(std::addressof(__a_), __d, __m);
  46. }
  47. _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
  48. _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) {
  49. return std::__cxx_atomic_load(std::addressof(__a_), __m);
  50. }
  51. _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
  52. _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) {
  53. return std::__cxx_atomic_load(std::addressof(__a_), __m);
  54. }
  55. _LIBCPP_HIDE_FROM_ABI
  56. operator _Tp() const volatile _NOEXCEPT {return load();}
  57. _LIBCPP_HIDE_FROM_ABI
  58. operator _Tp() const _NOEXCEPT {return load();}
  59. _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  60. return std::__cxx_atomic_exchange(std::addressof(__a_), __d, __m);
  61. }
  62. _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  63. return std::__cxx_atomic_exchange(std::addressof(__a_), __d, __m);
  64. }
  65. _LIBCPP_HIDE_FROM_ABI bool
  66. compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) volatile _NOEXCEPT
  67. _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
  68. return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
  69. }
  70. _LIBCPP_HIDE_FROM_ABI bool compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) _NOEXCEPT
  71. _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
  72. return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
  73. }
  74. _LIBCPP_HIDE_FROM_ABI bool
  75. compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) volatile _NOEXCEPT
  76. _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
  77. return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
  78. }
  79. _LIBCPP_HIDE_FROM_ABI bool compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) _NOEXCEPT
  80. _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
  81. return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
  82. }
  83. _LIBCPP_HIDE_FROM_ABI bool
  84. compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  85. return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
  86. }
  87. _LIBCPP_HIDE_FROM_ABI bool
  88. compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  89. return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
  90. }
  91. _LIBCPP_HIDE_FROM_ABI bool
  92. compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  93. return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
  94. }
  95. _LIBCPP_HIDE_FROM_ABI bool
  96. compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  97. return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
  98. }
  99. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const
  100. volatile _NOEXCEPT {
  101. std::__cxx_atomic_wait(std::addressof(__a_), __v, __m);
  102. }
  103. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
  104. wait(_Tp __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT {
  105. std::__cxx_atomic_wait(std::addressof(__a_), __v, __m);
  106. }
  107. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT {
  108. std::__cxx_atomic_notify_one(std::addressof(__a_));
  109. }
  110. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT {
  111. std::__cxx_atomic_notify_one(std::addressof(__a_));
  112. }
  113. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
  114. std::__cxx_atomic_notify_all(std::addressof(__a_));
  115. }
  116. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT {
  117. std::__cxx_atomic_notify_all(std::addressof(__a_));
  118. }
  119. #if _LIBCPP_STD_VER >= 20
  120. _LIBCPP_HIDE_FROM_ABI constexpr
  121. __atomic_base() noexcept(is_nothrow_default_constructible_v<_Tp>) : __a_(_Tp()) {}
  122. #else
  123. _LIBCPP_HIDE_FROM_ABI
  124. __atomic_base() _NOEXCEPT = default;
  125. #endif
  126. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  127. __atomic_base(_Tp __d) _NOEXCEPT : __a_(__d) {}
  128. __atomic_base(const __atomic_base&) = delete;
  129. };
  130. #if _LIBCPP_STD_VER >= 17
  131. template <class _Tp, bool __b>
  132. _LIBCPP_CONSTEXPR bool __atomic_base<_Tp, __b>::is_always_lock_free;
  133. #endif
  134. // atomic<Integral>
  135. template <class _Tp>
  136. struct __atomic_base<_Tp, true>
  137. : public __atomic_base<_Tp, false>
  138. {
  139. using __base = __atomic_base<_Tp, false>;
  140. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
  141. __atomic_base() _NOEXCEPT = default;
  142. _LIBCPP_HIDE_FROM_ABI
  143. _LIBCPP_CONSTEXPR __atomic_base(_Tp __d) _NOEXCEPT : __base(__d) {}
  144. _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  145. return std::__cxx_atomic_fetch_add(std::addressof(this->__a_), __op, __m);
  146. }
  147. _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  148. return std::__cxx_atomic_fetch_add(std::addressof(this->__a_), __op, __m);
  149. }
  150. _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  151. return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m);
  152. }
  153. _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  154. return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m);
  155. }
  156. _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  157. return std::__cxx_atomic_fetch_and(std::addressof(this->__a_), __op, __m);
  158. }
  159. _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  160. return std::__cxx_atomic_fetch_and(std::addressof(this->__a_), __op, __m);
  161. }
  162. _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  163. return std::__cxx_atomic_fetch_or(std::addressof(this->__a_), __op, __m);
  164. }
  165. _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  166. return std::__cxx_atomic_fetch_or(std::addressof(this->__a_), __op, __m);
  167. }
  168. _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
  169. return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m);
  170. }
  171. _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
  172. return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m);
  173. }
  174. _LIBCPP_HIDE_FROM_ABI
  175. _Tp operator++(int) volatile _NOEXCEPT {return fetch_add(_Tp(1));}
  176. _LIBCPP_HIDE_FROM_ABI
  177. _Tp operator++(int) _NOEXCEPT {return fetch_add(_Tp(1));}
  178. _LIBCPP_HIDE_FROM_ABI
  179. _Tp operator--(int) volatile _NOEXCEPT {return fetch_sub(_Tp(1));}
  180. _LIBCPP_HIDE_FROM_ABI
  181. _Tp operator--(int) _NOEXCEPT {return fetch_sub(_Tp(1));}
  182. _LIBCPP_HIDE_FROM_ABI
  183. _Tp operator++() volatile _NOEXCEPT {return fetch_add(_Tp(1)) + _Tp(1);}
  184. _LIBCPP_HIDE_FROM_ABI
  185. _Tp operator++() _NOEXCEPT {return fetch_add(_Tp(1)) + _Tp(1);}
  186. _LIBCPP_HIDE_FROM_ABI
  187. _Tp operator--() volatile _NOEXCEPT {return fetch_sub(_Tp(1)) - _Tp(1);}
  188. _LIBCPP_HIDE_FROM_ABI
  189. _Tp operator--() _NOEXCEPT {return fetch_sub(_Tp(1)) - _Tp(1);}
  190. _LIBCPP_HIDE_FROM_ABI
  191. _Tp operator+=(_Tp __op) volatile _NOEXCEPT {return fetch_add(__op) + __op;}
  192. _LIBCPP_HIDE_FROM_ABI
  193. _Tp operator+=(_Tp __op) _NOEXCEPT {return fetch_add(__op) + __op;}
  194. _LIBCPP_HIDE_FROM_ABI
  195. _Tp operator-=(_Tp __op) volatile _NOEXCEPT {return fetch_sub(__op) - __op;}
  196. _LIBCPP_HIDE_FROM_ABI
  197. _Tp operator-=(_Tp __op) _NOEXCEPT {return fetch_sub(__op) - __op;}
  198. _LIBCPP_HIDE_FROM_ABI
  199. _Tp operator&=(_Tp __op) volatile _NOEXCEPT {return fetch_and(__op) & __op;}
  200. _LIBCPP_HIDE_FROM_ABI
  201. _Tp operator&=(_Tp __op) _NOEXCEPT {return fetch_and(__op) & __op;}
  202. _LIBCPP_HIDE_FROM_ABI
  203. _Tp operator|=(_Tp __op) volatile _NOEXCEPT {return fetch_or(__op) | __op;}
  204. _LIBCPP_HIDE_FROM_ABI
  205. _Tp operator|=(_Tp __op) _NOEXCEPT {return fetch_or(__op) | __op;}
  206. _LIBCPP_HIDE_FROM_ABI
  207. _Tp operator^=(_Tp __op) volatile _NOEXCEPT {return fetch_xor(__op) ^ __op;}
  208. _LIBCPP_HIDE_FROM_ABI
  209. _Tp operator^=(_Tp __op) _NOEXCEPT {return fetch_xor(__op) ^ __op;}
  210. };
  211. _LIBCPP_END_NAMESPACE_STD
  212. #endif // _LIBCPP___ATOMIC_ATOMIC_BASE_H