once_flag.h 5.1 KB

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