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
atomicops.h
Go to the documentation of this file.
1 // Copyright 2010 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 // The routines exported by this module are subtle. If you use them, even if
29 // you get the code right, it will depend on careful reasoning about atomicity
30 // and memory ordering; it will be less readable, and harder to maintain. If
31 // you plan to use these routines, you should have a good reason, such as solid
32 // evidence that performance would otherwise suffer, or there being no
33 // alternative. You should assume only properties explicitly guaranteed by the
34 // specifications in this file. You are almost certainly _not_ writing code
35 // just for the x86; if you assume x86 semantics, x86 hardware bugs and
36 // implementations on other archtectures will cause your code to break. If you
37 // do not know what you are doing, avoid these routines, and use a Mutex.
38 //
39 // It is incorrect to make direct assignments to/from an atomic variable.
40 // You should use one of the Load or Store routines. The NoBarrier
41 // versions are provided when no barriers are needed:
42 // NoBarrier_Store()
43 // NoBarrier_Load()
44 // Although there are currently no compiler enforcement, you are encouraged
45 // to use these.
46 //
47 
48 #ifndef V8_ATOMICOPS_H_
49 #define V8_ATOMICOPS_H_
50 
51 #include "../include/v8.h"
52 #include "globals.h"
53 
54 #if defined(_WIN32) && defined(V8_HOST_ARCH_64_BIT)
55 // windows.h #defines this (only on x64). This causes problems because the
56 // public API also uses MemoryBarrier at the public name for this fence. So, on
57 // X64, undef it, and call its documented
58 // (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684208.aspx)
59 // implementation directly.
60 #undef MemoryBarrier
61 #endif
62 
63 namespace v8 {
64 namespace internal {
65 
66 typedef int32_t Atomic32;
67 #ifdef V8_HOST_ARCH_64_BIT
68 // We need to be able to go between Atomic64 and AtomicWord implicitly. This
69 // means Atomic64 and AtomicWord should be the same type on 64-bit.
70 #if defined(__ILP32__)
71 typedef int64_t Atomic64;
72 #else
73 typedef intptr_t Atomic64;
74 #endif
75 #endif
76 
77 // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
78 // Atomic64 routines below, depending on your architecture.
79 typedef intptr_t AtomicWord;
80 
81 // Atomically execute:
82 // result = *ptr;
83 // if (*ptr == old_value)
84 // *ptr = new_value;
85 // return result;
86 //
87 // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
88 // Always return the old value of "*ptr"
89 //
90 // This routine implies no memory barriers.
92  Atomic32 old_value,
93  Atomic32 new_value);
94 
95 // Atomically store new_value into *ptr, returning the previous value held in
96 // *ptr. This routine implies no memory barriers.
97 Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
98 
99 // Atomically increment *ptr by "increment". Returns the new value of
100 // *ptr with the increment applied. This routine implies no memory barriers.
101 Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
102 
104  Atomic32 increment);
105 
106 // These following lower-level operations are typically useful only to people
107 // implementing higher-level synchronization operations like spinlocks,
108 // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
109 // a store with appropriate memory-ordering instructions. "Acquire" operations
110 // ensure that no later memory access can be reordered ahead of the operation.
111 // "Release" operations ensure that no previous memory access can be reordered
112 // after the operation. "Barrier" operations have both "Acquire" and "Release"
113 // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
114 // access.
116  Atomic32 old_value,
117  Atomic32 new_value);
119  Atomic32 old_value,
120  Atomic32 new_value);
121 
122 void MemoryBarrier();
123 void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
124 void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
125 void Release_Store(volatile Atomic32* ptr, Atomic32 value);
126 
127 Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
128 Atomic32 Acquire_Load(volatile const Atomic32* ptr);
129 Atomic32 Release_Load(volatile const Atomic32* ptr);
130 
131 // 64-bit atomic operations (only available on 64-bit processors).
132 #ifdef V8_HOST_ARCH_64_BIT
133 Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
134  Atomic64 old_value,
135  Atomic64 new_value);
136 Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
137 Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
138 Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
139 
140 Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
141  Atomic64 old_value,
142  Atomic64 new_value);
143 Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
144  Atomic64 old_value,
145  Atomic64 new_value);
146 void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
147 void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
148 void Release_Store(volatile Atomic64* ptr, Atomic64 value);
149 Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
150 Atomic64 Acquire_Load(volatile const Atomic64* ptr);
151 Atomic64 Release_Load(volatile const Atomic64* ptr);
152 #endif // V8_HOST_ARCH_64_BIT
153 
154 } } // namespace v8::internal
155 
156 // Include our platform specific implementation.
157 #if defined(THREAD_SANITIZER)
159 #elif defined(_MSC_VER) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64)
161 #elif defined(__APPLE__)
162 #include "atomicops_internals_mac.h"
163 #elif defined(__GNUC__) && V8_HOST_ARCH_ARM64
165 #elif defined(__GNUC__) && V8_HOST_ARCH_ARM
167 #elif defined(__GNUC__) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64)
169 #elif defined(__GNUC__) && V8_HOST_ARCH_MIPS
171 #else
172 #error "Atomic operations are not supported on your platform"
173 #endif
174 
175 // On some platforms we need additional declarations to make
176 // AtomicWord compatible with our other Atomic* types.
177 #if defined(__APPLE__) || defined(__OpenBSD__)
179 #endif
180 
181 #endif // V8_ATOMICOPS_H_
void Acquire_Store(volatile Atomic32 *ptr, Atomic32 value)
int int32_t
Definition: unicode.cc:47
Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32 *ptr, Atomic32 old_value, Atomic32 new_value)
Atomic32 Release_CompareAndSwap(volatile Atomic32 *ptr, Atomic32 old_value, Atomic32 new_value)
void NoBarrier_Store(volatile Atomic32 *ptr, Atomic32 value)
Atomic32 NoBarrier_AtomicExchange(volatile Atomic32 *ptr, Atomic32 new_value)
intptr_t AtomicWord
Definition: atomicops.h:79
Atomic32 Release_Load(volatile const Atomic32 *ptr)
Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32 *ptr, Atomic32 increment)
void Release_Store(volatile Atomic32 *ptr, Atomic32 value)
Atomic32 Barrier_AtomicIncrement(volatile Atomic32 *ptr, Atomic32 increment)
Atomic32 NoBarrier_Load(volatile const Atomic32 *ptr)
Atomic32 Acquire_Load(volatile const Atomic32 *ptr)
int32_t Atomic32
Definition: atomicops.h:66
Atomic32 Acquire_CompareAndSwap(volatile Atomic32 *ptr, Atomic32 old_value, Atomic32 new_value)