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/platform_macros.h> 60 61 namespace google { 62 namespace protobuf { 63 namespace internal { 64 65 typedef int32 Atomic32; 66 #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT 67 // We need to be able to go between Atomic64 and AtomicWord implicitly. This 68 // means Atomic64 and AtomicWord should be the same type on 64-bit. 69 #if defined(__ILP32__) || defined(GOOGLE_PROTOBUF_OS_NACL) || defined(GOOGLE_PROTOBUF_ARCH_SPARC) 70 // NaCl's intptr_t is not actually 64-bits on 64-bit! 71 // http://code.google.com/p/nativeclient/issues/detail?id=1162 72 // sparcv9's pointer type is 32bits 73 typedef int64 Atomic64; 74 #else 75 typedef intptr_t Atomic64; 76 #endif 77 #endif 78 79 // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or 80 // Atomic64 routines below, depending on your architecture. 81 typedef intptr_t AtomicWord; 82 83 // Atomically execute: 84 // result = *ptr; 85 // if (*ptr == old_value) 86 // *ptr = new_value; 87 // return result; 88 // 89 // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value". 90 // Always return the old value of "*ptr" 91 // 92 // This routine implies no memory barriers. 93 Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, 94 Atomic32 old_value, 95 Atomic32 new_value); 96 97 // Atomically store new_value into *ptr, returning the previous value held in 98 // *ptr. This routine implies no memory barriers. 99 Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value); 100 101 // Atomically increment *ptr by "increment". Returns the new value of 102 // *ptr with the increment applied. This routine implies no memory barriers. 103 Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment); 104 105 Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, 106 Atomic32 increment); 107 108 // These following lower-level operations are typically useful only to people 109 // implementing higher-level synchronization operations like spinlocks, 110 // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or 111 // a store with appropriate memory-ordering instructions. "Acquire" operations 112 // ensure that no later memory access can be reordered ahead of the operation. 113 // "Release" operations ensure that no previous memory access can be reordered 114 // after the operation. "Barrier" operations have both "Acquire" and "Release" 115 // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory 116 // access. 117 Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, 118 Atomic32 old_value, 119 Atomic32 new_value); 120 Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, 121 Atomic32 old_value, 122 Atomic32 new_value); 123 124 #if defined(__MINGW32__) && defined(MemoryBarrier) 125 #undef MemoryBarrier 126 #endif 127 void MemoryBarrier(); 128 void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value); 129 void Acquire_Store(volatile Atomic32* ptr, Atomic32 value); 130 void Release_Store(volatile Atomic32* ptr, Atomic32 value); 131 132 Atomic32 NoBarrier_Load(volatile const Atomic32* ptr); 133 Atomic32 Acquire_Load(volatile const Atomic32* ptr); 134 Atomic32 Release_Load(volatile const Atomic32* ptr); 135 136 // 64-bit atomic operations (only available on 64-bit processors). 137 #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT 138 Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, 139 Atomic64 old_value, 140 Atomic64 new_value); 141 Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value); 142 Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment); 143 Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment); 144 145 Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, 146 Atomic64 old_value, 147 Atomic64 new_value); 148 Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, 149 Atomic64 old_value, 150 Atomic64 new_value); 151 void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value); 152 void Acquire_Store(volatile Atomic64* ptr, Atomic64 value); 153 void Release_Store(volatile Atomic64* ptr, Atomic64 value); 154 Atomic64 NoBarrier_Load(volatile const Atomic64* ptr); 155 Atomic64 Acquire_Load(volatile const Atomic64* ptr); 156 Atomic64 Release_Load(volatile const Atomic64* ptr); 157 #endif // GOOGLE_PROTOBUF_ARCH_64_BIT 158 159 } // namespace internal 160 } // namespace protobuf 161 } // namespace google 162 163 // Include our platform specific implementation. 164 #define GOOGLE_PROTOBUF_ATOMICOPS_ERROR \ 165 #error "Atomic operations are not supported on your platform" 166 167 // ThreadSanitizer, http://clang.llvm.org/docs/ThreadSanitizer.html. 168 #if defined(THREAD_SANITIZER) 169 #include <google/protobuf/stubs/atomicops_internals_tsan.h> 170 // MSVC. 171 #elif defined(_MSC_VER) 172 #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64) 173 #include <google/protobuf/stubs/atomicops_internals_x86_msvc.h> 174 #else 175 GOOGLE_PROTOBUF_ATOMICOPS_ERROR 176 #endif 177 178 // Solaris 179 #elif defined(GOOGLE_PROTOBUF_OS_SOLARIS) 180 #include <google/protobuf/stubs/atomicops_internals_solaris.h> 181 182 // Apple. 183 #elif defined(GOOGLE_PROTOBUF_OS_APPLE) 184 #include <google/protobuf/stubs/atomicops_internals_macosx.h> 185 186 // GCC. 187 #elif defined(__GNUC__) 188 #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64) 189 #include <google/protobuf/stubs/atomicops_internals_x86_gcc.h> 190 #elif defined(GOOGLE_PROTOBUF_ARCH_ARM) && defined(__linux__) 191 #include <google/protobuf/stubs/atomicops_internals_arm_gcc.h> 192 #elif defined(GOOGLE_PROTOBUF_ARCH_AARCH64) 193 #include <google/protobuf/stubs/atomicops_internals_arm64_gcc.h> 194 #elif defined(GOOGLE_PROTOBUF_ARCH_ARM_QNX) 195 #include <google/protobuf/stubs/atomicops_internals_arm_qnx.h> 196 #elif defined(GOOGLE_PROTOBUF_ARCH_MIPS) || defined(GOOGLE_PROTOBUF_ARCH_MIPS64) 197 #include <google/protobuf/stubs/atomicops_internals_mips_gcc.h> 198 #elif defined(__native_client__) 199 #include <google/protobuf/stubs/atomicops_internals_pnacl.h> 200 #elif (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)) 201 #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h> 202 #elif defined(__clang__) 203 #if __has_extension(c_atomic) 204 #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h> 205 #else 206 GOOGLE_PROTOBUF_ATOMICOPS_ERROR 207 #endif 208 #else 209 GOOGLE_PROTOBUF_ATOMICOPS_ERROR 210 #endif 211 212 // Unknown. 213 #else 214 GOOGLE_PROTOBUF_ATOMICOPS_ERROR 215 #endif 216 217 // On some platforms we need additional declarations to make AtomicWord 218 // compatible with our other Atomic* types. 219 #if defined(GOOGLE_PROTOBUF_OS_APPLE) 220 #include <google/protobuf/stubs/atomicops_internals_atomicword_compat.h> 221 #endif 222 223 #undef GOOGLE_PROTOBUF_ATOMICOPS_ERROR 224 225 #endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY 226 227 #endif // GOOGLE_PROTOBUF_ATOMICOPS_H_ 228