once_flag.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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___MUTEX_ONCE_FLAG_H
  9. #define _LIBCPP___MUTEX_ONCE_FLAG_H
  10. #include <__config>
  11. #include <__functional/invoke.h>
  12. #include <__memory/shared_ptr.h> // __libcpp_acquire_load
  13. #include <__tuple/tuple_indices.h>
  14. #include <__tuple/tuple_size.h>
  15. #include <__utility/forward.h>
  16. #include <__utility/move.h>
  17. #include <cstdint>
  18. #ifndef _LIBCPP_CXX03_LANG
  19. # include <tuple>
  20. #endif
  21. #if !defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_CXX03_LANG)
  22. # include <atomic>
  23. #endif
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_PUSH_MACROS
  28. #include <__undef_macros>
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. struct _LIBCPP_TEMPLATE_VIS once_flag;
  31. #ifndef _LIBCPP_CXX03_LANG
  32. template <class _Callable, class... _Args>
  33. _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, _Callable&&, _Args&&...);
  34. #else // _LIBCPP_CXX03_LANG
  35. template <class _Callable>
  36. _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, _Callable&);
  37. template <class _Callable>
  38. _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, const _Callable&);
  39. #endif // _LIBCPP_CXX03_LANG
  40. struct _LIBCPP_TEMPLATE_VIS once_flag {
  41. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR once_flag() _NOEXCEPT : __state_(_Unset) {}
  42. once_flag(const once_flag&) = delete;
  43. once_flag& operator=(const once_flag&) = delete;
  44. #if defined(_LIBCPP_ABI_MICROSOFT)
  45. typedef uintptr_t _State_type;
  46. #else
  47. typedef unsigned long _State_type;
  48. #endif
  49. static const _State_type _Unset = 0;
  50. static const _State_type _Pending = 1;
  51. static const _State_type _Complete = ~_State_type(0);
  52. private:
  53. #if defined(_LIBCPP_ABI_MICROSOFT)
  54. atomic<_State_type> __state_;
  55. #else
  56. _State_type __state_;
  57. #endif
  58. #ifndef _LIBCPP_CXX03_LANG
  59. template <class _Callable, class... _Args>
  60. friend void call_once(once_flag&, _Callable&&, _Args&&...);
  61. #else // _LIBCPP_CXX03_LANG
  62. template <class _Callable>
  63. friend void call_once(once_flag&, _Callable&);
  64. template <class _Callable>
  65. friend void call_once(once_flag&, const _Callable&);
  66. #endif // _LIBCPP_CXX03_LANG
  67. };
  68. #ifndef _LIBCPP_CXX03_LANG
  69. template <class _Fp>
  70. class __call_once_param {
  71. _Fp& __f_;
  72. public:
  73. _LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  74. _LIBCPP_HIDE_FROM_ABI void operator()() {
  75. typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
  76. __execute(_Index());
  77. }
  78. private:
  79. template <size_t... _Indices>
  80. _LIBCPP_HIDE_FROM_ABI void __execute(__tuple_indices<_Indices...>) {
  81. std::__invoke(std::get<0>(std::move(__f_)), std::get<_Indices>(std::move(__f_))...);
  82. }
  83. };
  84. #else
  85. template <class _Fp>
  86. class __call_once_param {
  87. _Fp& __f_;
  88. public:
  89. _LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}
  90. _LIBCPP_HIDE_FROM_ABI void operator()() { __f_(); }
  91. };
  92. #endif
  93. template <class _Fp>
  94. void _LIBCPP_HIDE_FROM_ABI __call_once_proxy(void* __vp) {
  95. __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
  96. (*__p)();
  97. }
  98. #ifdef _LIBCPP_ABI_MICROSOFT
  99. _LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile atomic<once_flag::_State_type>&, void*, void (*)(void*));
  100. #else
  101. _LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile once_flag::_State_type&, void*, void (*)(void*));
  102. #endif
  103. #ifndef _LIBCPP_CXX03_LANG
  104. template <class _Callable, class... _Args>
  105. inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) {
  106. # if defined(_LIBCPP_ABI_MICROSOFT)
  107. if (__flag.__state_.load(memory_order_acquire) != ~once_flag::_State_type(0)) {
  108. # else
  109. if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
  110. # endif
  111. typedef tuple<_Callable&&, _Args&&...> _Gp;
  112. _Gp __f(std::forward<_Callable>(__func), std::forward<_Args>(__args)...);
  113. __call_once_param<_Gp> __p(__f);
  114. std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
  115. }
  116. }
  117. #else // _LIBCPP_CXX03_LANG
  118. template <class _Callable>
  119. inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable& __func) {
  120. # if defined(_LIBCPP_ABI_MICROSOFT)
  121. if (__flag.__state_.load(memory_order_acquire) != ~once_flag::_State_type(0)) {
  122. # else
  123. if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
  124. # endif
  125. __call_once_param<_Callable> __p(__func);
  126. std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
  127. }
  128. }
  129. template <class _Callable>
  130. inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, const _Callable& __func) {
  131. # if defined(_LIBCPP_ABI_MICROSOFT)
  132. if (__flag.__state_.load(memory_order_relaxed) != ~once_flag::_State_type(0)) {
  133. # else
  134. if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
  135. # endif
  136. __call_once_param<const _Callable> __p(__func);
  137. std::__call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
  138. }
  139. }
  140. #endif // _LIBCPP_CXX03_LANG
  141. _LIBCPP_END_NAMESPACE_STD
  142. _LIBCPP_POP_MACROS
  143. #endif // _LIBCPP___MUTEX_ONCE_FLAG_H