Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2012 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // The routines exported by this module are subtle. If you use them, even if |
| 32 | // you get the code right, it will depend on careful reasoning about atomicity |
| 33 | // and memory ordering; it will be less readable, and harder to maintain. If |
| 34 | // you plan to use these routines, you should have a good reason, such as solid |
| 35 | // evidence that performance would otherwise suffer, or there being no |
| 36 | // alternative. You should assume only properties explicitly guaranteed by the |
| 37 | // specifications in this file. You are almost certainly _not_ writing code |
| 38 | // just for the x86; if you assume x86 semantics, x86 hardware bugs and |
| 39 | // implementations on other archtectures will cause your code to break. If you |
| 40 | // do not know what you are doing, avoid these routines, and use a Mutex. |
| 41 | // |
| 42 | // It is incorrect to make direct assignments to/from an atomic variable. |
| 43 | // You should use one of the Load or Store routines. The NoBarrier |
| 44 | // versions are provided when no barriers are needed: |
| 45 | // NoBarrier_Store() |
| 46 | // NoBarrier_Load() |
| 47 | // Although there are currently no compiler enforcement, you are encouraged |
| 48 | // to use these. |
| 49 | |
| 50 | // This header and the implementations for each platform (located in |
| 51 | // atomicops_internals_*) must be kept in sync with the upstream code (V8). |
| 52 | |
| 53 | #ifndef GOOGLE_PROTOBUF_ATOMICOPS_H_ |
| 54 | #define GOOGLE_PROTOBUF_ATOMICOPS_H_ |
| 55 | |
| 56 | // Don't include this file for people not concerned about thread safety. |
| 57 | #ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY |
| 58 | |
| 59 | #include <google/protobuf/stubs/common.h> |
| 60 | #include <google/protobuf/stubs/platform_macros.h> |
| 61 | |
| 62 | namespace google { |
| 63 | namespace protobuf { |
| 64 | namespace internal { |
| 65 | |
| 66 | #if defined(GOOGLE_PROTOBUF_ARCH_POWER) |
| 67 | #if defined(_LP64) || defined(__LP64__) |
| 68 | typedef int32 Atomic32; |
| 69 | typedef intptr_t Atomic64; |
| 70 | #else |
| 71 | typedef intptr_t Atomic32; |
| 72 | typedef int64 Atomic64; |
| 73 | #endif |
| 74 | #else |
| 75 | typedef int32 Atomic32; |
| 76 | #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT |
| 77 | // We need to be able to go between Atomic64 and AtomicWord implicitly. This |
| 78 | // means Atomic64 and AtomicWord should be the same type on 64-bit. |
| 79 | #if defined(__ILP32__) || defined(GOOGLE_PROTOBUF_OS_NACL) |
| 80 | // NaCl's intptr_t is not actually 64-bits on 64-bit! |
| 81 | // http://code.google.com/p/nativeclient/issues/detail?id=1162 |
| 82 | // sparcv9's pointer type is 32bits |
| 83 | typedef int64 Atomic64; |
| 84 | #else |
| 85 | typedef intptr_t Atomic64; |
| 86 | #endif |
| 87 | #endif |
| 88 | #endif |
| 89 | |
| 90 | // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or |
| 91 | // Atomic64 routines below, depending on your architecture. |
| 92 | typedef intptr_t AtomicWord; |
| 93 | |
| 94 | // Atomically execute: |
| 95 | // result = *ptr; |
| 96 | // if (*ptr == old_value) |
| 97 | // *ptr = new_value; |
| 98 | // return result; |
| 99 | // |
| 100 | // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value". |
| 101 | // Always return the old value of "*ptr" |
| 102 | // |
| 103 | // This routine implies no memory barriers. |
| 104 | Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
| 105 | Atomic32 old_value, |
| 106 | Atomic32 new_value); |
| 107 | |
| 108 | // Atomically store new_value into *ptr, returning the previous value held in |
| 109 | // *ptr. This routine implies no memory barriers. |
| 110 | Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value); |
| 111 | |
| 112 | // Atomically increment *ptr by "increment". Returns the new value of |
| 113 | // *ptr with the increment applied. This routine implies no memory barriers. |
| 114 | Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment); |
| 115 | |
| 116 | Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
| 117 | Atomic32 increment); |
| 118 | |
| 119 | // These following lower-level operations are typically useful only to people |
| 120 | // implementing higher-level synchronization operations like spinlocks, |
| 121 | // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or |
| 122 | // a store with appropriate memory-ordering instructions. "Acquire" operations |
| 123 | // ensure that no later memory access can be reordered ahead of the operation. |
| 124 | // "Release" operations ensure that no previous memory access can be reordered |
| 125 | // after the operation. "Barrier" operations have both "Acquire" and "Release" |
| 126 | // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory |
| 127 | // access. |
| 128 | Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
| 129 | Atomic32 old_value, |
| 130 | Atomic32 new_value); |
| 131 | Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
| 132 | Atomic32 old_value, |
| 133 | Atomic32 new_value); |
| 134 | |
| 135 | #if defined(__MINGW32__) && defined(MemoryBarrier) |
| 136 | #undef MemoryBarrier |
| 137 | #endif |
| 138 | void MemoryBarrier(); |
| 139 | void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value); |
| 140 | void Acquire_Store(volatile Atomic32* ptr, Atomic32 value); |
| 141 | void Release_Store(volatile Atomic32* ptr, Atomic32 value); |
| 142 | |
| 143 | Atomic32 NoBarrier_Load(volatile const Atomic32* ptr); |
| 144 | Atomic32 Acquire_Load(volatile const Atomic32* ptr); |
| 145 | Atomic32 Release_Load(volatile const Atomic32* ptr); |
| 146 | |
| 147 | // 64-bit atomic operations (only available on 64-bit processors). |
| 148 | #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT |
| 149 | Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
| 150 | Atomic64 old_value, |
| 151 | Atomic64 new_value); |
| 152 | Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value); |
| 153 | Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment); |
| 154 | Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment); |
| 155 | |
| 156 | Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
| 157 | Atomic64 old_value, |
| 158 | Atomic64 new_value); |
| 159 | Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, |
| 160 | Atomic64 old_value, |
| 161 | Atomic64 new_value); |
| 162 | void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value); |
| 163 | void Acquire_Store(volatile Atomic64* ptr, Atomic64 value); |
| 164 | void Release_Store(volatile Atomic64* ptr, Atomic64 value); |
| 165 | Atomic64 NoBarrier_Load(volatile const Atomic64* ptr); |
| 166 | Atomic64 Acquire_Load(volatile const Atomic64* ptr); |
| 167 | Atomic64 Release_Load(volatile const Atomic64* ptr); |
| 168 | #endif // GOOGLE_PROTOBUF_ARCH_64_BIT |
| 169 | |
| 170 | } // namespace internal |
| 171 | } // namespace protobuf |
| 172 | } // namespace google |
| 173 | |
| 174 | // Include our platform specific implementation. |
| 175 | #define GOOGLE_PROTOBUF_ATOMICOPS_ERROR \ |
| 176 | "Atomic operations are not supported on your platform" |
| 177 | |
| 178 | // ThreadSanitizer, http://clang.llvm.org/docs/ThreadSanitizer.html. |
| 179 | #if defined(THREAD_SANITIZER) |
| 180 | #include <google/protobuf/stubs/atomicops_internals_tsan.h> |
| 181 | // MSVC. |
| 182 | #elif defined(_MSC_VER) |
| 183 | #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64) |
| 184 | #include <google/protobuf/stubs/atomicops_internals_x86_msvc.h> |
| 185 | #else |
| 186 | #error GOOGLE_PROTOBUF_ATOMICOPS_ERROR |
| 187 | #endif |
| 188 | |
| 189 | // Solaris |
| 190 | #elif defined(GOOGLE_PROTOBUF_OS_SOLARIS) |
| 191 | #include <google/protobuf/stubs/atomicops_internals_solaris.h> |
| 192 | |
| 193 | // AIX |
| 194 | #elif defined(GOOGLE_PROTOBUF_OS_AIX) |
| 195 | #include <google/protobuf/stubs/atomicops_internals_power.h> |
| 196 | |
| 197 | // Apple. |
| 198 | #elif defined(GOOGLE_PROTOBUF_OS_APPLE) |
| 199 | #include <google/protobuf/stubs/atomicops_internals_macosx.h> |
| 200 | |
| 201 | // GCC. |
| 202 | #elif defined(__GNUC__) |
| 203 | #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64) |
| 204 | #include <google/protobuf/stubs/atomicops_internals_x86_gcc.h> |
| 205 | #elif defined(GOOGLE_PROTOBUF_ARCH_ARM) && defined(__linux__) |
| 206 | #include <google/protobuf/stubs/atomicops_internals_arm_gcc.h> |
| 207 | #elif defined(GOOGLE_PROTOBUF_ARCH_AARCH64) |
| 208 | #include <google/protobuf/stubs/atomicops_internals_arm64_gcc.h> |
| 209 | #elif defined(GOOGLE_PROTOBUF_ARCH_ARM_QNX) |
| 210 | #include <google/protobuf/stubs/atomicops_internals_arm_qnx.h> |
| 211 | #elif defined(GOOGLE_PROTOBUF_ARCH_MIPS) || defined(GOOGLE_PROTOBUF_ARCH_MIPS64) |
| 212 | #include <google/protobuf/stubs/atomicops_internals_mips_gcc.h> |
| 213 | #elif defined(GOOGLE_PROTOBUF_ARCH_POWER) |
| 214 | #include <google/protobuf/stubs/atomicops_internals_power.h> |
| 215 | #elif defined(__native_client__) |
| 216 | #include <google/protobuf/stubs/atomicops_internals_pnacl.h> |
| 217 | #elif (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) |
| 218 | #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h> |
| 219 | #elif defined(__clang__) |
| 220 | #if __has_extension(c_atomic) |
| 221 | #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h> |
| 222 | #else |
| 223 | #error GOOGLE_PROTOBUF_ATOMICOPS_ERROR |
| 224 | #endif |
| 225 | #else |
| 226 | #error GOOGLE_PROTOBUF_ATOMICOPS_ERROR |
| 227 | #endif |
| 228 | |
| 229 | // Unknown. |
| 230 | #else |
| 231 | #error GOOGLE_PROTOBUF_ATOMICOPS_ERROR |
| 232 | #endif |
| 233 | |
| 234 | // On some platforms we need additional declarations to make AtomicWord |
| 235 | // compatible with our other Atomic* types. |
| 236 | #if defined(GOOGLE_PROTOBUF_OS_APPLE) |
| 237 | #include <google/protobuf/stubs/atomicops_internals_atomicword_compat.h> |
| 238 | #endif |
| 239 | |
| 240 | #undef GOOGLE_PROTOBUF_ATOMICOPS_ERROR |
| 241 | |
| 242 | #endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY |
| 243 | |
| 244 | #endif // GOOGLE_PROTOBUF_ATOMICOPS_H_ |