V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
atomicops.h
1 // Copyright 2010 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 // The routines exported by this module are subtle. If you use them, even if
6 // you get the code right, it will depend on careful reasoning about atomicity
7 // and memory ordering; it will be less readable, and harder to maintain. If
8 // you plan to use these routines, you should have a good reason, such as solid
9 // evidence that performance would otherwise suffer, or there being no
10 // alternative. You should assume only properties explicitly guaranteed by the
11 // specifications in this file. You are almost certainly _not_ writing code
12 // just for the x86; if you assume x86 semantics, x86 hardware bugs and
13 // implementations on other archtectures will cause your code to break. If you
14 // do not know what you are doing, avoid these routines, and use a Mutex.
15 //
16 // It is incorrect to make direct assignments to/from an atomic variable.
17 // You should use one of the Load or Store routines. The Relaxed versions
18 // are provided when no fences are needed:
19 // Relaxed_Store()
20 // Relaxed_Load()
21 // Although there are currently no compiler enforcement, you are encouraged
22 // to use these.
23 //
24 
25 #ifndef V8_BASE_ATOMICOPS_H_
26 #define V8_BASE_ATOMICOPS_H_
27 
28 #include <stdint.h>
29 
30 // Small C++ header which defines implementation specific macros used to
31 // identify the STL implementation.
32 // - libc++: captures __config for _LIBCPP_VERSION
33 // - libstdc++: captures bits/c++config.h for __GLIBCXX__
34 #include <cstddef>
35 
36 #include "src/base/base-export.h"
37 #include "src/base/build_config.h"
38 
39 namespace v8 {
40 namespace base {
41 
42 typedef char Atomic8;
43 typedef int16_t Atomic16;
44 typedef int32_t Atomic32;
45 #if defined(V8_HOST_ARCH_64_BIT)
46 // We need to be able to go between Atomic64 and AtomicWord implicitly. This
47 // means Atomic64 and AtomicWord should be the same type on 64-bit.
48 #if defined(__ILP32__)
49 typedef int64_t Atomic64;
50 #else
51 typedef intptr_t Atomic64;
52 #endif // defined(__ILP32__)
53 #endif // defined(V8_HOST_ARCH_64_BIT)
54 
55 // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
56 // Atomic64 routines below, depending on your architecture.
57 typedef intptr_t AtomicWord;
58 
59 // Atomically execute:
60 // result = *ptr;
61 // if (*ptr == old_value)
62 // *ptr = new_value;
63 // return result;
64 //
65 // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
66 // Always return the old value of "*ptr"
67 //
68 // This routine implies no memory barriers.
69 Atomic32 Relaxed_CompareAndSwap(volatile Atomic32* ptr, Atomic32 old_value,
70  Atomic32 new_value);
71 
72 // Atomically store new_value into *ptr, returning the previous value held in
73 // *ptr. This routine implies no memory barriers.
74 Atomic32 Relaxed_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
75 
76 // Atomically increment *ptr by "increment". Returns the new value of
77 // *ptr with the increment applied. This routine implies no memory barriers.
78 Atomic32 Relaxed_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
79 
80 Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
81  Atomic32 increment);
82 
83 // These following lower-level operations are typically useful only to people
84 // implementing higher-level synchronization operations like spinlocks,
85 // mutexes, and condition-variables. They combine CompareAndSwap(), a load,
86 // or a store with appropriate memory-ordering instructions. "Acquire"
87 // operations ensure that no later memory access can be reordered ahead of the
88 // operation. "Release" operations ensure that no previous memory access can
89 // be reordered after the operation. "Fence" operations have both "Acquire"
90 // and "Release" semantics. A SeqCst_MemoryFence() has "Fence" semantics, but
91 // does no memory access.
92 Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
93  Atomic32 old_value,
94  Atomic32 new_value);
95 Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
96  Atomic32 old_value,
97  Atomic32 new_value);
98 
99 void SeqCst_MemoryFence();
100 void Relaxed_Store(volatile Atomic8* ptr, Atomic8 value);
101 void Relaxed_Store(volatile Atomic16* ptr, Atomic16 value);
102 void Relaxed_Store(volatile Atomic32* ptr, Atomic32 value);
103 void Release_Store(volatile Atomic32* ptr, Atomic32 value);
104 
105 Atomic8 Relaxed_Load(volatile const Atomic8* ptr);
106 Atomic16 Relaxed_Load(volatile const Atomic16* ptr);
107 Atomic32 Relaxed_Load(volatile const Atomic32* ptr);
108 Atomic32 Acquire_Load(volatile const Atomic32* ptr);
109 
110 // 64-bit atomic operations (only available on 64-bit processors).
111 #ifdef V8_HOST_ARCH_64_BIT
112 Atomic64 Relaxed_CompareAndSwap(volatile Atomic64* ptr, Atomic64 old_value,
113  Atomic64 new_value);
114 Atomic64 Relaxed_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
115 Atomic64 Relaxed_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
116 Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
117 
118 Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
119  Atomic64 old_value,
120  Atomic64 new_value);
121 Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
122  Atomic64 old_value,
123  Atomic64 new_value);
124 void Relaxed_Store(volatile Atomic64* ptr, Atomic64 value);
125 void Release_Store(volatile Atomic64* ptr, Atomic64 value);
126 Atomic64 Relaxed_Load(volatile const Atomic64* ptr);
127 Atomic64 Acquire_Load(volatile const Atomic64* ptr);
128 #endif // V8_HOST_ARCH_64_BIT
129 
130 } // namespace base
131 } // namespace v8
132 
133 #if defined(V8_OS_WIN)
134 #include "src/base/atomicops_internals_std.h"
135 #else
136 // TODO(ulan): Switch to std version after performance regression with Wheezy
137 // sysroot is no longer relevant. Debian Wheezy LTS ends on 31st of May 2018.
138 #include "src/base/atomicops_internals_portable.h"
139 #endif
140 
141 // On some platforms we need additional declarations to make
142 // AtomicWord compatible with our other Atomic* types.
143 #if defined(V8_OS_MACOSX) || defined(V8_OS_OPENBSD) || defined(V8_OS_AIX)
144 #include "src/base/atomicops_internals_atomicword_compat.h"
145 #endif
146 
147 #endif // V8_BASE_ATOMICOPS_H_
Definition: libplatform.h:13