| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2012 Google Inc. All rights reserved. | 2 // Copyright 2012 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // This file is an internal atomic implementation, use atomicops.h instead. | 31 // This file is an internal atomic implementation, use atomicops.h instead. |
| 32 | 32 |
| 33 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_PNACL_H_ | 33 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_GENERIC_C11_ATOMIC_H_ |
| 34 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_PNACL_H_ | 34 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_GENERIC_C11_ATOMIC_H_ |
| 35 | 35 |
| 36 #include <atomic> | 36 #include <atomic> |
| 37 | 37 |
| 38 namespace google { | 38 namespace google { |
| 39 namespace protobuf { | 39 namespace protobuf { |
| 40 namespace internal { | 40 namespace internal { |
| 41 | 41 |
| 42 // This implementation is transitional and maintains the original API for | 42 // This implementation is transitional and maintains the original API for |
| 43 // atomicops.h. This requires casting memory locations to the atomic types, and | 43 // atomicops.h. This requires casting memory locations to the atomic types, and |
| 44 // assumes that the API and the C++11 implementation are layout-compatible, | 44 // assumes that the API and the C++11 implementation are layout-compatible, |
| 45 // which isn't true for all implementations or hardware platforms. The static | 45 // which isn't true for all implementations or hardware platforms. The static |
| 46 // assertion should detect this issue, were it to fire then this header | 46 // assertion should detect this issue, were it to fire then this header |
| 47 // shouldn't be used. | 47 // shouldn't be used. |
| 48 // | 48 // |
| 49 // TODO(jfb) If this header manages to stay committed then the API should be | 49 // TODO(jfb) If this header manages to stay committed then the API should be |
| 50 // modified, and all call sites updated. | 50 // modified, and all call sites updated. |
| 51 typedef volatile std::atomic<Atomic32>* AtomicLocation32; | 51 typedef volatile std::atomic<Atomic32>* AtomicLocation32; |
| 52 static_assert(sizeof(*(AtomicLocation32) nullptr) == sizeof(Atomic32), | 52 static_assert(sizeof(*(AtomicLocation32) nullptr) == sizeof(Atomic32), |
| 53 "incompatible 32-bit atomic layout"); | 53 "incompatible 32-bit atomic layout"); |
| 54 | 54 |
| 55 inline void MemoryBarrier() { | 55 inline void MemoryBarrierInternal() { |
| 56 #if defined(__GLIBCXX__) | 56 #if defined(__GLIBCXX__) |
| 57 // Work around libstdc++ bug 51038 where atomic_thread_fence was declared but | 57 // Work around libstdc++ bug 51038 where atomic_thread_fence was declared but |
| 58 // not defined, leading to the linker complaining about undefined references. | 58 // not defined, leading to the linker complaining about undefined references. |
| 59 __atomic_thread_fence(std::memory_order_seq_cst); | 59 __atomic_thread_fence(std::memory_order_seq_cst); |
| 60 #else | 60 #else |
| 61 std::atomic_thread_fence(std::memory_order_seq_cst); | 61 std::atomic_thread_fence(std::memory_order_seq_cst); |
| 62 #endif | 62 #endif |
| 63 } | 63 } |
| 64 | 64 |
| 65 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, | 65 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 std::memory_order_relaxed); | 112 std::memory_order_relaxed); |
| 113 return old_value; | 113 return old_value; |
| 114 } | 114 } |
| 115 | 115 |
| 116 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { | 116 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
| 117 ((AtomicLocation32)ptr)->store(value, std::memory_order_relaxed); | 117 ((AtomicLocation32)ptr)->store(value, std::memory_order_relaxed); |
| 118 } | 118 } |
| 119 | 119 |
| 120 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { | 120 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
| 121 ((AtomicLocation32)ptr)->store(value, std::memory_order_relaxed); | 121 ((AtomicLocation32)ptr)->store(value, std::memory_order_relaxed); |
| 122 MemoryBarrier(); | 122 MemoryBarrierInternal(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { | 125 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
| 126 ((AtomicLocation32)ptr)->store(value, std::memory_order_release); | 126 ((AtomicLocation32)ptr)->store(value, std::memory_order_release); |
| 127 } | 127 } |
| 128 | 128 |
| 129 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { | 129 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
| 130 return ((AtomicLocation32)ptr)->load(std::memory_order_relaxed); | 130 return ((AtomicLocation32)ptr)->load(std::memory_order_relaxed); |
| 131 } | 131 } |
| 132 | 132 |
| 133 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { | 133 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
| 134 return ((AtomicLocation32)ptr)->load(std::memory_order_acquire); | 134 return ((AtomicLocation32)ptr)->load(std::memory_order_acquire); |
| 135 } | 135 } |
| 136 | 136 |
| 137 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { | 137 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
| 138 MemoryBarrier(); | 138 MemoryBarrierInternal(); |
| 139 return ((AtomicLocation32)ptr)->load(std::memory_order_relaxed); | 139 return ((AtomicLocation32)ptr)->load(std::memory_order_relaxed); |
| 140 } | 140 } |
| 141 | 141 |
| 142 #if defined(GOOGLE_PROTOBUF_ARCH_64_BIT) | 142 #if defined(GOOGLE_PROTOBUF_ARCH_64_BIT) |
| 143 | 143 |
| 144 typedef volatile std::atomic<Atomic64>* AtomicLocation64; | 144 typedef volatile std::atomic<Atomic64>* AtomicLocation64; |
| 145 static_assert(sizeof(*(AtomicLocation64) nullptr) == sizeof(Atomic64), | 145 static_assert(sizeof(*(AtomicLocation64) nullptr) == sizeof(Atomic64), |
| 146 "incompatible 64-bit atomic layout"); | 146 "incompatible 64-bit atomic layout"); |
| 147 | 147 |
| 148 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, | 148 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 std::memory_order_relaxed); | 195 std::memory_order_relaxed); |
| 196 return old_value; | 196 return old_value; |
| 197 } | 197 } |
| 198 | 198 |
| 199 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { | 199 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { |
| 200 ((AtomicLocation64)ptr)->store(value, std::memory_order_relaxed); | 200 ((AtomicLocation64)ptr)->store(value, std::memory_order_relaxed); |
| 201 } | 201 } |
| 202 | 202 |
| 203 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { | 203 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { |
| 204 ((AtomicLocation64)ptr)->store(value, std::memory_order_relaxed); | 204 ((AtomicLocation64)ptr)->store(value, std::memory_order_relaxed); |
| 205 MemoryBarrier(); | 205 MemoryBarrierInternal(); |
| 206 } | 206 } |
| 207 | 207 |
| 208 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { | 208 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { |
| 209 ((AtomicLocation64)ptr)->store(value, std::memory_order_release); | 209 ((AtomicLocation64)ptr)->store(value, std::memory_order_release); |
| 210 } | 210 } |
| 211 | 211 |
| 212 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { | 212 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { |
| 213 return ((AtomicLocation64)ptr)->load(std::memory_order_relaxed); | 213 return ((AtomicLocation64)ptr)->load(std::memory_order_relaxed); |
| 214 } | 214 } |
| 215 | 215 |
| 216 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { | 216 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { |
| 217 return ((AtomicLocation64)ptr)->load(std::memory_order_acquire); | 217 return ((AtomicLocation64)ptr)->load(std::memory_order_acquire); |
| 218 } | 218 } |
| 219 | 219 |
| 220 inline Atomic64 Release_Load(volatile const Atomic64* ptr) { | 220 inline Atomic64 Release_Load(volatile const Atomic64* ptr) { |
| 221 MemoryBarrier(); | 221 MemoryBarrierInternal(); |
| 222 return ((AtomicLocation64)ptr)->load(std::memory_order_relaxed); | 222 return ((AtomicLocation64)ptr)->load(std::memory_order_relaxed); |
| 223 } | 223 } |
| 224 | 224 |
| 225 #endif // defined(GOOGLE_PROTOBUF_ARCH_64_BIT) | 225 #endif // defined(GOOGLE_PROTOBUF_ARCH_64_BIT) |
| 226 | 226 |
| 227 } // namespace internal | 227 } // namespace internal |
| 228 } // namespace protobuf | 228 } // namespace protobuf |
| 229 } // namespace google | 229 } // namespace google |
| 230 | 230 |
| 231 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_PNACL_H_ | 231 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_GENERIC_C11_ATOMIC_H_ |
| OLD | NEW |