[email protected] | f7d2fbd | 2012-06-18 02:47:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 4 | |
| 5 | // For atomic operations on reference counts, see atomic_refcount.h. |
| 6 | // For atomic operations on sequence numbers, see atomic_sequence_num.h. |
| 7 | |
| 8 | // The routines exported by this module are subtle. If you use them, even if |
| 9 | // you get the code right, it will depend on careful reasoning about atomicity |
| 10 | // and memory ordering; it will be less readable, and harder to maintain. If |
| 11 | // you plan to use these routines, you should have a good reason, such as solid |
| 12 | // evidence that performance would otherwise suffer, or there being no |
| 13 | // alternative. You should assume only properties explicitly guaranteed by the |
| 14 | // specifications in this file. You are almost certainly _not_ writing code |
| 15 | // just for the x86; if you assume x86 semantics, x86 hardware bugs and |
| 16 | // implementations on other archtectures will cause your code to break. If you |
| 17 | // do not know what you are doing, avoid these routines, and use a Mutex. |
| 18 | // |
| 19 | // It is incorrect to make direct assignments to/from an atomic variable. |
| 20 | // You should use one of the Load or Store routines. The NoBarrier |
| 21 | // versions are provided when no barriers are needed: |
| 22 | // NoBarrier_Store() |
| 23 | // NoBarrier_Load() |
| 24 | // Although there are currently no compiler enforcement, you are encouraged |
| 25 | // to use these. |
| 26 | // |
| 27 | |
| 28 | #ifndef BASE_ATOMICOPS_H_ |
| 29 | #define BASE_ATOMICOPS_H_ |
| 30 | |
jfb | 57a4e4a | 2014-10-14 15:06:05 | [diff] [blame] | 31 | #include <cassert> // Small C++ header which defines implementation specific |
| 32 | // macros used to identify the STL implementation. |
[email protected] | 9267710 | 2014-02-08 04:59:55 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | |
jfb | 57a4e4a | 2014-10-14 15:06:05 | [diff] [blame] | 35 | #include "base/base_export.h" |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 36 | #include "build/build_config.h" |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 37 | |
[email protected] | a48610c | 2013-01-15 08:51:28 | [diff] [blame] | 38 | #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS) |
| 39 | // windows.h #defines this (only on x64). This causes problems because the |
| 40 | // public API also uses MemoryBarrier at the public name for this fence. So, on |
| 41 | // X64, undef it, and call its documented |
| 42 | // (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684208.aspx) |
| 43 | // implementation directly. |
| 44 | #undef MemoryBarrier |
| 45 | #endif |
| 46 | |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 47 | namespace base { |
| 48 | namespace subtle { |
| 49 | |
[email protected] | 9267710 | 2014-02-08 04:59:55 | [diff] [blame] | 50 | typedef int32_t Atomic32; |
[email protected] | 616f9a1 | 2009-07-27 21:17:23 | [diff] [blame] | 51 | #ifdef ARCH_CPU_64_BITS |
| 52 | // We need to be able to go between Atomic64 and AtomicWord implicitly. This |
| 53 | // means Atomic64 and AtomicWord should be the same type on 64-bit. |
[email protected] | c163248d | 2013-02-07 00:32:49 | [diff] [blame] | 54 | #if defined(__ILP32__) || defined(OS_NACL) |
[email protected] | bd254b4 | 2010-12-01 07:34:58 | [diff] [blame] | 55 | // NaCl's intptr_t is not actually 64-bits on 64-bit! |
| 56 | // http://code.google.com/p/nativeclient/issues/detail?id=1162 |
| 57 | typedef int64_t Atomic64; |
| 58 | #else |
[email protected] | 616f9a1 | 2009-07-27 21:17:23 | [diff] [blame] | 59 | typedef intptr_t Atomic64; |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 60 | #endif |
[email protected] | bd254b4 | 2010-12-01 07:34:58 | [diff] [blame] | 61 | #endif |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 62 | |
| 63 | // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or |
| 64 | // Atomic64 routines below, depending on your architecture. |
| 65 | typedef intptr_t AtomicWord; |
| 66 | |
| 67 | // Atomically execute: |
| 68 | // result = *ptr; |
| 69 | // if (*ptr == old_value) |
| 70 | // *ptr = new_value; |
| 71 | // return result; |
| 72 | // |
| 73 | // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value". |
| 74 | // Always return the old value of "*ptr" |
| 75 | // |
| 76 | // This routine implies no memory barriers. |
| 77 | Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
| 78 | Atomic32 old_value, |
| 79 | Atomic32 new_value); |
| 80 | |
| 81 | // Atomically store new_value into *ptr, returning the previous value held in |
| 82 | // *ptr. This routine implies no memory barriers. |
| 83 | Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value); |
| 84 | |
| 85 | // Atomically increment *ptr by "increment". Returns the new value of |
| 86 | // *ptr with the increment applied. This routine implies no memory barriers. |
| 87 | Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment); |
| 88 | |
| 89 | Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
| 90 | Atomic32 increment); |
| 91 | |
| 92 | // These following lower-level operations are typically useful only to people |
| 93 | // implementing higher-level synchronization operations like spinlocks, |
| 94 | // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or |
| 95 | // a store with appropriate memory-ordering instructions. "Acquire" operations |
| 96 | // ensure that no later memory access can be reordered ahead of the operation. |
| 97 | // "Release" operations ensure that no previous memory access can be reordered |
| 98 | // after the operation. "Barrier" operations have both "Acquire" and "Release" |
| 99 | // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory |
| 100 | // access. |
| 101 | Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
| 102 | Atomic32 old_value, |
| 103 | Atomic32 new_value); |
| 104 | Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
| 105 | Atomic32 old_value, |
| 106 | Atomic32 new_value); |
| 107 | |
| 108 | void MemoryBarrier(); |
| 109 | void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value); |
| 110 | void Acquire_Store(volatile Atomic32* ptr, Atomic32 value); |
| 111 | void Release_Store(volatile Atomic32* ptr, Atomic32 value); |
| 112 | |
| 113 | Atomic32 NoBarrier_Load(volatile const Atomic32* ptr); |
| 114 | Atomic32 Acquire_Load(volatile const Atomic32* ptr); |
| 115 | Atomic32 Release_Load(volatile const Atomic32* ptr); |
| 116 | |
| 117 | // 64-bit atomic operations (only available on 64-bit processors). |
[email protected] | 616f9a1 | 2009-07-27 21:17:23 | [diff] [blame] | 118 | #ifdef ARCH_CPU_64_BITS |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 119 | Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
| 120 | Atomic64 old_value, |
| 121 | Atomic64 new_value); |
| 122 | Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value); |
| 123 | Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment); |
| 124 | Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment); |
| 125 | |
| 126 | Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
| 127 | Atomic64 old_value, |
| 128 | Atomic64 new_value); |
| 129 | Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, |
| 130 | Atomic64 old_value, |
| 131 | Atomic64 new_value); |
| 132 | void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value); |
| 133 | void Acquire_Store(volatile Atomic64* ptr, Atomic64 value); |
| 134 | void Release_Store(volatile Atomic64* ptr, Atomic64 value); |
| 135 | Atomic64 NoBarrier_Load(volatile const Atomic64* ptr); |
| 136 | Atomic64 Acquire_Load(volatile const Atomic64* ptr); |
| 137 | Atomic64 Release_Load(volatile const Atomic64* ptr); |
[email protected] | 616f9a1 | 2009-07-27 21:17:23 | [diff] [blame] | 138 | #endif // ARCH_CPU_64_BITS |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 139 | |
[email protected] | abe936a3 | 2014-04-13 17:36:27 | [diff] [blame] | 140 | } // namespace subtle |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 141 | } // namespace base |
| 142 | |
jfb | 57a4e4a | 2014-10-14 15:06:05 | [diff] [blame] | 143 | // The following x86 CPU features are used in atomicops_internals_x86_gcc.h, but |
| 144 | // this file is duplicated inside of Chrome: protobuf and tcmalloc rely on the |
| 145 | // struct being present at link time. Some parts of Chrome can currently use the |
| 146 | // portable interface whereas others still use GCC one. The include guards are |
| 147 | // the same as in atomicops_internals_x86_gcc.cc. |
| 148 | #if defined(__i386__) || defined(__x86_64__) |
| 149 | // This struct is not part of the public API of this module; clients may not |
| 150 | // use it. (However, it's exported via BASE_EXPORT because clients implicitly |
| 151 | // do use it at link time by inlining these functions.) |
| 152 | // Features of this x86. Values may not be correct before main() is run, |
| 153 | // but are set conservatively. |
| 154 | struct AtomicOps_x86CPUFeatureStruct { |
| 155 | bool has_amd_lock_mb_bug; // Processor has AMD memory-barrier bug; do lfence |
| 156 | // after acquire compare-and-swap. |
| 157 | // The following fields are unused by Chrome's base implementation but are |
| 158 | // still used by copies of the same code in other parts of the code base. This |
| 159 | // causes an ODR violation, and the other code is likely reading invalid |
| 160 | // memory. |
| 161 | // TODO(jfb) Delete these fields once the rest of the Chrome code base doesn't |
| 162 | // depend on them. |
| 163 | bool has_sse2; // Processor has SSE2. |
| 164 | bool has_cmpxchg16b; // Processor supports cmpxchg16b instruction. |
| 165 | }; |
| 166 | BASE_EXPORT extern struct AtomicOps_x86CPUFeatureStruct |
| 167 | AtomicOps_Internalx86CPUFeatures; |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 168 | #endif |
| 169 | |
jfb | 57a4e4a | 2014-10-14 15:06:05 | [diff] [blame] | 170 | // Try to use a portable implementation based on C++11 atomics. |
| 171 | // |
| 172 | // Some toolchains support C++11 language features without supporting library |
| 173 | // features (recent compiler, older STL). Whitelist libstdc++ and libc++ that we |
| 174 | // know will have <atomic> when compiling C++11. |
| 175 | #if ((__cplusplus >= 201103L) && \ |
| 176 | ((defined(__GLIBCXX__) && (__GLIBCXX__ > 20110216)) || \ |
| 177 | (defined(_LIBCPP_VERSION) && (_LIBCPP_STD_VER >= 11)))) |
| 178 | # include "base/atomicops_internals_portable.h" |
| 179 | #else // Otherwise use a platform specific implementation. |
| 180 | # if defined(THREAD_SANITIZER) |
| 181 | # error "Thread sanitizer must use the portable atomic operations" |
| 182 | # elif (defined(OS_WIN) && defined(COMPILER_MSVC) && \ |
| 183 | defined(ARCH_CPU_X86_FAMILY)) |
| 184 | # include "base/atomicops_internals_x86_msvc.h" |
| 185 | # elif defined(OS_MACOSX) |
| 186 | # include "base/atomicops_internals_mac.h" |
| 187 | # elif defined(OS_NACL) |
| 188 | # include "base/atomicops_internals_gcc.h" |
| 189 | # elif defined(COMPILER_GCC) && defined(ARCH_CPU_ARMEL) |
| 190 | # include "base/atomicops_internals_arm_gcc.h" |
| 191 | # elif defined(COMPILER_GCC) && defined(ARCH_CPU_ARM64) |
| 192 | # include "base/atomicops_internals_arm64_gcc.h" |
| 193 | # elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_FAMILY) |
| 194 | # include "base/atomicops_internals_x86_gcc.h" |
| 195 | # elif (defined(COMPILER_GCC) && \ |
| 196 | (defined(ARCH_CPU_MIPS_FAMILY) || defined(ARCH_CPU_MIPS64_FAMILY))) |
| 197 | # include "base/atomicops_internals_mips_gcc.h" |
| 198 | # else |
| 199 | # error "Atomic operations are not supported on your platform" |
| 200 | # endif |
| 201 | #endif // Portable / non-portable includes. |
| 202 | |
[email protected] | 0c6f3b0c | 2011-04-15 06:15:04 | [diff] [blame] | 203 | // On some platforms we need additional declarations to make |
| 204 | // AtomicWord compatible with our other Atomic* types. |
| 205 | #if defined(OS_MACOSX) || defined(OS_OPENBSD) |
| 206 | #include "base/atomicops_internals_atomicword_compat.h" |
| 207 | #endif |
| 208 | |
[email protected] | 611dbe0 | 2008-08-05 09:57:36 | [diff] [blame] | 209 | #endif // BASE_ATOMICOPS_H_ |