v8  3.14.5(node0.10.28)
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_
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:75
#define T(name, string, precedence)
Definition: token.cc:48
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
Atomic32 Acquire_Load(volatile const Atomic32 *ptr)