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