v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
once.h
Go to the documentation of this file.
1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_ONCE_H_
6#define V8_BASE_ONCE_H_
7
8// emulates google3/base/once.h
9//
10// This header is intended to be included only by v8's internal code. Users
11// should not use this directly.
12//
13// This is basically a portable version of pthread_once().
14//
15// This header declares:
16// * A type called OnceType.
17// * A macro V8_DECLARE_ONCE() which declares a (global) variable of type
18// OnceType.
19// * A function CallOnce(OnceType* once, void (*init_func)()).
20// This function, when invoked multiple times given the same OnceType object,
21// will invoke init_func on the first call only, and will make sure none of
22// the calls return before that first call to init_func has finished.
23//
24// Additionally, the following features are supported:
25// * A macro V8_ONCE_INIT which is expanded into the expression used to
26// initialize a OnceType. This is only useful when clients embed a OnceType
27// into a structure of their own and want to initialize it statically.
28// * The user can provide a parameter which CallOnce() forwards to the
29// user-provided function when it is called. Usage example:
30// CallOnce(&my_once, &MyFunctionExpectingIntArgument, 10);
31// * This implementation guarantees that OnceType is a POD (i.e. no static
32// initializer generated).
33//
34// This implements a way to perform lazy initialization. It's more efficient
35// than using mutexes as no lock is needed if initialization has already
36// happened.
37//
38// Example usage:
39// void Init();
40// V8_DECLARE_ONCE(once_init);
41//
42// // Calls Init() exactly once.
43// void InitOnce() {
44// CallOnce(&once_init, &Init);
45// }
46//
47// Note that if CallOnce() is called before main() has begun, it must
48// only be called by the thread that will eventually call main() -- that is,
49// the thread that performs dynamic initialization. In general this is a safe
50// assumption since people don't usually construct threads before main() starts,
51// but it is technically not guaranteed. Unfortunately, Win32 provides no way
52// whatsoever to statically-initialize its synchronization primitives, so our
53// only choice is to assume that dynamic initialization is single-threaded.
54
55#include <stddef.h>
56#include <stdint.h>
57
58#include <atomic>
59#include <functional>
60
63
64namespace v8 {
65namespace base {
66
67using OnceType = std::atomic<uint8_t>;
68
69#define V8_ONCE_INIT \
70 { 0 }
71
72#define V8_DECLARE_ONCE(NAME) ::v8::base::OnceType NAME
73
74enum : uint8_t {
78};
79
80using PointerArgFunction = void (*)(void* arg);
81
82template <typename... Args>
84 using type = void (*)(Args...);
85};
86
88 std::function<void()> init_func);
89
90inline void CallOnce(OnceType* once, std::function<void()> init_func) {
91 if (once->load(std::memory_order_acquire) != ONCE_STATE_DONE) {
92 CallOnceImpl(once, init_func);
93 }
94}
95
96template <typename... Args>
97inline void CallOnce(OnceType* once,
98 typename FunctionWithArgs<Args...>::type init_func,
99 Args... args)
100 requires(std::conjunction_v<std::is_scalar<Args>...>)
101{
102 if (once->load(std::memory_order_acquire) != ONCE_STATE_DONE) {
103 CallOnceImpl(once, [=]() { init_func(args...); });
104 }
105}
106
107} // namespace base
108} // namespace v8
109
110#endif // V8_BASE_ONCE_H_
#define V8_BASE_EXPORT
Definition base-export.h:26
void CallOnceImpl(OnceType *once, std::function< void()> init_func)
Definition once.cc:18
void(*)(void *arg) PointerArgFunction
Definition once.h:80
void CallOnce(OnceType *once, std::function< void()> init_func)
Definition once.h:90
std::atomic< uint8_t > OnceType
Definition once.h:67
V8_BASE_EXPORT int const char va_list args
Definition strings.h:23
@ ONCE_STATE_UNINITIALIZED
Definition once.h:75
@ ONCE_STATE_DONE
Definition once.h:77
@ ONCE_STATE_EXECUTING_FUNCTION
Definition once.h:76
void(*)(Args...) type
Definition once.h:84