v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
once.h
Go to the documentation of this file.
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 // emulates google3/base/once.h
29 //
30 // This header is intended to be included only by v8's internal code. Users
31 // should not use this directly.
32 //
33 // This is basically a portable version of pthread_once().
34 //
35 // This header declares:
36 // * A type called OnceType.
37 // * A macro V8_DECLARE_ONCE() which declares a (global) variable of type
38 // OnceType.
39 // * A function CallOnce(OnceType* once, void (*init_func)()).
40 // This function, when invoked multiple times given the same OnceType object,
41 // will invoke init_func on the first call only, and will make sure none of
42 // the calls return before that first call to init_func has finished.
43 //
44 // Additionally, the following features are supported:
45 // * A macro V8_ONCE_INIT which is expanded into the expression used to
46 // initialize a OnceType. This is only useful when clients embed a OnceType
47 // into a structure of their own and want to initialize it statically.
48 // * The user can provide a parameter which CallOnce() forwards to the
49 // user-provided function when it is called. Usage example:
50 // CallOnce(&my_once, &MyFunctionExpectingIntArgument, 10);
51 // * This implementation guarantees that OnceType is a POD (i.e. no static
52 // initializer generated).
53 //
54 // This implements a way to perform lazy initialization. It's more efficient
55 // than using mutexes as no lock is needed if initialization has already
56 // happened.
57 //
58 // Example usage:
59 // void Init();
60 // V8_DECLARE_ONCE(once_init);
61 //
62 // // Calls Init() exactly once.
63 // void InitOnce() {
64 // CallOnce(&once_init, &Init);
65 // }
66 //
67 // Note that if CallOnce() is called before main() has begun, it must
68 // only be called by the thread that will eventually call main() -- that is,
69 // the thread that performs dynamic initialization. In general this is a safe
70 // assumption since people don't usually construct threads before main() starts,
71 // but it is technically not guaranteed. Unfortunately, Win32 provides no way
72 // whatsoever to statically-initialize its synchronization primitives, so our
73 // only choice is to assume that dynamic initialization is single-threaded.
74 
75 #ifndef V8_ONCE_H_
76 #define V8_ONCE_H_
77 
78 #include "atomicops.h"
79 
80 namespace v8 {
81 namespace internal {
82 
84 
85 #define V8_ONCE_INIT 0
86 
87 #define V8_DECLARE_ONCE(NAME) ::v8::internal::OnceType NAME
88 
89 enum {
93 };
94 
95 typedef void (*NoArgFunction)();
96 typedef void (*PointerArgFunction)(void* arg);
97 
98 template <typename T>
100  typedef void (*type)(T);
101 };
102 
103 void CallOnceImpl(OnceType* once, PointerArgFunction init_func, void* arg);
104 
105 inline void CallOnce(OnceType* once, NoArgFunction init_func) {
106  if (Acquire_Load(once) != ONCE_STATE_DONE) {
107  CallOnceImpl(once, reinterpret_cast<PointerArgFunction>(init_func), NULL);
108  }
109 }
110 
111 
112 template <typename Arg>
113 inline void CallOnce(OnceType* once,
114  typename OneArgFunction<Arg*>::type init_func, Arg* arg) {
115  if (Acquire_Load(once) != ONCE_STATE_DONE) {
116  CallOnceImpl(once, reinterpret_cast<PointerArgFunction>(init_func),
117  static_cast<void*>(arg));
118  }
119 }
120 
121 } } // namespace v8::internal
122 
123 #endif // V8_ONCE_H_
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
void CallOnceImpl(OnceType *once, PointerArgFunction init_func, void *arg)
Definition: once.cc:42
void(* NoArgFunction)()
Definition: once.h:95
void CallOnce(OnceType *once, NoArgFunction init_func)
Definition: once.h:105
void(* PointerArgFunction)(void *arg)
Definition: once.h:96
AtomicWord OnceType
Definition: once.h:83
intptr_t AtomicWord
Definition: atomicops.h:79
#define T(name, string, precedence)
Definition: token.cc:48
Atomic32 Acquire_Load(volatile const Atomic32 *ptr)