1 // Copyright 2017 The Abseil Authors.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // -----------------------------------------------------------------------------
17 // -----------------------------------------------------------------------------
19 // This header file provides an Abseil version of `std::call_once` for invoking
20 // a given function at most once, across all threads. This Abseil version is
21 // faster than the C++11 version and incorporates the C++17 argument-passing
22 // fix, so that (for example) non-const references may be passed to the invoked
25 #ifndef ABSL_BASE_CALL_ONCE_H_
26 #define ABSL_BASE_CALL_ONCE_H_
31 #include <type_traits>
34 #include "absl/base/internal/invoke.h"
35 #include "absl/base/internal/low_level_scheduling.h"
36 #include "absl/base/internal/raw_logging.h"
37 #include "absl/base/internal/scheduling_mode.h"
38 #include "absl/base/internal/spinlock_wait.h"
39 #include "absl/base/macros.h"
40 #include "absl/base/optimization.h"
41 #include "absl/base/port.h"
47 namespace base_internal {
48 std::atomic<uint32_t>* ControlWord(absl::once_flag* flag);
49 } // namespace base_internal
53 // For all invocations using a given `once_flag`, invokes a given `fn` exactly
54 // once across all threads. The first call to `call_once()` with a particular
55 // `once_flag` argument (that does not throw an exception) will run the
56 // specified function with the provided `args`; other calls with the same
57 // `once_flag` argument will not run the function, but will wait
58 // for the provided function to finish running (if it is still running).
60 // This mechanism provides a safe, simple, and fast mechanism for one-time
61 // initialization in a multi-threaded process.
65 // class MyInitClass {
68 // mutable absl::once_flag once_;
70 // MyInitClass* init() const {
71 // absl::call_once(once_, &MyInitClass::Init, this);
75 template <typename Callable, typename... Args>
76 void call_once(absl::once_flag& flag, Callable&& fn, Args&&... args);
80 // Objects of this type are used to distinguish calls to `call_once()` and
81 // ensure the provided function is only invoked once across all threads. This
82 // type is not copyable or movable. However, it has a `constexpr`
83 // constructor, and is safe to use as a namespace-scoped global variable.
86 constexpr once_flag() : control_(0) {}
87 once_flag(const once_flag&) = delete;
88 once_flag& operator=(const once_flag&) = delete;
91 friend std::atomic<uint32_t>* base_internal::ControlWord(once_flag* flag);
92 std::atomic<uint32_t> control_;
95 //------------------------------------------------------------------------------
96 // End of public interfaces.
97 // Implementation details follow.
98 //------------------------------------------------------------------------------
100 namespace base_internal {
102 // Like call_once, but uses KERNEL_ONLY scheduling. Intended to be used to
103 // initialize entities used by the scheduler implementation.
104 template <typename Callable, typename... Args>
105 void LowLevelCallOnce(absl::once_flag* flag, Callable&& fn, Args&&... args);
107 // Disables scheduling while on stack when scheduling mode is non-cooperative.
108 // No effect for cooperative scheduling modes.
109 class SchedulingHelper {
111 explicit SchedulingHelper(base_internal::SchedulingMode mode) : mode_(mode) {
112 if (mode_ == base_internal::SCHEDULE_KERNEL_ONLY) {
113 guard_result_ = base_internal::SchedulingGuard::DisableRescheduling();
117 ~SchedulingHelper() {
118 if (mode_ == base_internal::SCHEDULE_KERNEL_ONLY) {
119 base_internal::SchedulingGuard::EnableRescheduling(guard_result_);
124 base_internal::SchedulingMode mode_;
128 // Bit patterns for call_once state machine values. Internal implementation
129 // detail, not for use by clients.
131 // The bit patterns are arbitrarily chosen from unlikely values, to aid in
132 // debugging. However, kOnceInit must be 0, so that a zero-initialized
133 // once_flag will be valid for immediate use.
136 kOnceRunning = 0x65C2937B,
137 kOnceWaiter = 0x05A308D2,
138 // A very small constant is chosen for kOnceDone so that it fit in a single
139 // compare with immediate instruction for most common ISAs. This is verified
140 // for x86, POWER and ARM.
141 kOnceDone = 221, // Random Number
144 template <typename Callable, typename... Args>
145 void CallOnceImpl(std::atomic<uint32_t>* control,
146 base_internal::SchedulingMode scheduling_mode, Callable&& fn,
150 uint32_t old_control = control->load(std::memory_order_acquire);
151 if (old_control != kOnceInit &&
152 old_control != kOnceRunning &&
153 old_control != kOnceWaiter &&
154 old_control != kOnceDone) {
155 ABSL_RAW_LOG(FATAL, "Unexpected value for control word: 0x%lx",
156 static_cast<unsigned long>(old_control)); // NOLINT
160 static const base_internal::SpinLockWaitTransition trans[] = {
161 {kOnceInit, kOnceRunning, true},
162 {kOnceRunning, kOnceWaiter, false},
163 {kOnceDone, kOnceDone, true}};
165 // Must do this before potentially modifying control word's state.
166 base_internal::SchedulingHelper maybe_disable_scheduling(scheduling_mode);
167 // Short circuit the simplest case to avoid procedure call overhead.
168 uint32_t old_control = kOnceInit;
169 if (control->compare_exchange_strong(old_control, kOnceRunning,
170 std::memory_order_acquire,
171 std::memory_order_relaxed) ||
172 base_internal::SpinLockWait(control, ABSL_ARRAYSIZE(trans), trans,
173 scheduling_mode) == kOnceInit) {
174 base_internal::Invoke(std::forward<Callable>(fn),
175 std::forward<Args>(args)...);
176 old_control = control->load(std::memory_order_relaxed);
177 control->store(base_internal::kOnceDone, std::memory_order_release);
178 if (old_control == base_internal::kOnceWaiter) {
179 base_internal::SpinLockWake(control, true);
181 } // else *control is already kOnceDone
184 inline std::atomic<uint32_t>* ControlWord(once_flag* flag) {
185 return &flag->control_;
188 template <typename Callable, typename... Args>
189 void LowLevelCallOnce(absl::once_flag* flag, Callable&& fn, Args&&... args) {
190 std::atomic<uint32_t>* once = base_internal::ControlWord(flag);
191 uint32_t s = once->load(std::memory_order_acquire);
192 if (ABSL_PREDICT_FALSE(s != base_internal::kOnceDone)) {
193 base_internal::CallOnceImpl(once, base_internal::SCHEDULE_KERNEL_ONLY,
194 std::forward<Callable>(fn),
195 std::forward<Args>(args)...);
199 } // namespace base_internal
201 template <typename Callable, typename... Args>
202 void call_once(absl::once_flag& flag, Callable&& fn, Args&&... args) {
203 std::atomic<uint32_t>* once = base_internal::ControlWord(&flag);
204 uint32_t s = once->load(std::memory_order_acquire);
205 if (ABSL_PREDICT_FALSE(s != base_internal::kOnceDone)) {
206 base_internal::CallOnceImpl(
207 once, base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL,
208 std::forward<Callable>(fn), std::forward<Args>(args)...);
214 #endif // ABSL_BASE_CALL_ONCE_H_