Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=8 et : |
michael@0 | 3 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | // This file is an internal atomic implementation, use |
michael@0 | 9 | // base/atomicops.h instead. |
michael@0 | 10 | // |
michael@0 | 11 | // This is a very slow fallback implementation of atomic operations |
michael@0 | 12 | // that uses a mutex instead of atomic instructions. |
michael@0 | 13 | // |
michael@0 | 14 | // (NB: a small "optimization" here would be using a spinlock instead |
michael@0 | 15 | // of a blocking mutex, but it's probably not worth the time.) |
michael@0 | 16 | |
michael@0 | 17 | #ifndef base_atomicops_internals_mutex_h |
michael@0 | 18 | #define base_atomicops_internals_mutex_h |
michael@0 | 19 | |
michael@0 | 20 | #include "base/lock.h" |
michael@0 | 21 | |
michael@0 | 22 | namespace base { |
michael@0 | 23 | namespace subtle { |
michael@0 | 24 | |
michael@0 | 25 | extern Lock gAtomicsMutex; |
michael@0 | 26 | |
michael@0 | 27 | template<typename T> |
michael@0 | 28 | T Locked_CAS(volatile T* ptr, T old_value, T new_value) { |
michael@0 | 29 | AutoLock _(gAtomicsMutex); |
michael@0 | 30 | |
michael@0 | 31 | T current_value = *ptr; |
michael@0 | 32 | if (current_value == old_value) |
michael@0 | 33 | *ptr = new_value; |
michael@0 | 34 | |
michael@0 | 35 | return current_value; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | template<typename T> |
michael@0 | 39 | T Locked_AtomicExchange(volatile T* ptr, T new_value) { |
michael@0 | 40 | AutoLock _(gAtomicsMutex); |
michael@0 | 41 | |
michael@0 | 42 | T current_value = *ptr; |
michael@0 | 43 | *ptr = new_value; |
michael@0 | 44 | return current_value; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | template<typename T> |
michael@0 | 48 | T Locked_AtomicIncrement(volatile T* ptr, T increment) { |
michael@0 | 49 | AutoLock _(gAtomicsMutex); |
michael@0 | 50 | return *ptr += increment; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | template<typename T> |
michael@0 | 54 | void Locked_Store(volatile T* ptr, T value) { |
michael@0 | 55 | AutoLock _(gAtomicsMutex); |
michael@0 | 56 | *ptr = value; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | template<typename T> |
michael@0 | 60 | T Locked_Load(volatile const T* ptr) { |
michael@0 | 61 | AutoLock _(gAtomicsMutex); |
michael@0 | 62 | return *ptr; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 66 | Atomic32 old_value, |
michael@0 | 67 | Atomic32 new_value) { |
michael@0 | 68 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
michael@0 | 72 | Atomic32 new_value) { |
michael@0 | 73 | return Locked_AtomicExchange(ptr, new_value); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 77 | Atomic32 increment) { |
michael@0 | 78 | return Locked_AtomicIncrement(ptr, increment); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 82 | Atomic32 increment) { |
michael@0 | 83 | return Locked_AtomicIncrement(ptr, increment); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 87 | Atomic32 old_value, |
michael@0 | 88 | Atomic32 new_value) { |
michael@0 | 89 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 93 | Atomic32 old_value, |
michael@0 | 94 | Atomic32 new_value) { |
michael@0 | 95 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 99 | return Locked_Store(ptr, value); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | inline void MemoryBarrier() { |
michael@0 | 103 | AutoLock _(gAtomicsMutex); |
michael@0 | 104 | // lock/unlock work as a barrier here |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 108 | return Locked_Store(ptr, value); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 112 | return Locked_Store(ptr, value); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
michael@0 | 116 | return Locked_Load(ptr); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
michael@0 | 120 | return NoBarrier_Load(ptr); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
michael@0 | 124 | return Locked_Load(ptr); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | #ifdef ARCH_CPU_64_BITS |
michael@0 | 128 | |
michael@0 | 129 | inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 130 | Atomic64 old_value, |
michael@0 | 131 | Atomic64 new_value) { |
michael@0 | 132 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, |
michael@0 | 136 | Atomic64 new_value) { |
michael@0 | 137 | return Locked_AtomicExchange(ptr, new_value); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, |
michael@0 | 141 | Atomic64 increment) { |
michael@0 | 142 | return Locked_AtomicIncrement(ptr, increment); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, |
michael@0 | 146 | Atomic64 increment) { |
michael@0 | 147 | return Locked_AtomicIncrement(ptr, increment); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 151 | return Locked_Store(ptr, value); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 155 | Atomic64 old_value, |
michael@0 | 156 | Atomic64 new_value) { |
michael@0 | 157 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 161 | return Locked_Store(ptr, value); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 165 | return Locked_Store(ptr, value); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { |
michael@0 | 169 | return Locked_Load(ptr); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { |
michael@0 | 173 | return Locked_Load(ptr); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | inline Atomic64 Release_Load(volatile const Atomic64* ptr) { |
michael@0 | 177 | return Locked_Load(ptr); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | #endif // ARCH_CPU_64_BITS |
michael@0 | 181 | |
michael@0 | 182 | #ifdef OS_MACOSX |
michael@0 | 183 | // From atomicops_internals_x86_macosx.h: |
michael@0 | 184 | // |
michael@0 | 185 | // MacOS uses long for intptr_t, AtomicWord and Atomic32 are always |
michael@0 | 186 | // different on the Mac, even when they are the same size. We need |
michael@0 | 187 | // to explicitly cast from AtomicWord to Atomic32/64 to implement |
michael@0 | 188 | // the AtomicWord interface. |
michael@0 | 189 | |
michael@0 | 190 | inline AtomicWord NoBarrier_CompareAndSwap(volatile AtomicWord* ptr, |
michael@0 | 191 | AtomicWord old_value, |
michael@0 | 192 | AtomicWord new_value) { |
michael@0 | 193 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | inline AtomicWord NoBarrier_AtomicExchange(volatile AtomicWord* ptr, |
michael@0 | 197 | AtomicWord new_value) { |
michael@0 | 198 | return Locked_AtomicExchange(ptr, new_value); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | inline AtomicWord NoBarrier_AtomicIncrement(volatile AtomicWord* ptr, |
michael@0 | 202 | AtomicWord increment) { |
michael@0 | 203 | return Locked_AtomicIncrement(ptr, increment); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | inline AtomicWord Barrier_AtomicIncrement(volatile AtomicWord* ptr, |
michael@0 | 207 | AtomicWord increment) { |
michael@0 | 208 | return Locked_AtomicIncrement(ptr, increment); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | inline AtomicWord Acquire_CompareAndSwap(volatile AtomicWord* ptr, |
michael@0 | 212 | AtomicWord old_value, |
michael@0 | 213 | AtomicWord new_value) { |
michael@0 | 214 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | inline AtomicWord Release_CompareAndSwap(volatile AtomicWord* ptr, |
michael@0 | 218 | AtomicWord old_value, |
michael@0 | 219 | AtomicWord new_value) { |
michael@0 | 220 | return Locked_CAS(ptr, old_value, new_value); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | inline void NoBarrier_Store(volatile AtomicWord *ptr, AtomicWord value) { |
michael@0 | 224 | return Locked_Store(ptr, value); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | inline void Acquire_Store(volatile AtomicWord* ptr, AtomicWord value) { |
michael@0 | 228 | return Locked_Store(ptr, value); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | inline void Release_Store(volatile AtomicWord* ptr, AtomicWord value) { |
michael@0 | 232 | return Locked_Store(ptr, value); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | inline AtomicWord NoBarrier_Load(volatile const AtomicWord *ptr) { |
michael@0 | 236 | return Locked_Load(ptr); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | inline AtomicWord Acquire_Load(volatile const AtomicWord* ptr) { |
michael@0 | 240 | return Locked_Load(ptr); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | inline AtomicWord Release_Load(volatile const AtomicWord* ptr) { |
michael@0 | 244 | return Locked_Load(ptr); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | #endif // OS_MACOSX |
michael@0 | 248 | |
michael@0 | 249 | } // namespace subtle |
michael@0 | 250 | } // namespace base |
michael@0 | 251 | |
michael@0 | 252 | #endif // base_atomicops_internals_mutex_h |