xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__mutex/once_flag.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #ifndef _LIBCPP___CXX03___MUTEX_ONCE_FLAG_H
10 #define _LIBCPP___CXX03___MUTEX_ONCE_FLAG_H
11 
12 #include <__cxx03/__config>
13 #include <__cxx03/__memory/shared_ptr.h> // __libcpp_acquire_load
14 #include <__cxx03/__tuple/tuple_indices.h>
15 #include <__cxx03/__tuple/tuple_size.h>
16 #include <__cxx03/__type_traits/invoke.h>
17 #include <__cxx03/__utility/forward.h>
18 #include <__cxx03/__utility/move.h>
19 #include <__cxx03/cstdint>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_PUSH_MACROS
26 #include <__cxx03/__undef_macros>
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 struct _LIBCPP_TEMPLATE_VIS once_flag;
31 
32 template <class _Callable>
33 _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, _Callable&);
34 
35 template <class _Callable>
36 _LIBCPP_HIDE_FROM_ABI void call_once(once_flag&, const _Callable&);
37 
38 struct _LIBCPP_TEMPLATE_VIS once_flag {
once_flagonce_flag39   _LIBCPP_HIDE_FROM_ABI once_flag() _NOEXCEPT : __state_(_Unset) {}
40   once_flag(const once_flag&)            = delete;
41   once_flag& operator=(const once_flag&) = delete;
42 
43 #if defined(_LIBCPP_ABI_MICROSOFT)
44   typedef uintptr_t _State_type;
45 #else
46   typedef unsigned long _State_type;
47 #endif
48 
49   static const _State_type _Unset    = 0;
50   static const _State_type _Pending  = 1;
51   static const _State_type _Complete = ~_State_type(0);
52 
53 private:
54   _State_type __state_;
55 
56   template <class _Callable>
57   friend void call_once(once_flag&, _Callable&);
58 
59   template <class _Callable>
60   friend void call_once(once_flag&, const _Callable&);
61 };
62 
63 template <class _Fp>
64 class __call_once_param {
65   _Fp& __f_;
66 
67 public:
__call_once_param(_Fp & __f)68   _LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}
69 
operator()70   _LIBCPP_HIDE_FROM_ABI void operator()() { __f_(); }
71 };
72 
73 template <class _Fp>
__call_once_proxy(void * __vp)74 void _LIBCPP_HIDE_FROM_ABI __call_once_proxy(void* __vp) {
75   __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
76   (*__p)();
77 }
78 
79 _LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile once_flag::_State_type&, void*, void (*)(void*));
80 
81 template <class _Callable>
call_once(once_flag & __flag,_Callable & __func)82 inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, _Callable& __func) {
83   if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
84     __call_once_param<_Callable> __p(__func);
85     std::__call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
86   }
87 }
88 
89 template <class _Callable>
call_once(once_flag & __flag,const _Callable & __func)90 inline _LIBCPP_HIDE_FROM_ABI void call_once(once_flag& __flag, const _Callable& __func) {
91   if (__libcpp_acquire_load(&__flag.__state_) != once_flag::_Complete) {
92     __call_once_param<const _Callable> __p(__func);
93     std::__call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
94   }
95 }
96 
97 _LIBCPP_END_NAMESPACE_STD
98 
99 _LIBCPP_POP_MACROS
100 
101 #endif // _LIBCPP___CXX03___MUTEX_ONCE_FLAG_H
102