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 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // This file is an internal atomic implementation, use base/atomicops.h instead. |
michael@0 | 6 | // |
michael@0 | 7 | // LinuxKernelCmpxchg and Barrier_AtomicIncrement are from Google Gears. |
michael@0 | 8 | |
michael@0 | 9 | #ifndef BASE_ATOMICOPS_INTERNALS_ARM_GCC_H_ |
michael@0 | 10 | #define BASE_ATOMICOPS_INTERNALS_ARM_GCC_H_ |
michael@0 | 11 | |
michael@0 | 12 | namespace base { |
michael@0 | 13 | namespace subtle { |
michael@0 | 14 | |
michael@0 | 15 | // 0xffff0fc0 is the hard coded address of a function provided by |
michael@0 | 16 | // the kernel which implements an atomic compare-exchange. On older |
michael@0 | 17 | // ARM architecture revisions (pre-v6) this may be implemented using |
michael@0 | 18 | // a syscall. This address is stable, and in active use (hard coded) |
michael@0 | 19 | // by at least glibc-2.7 and the Android C library. |
michael@0 | 20 | typedef Atomic32 (*LinuxKernelCmpxchgFunc)(Atomic32 old_value, |
michael@0 | 21 | Atomic32 new_value, |
michael@0 | 22 | volatile Atomic32* ptr); |
michael@0 | 23 | LinuxKernelCmpxchgFunc pLinuxKernelCmpxchg __attribute__((weak)) = |
michael@0 | 24 | (LinuxKernelCmpxchgFunc) 0xffff0fc0; |
michael@0 | 25 | |
michael@0 | 26 | typedef void (*LinuxKernelMemoryBarrierFunc)(void); |
michael@0 | 27 | LinuxKernelMemoryBarrierFunc pLinuxKernelMemoryBarrier __attribute__((weak)) = |
michael@0 | 28 | (LinuxKernelMemoryBarrierFunc) 0xffff0fa0; |
michael@0 | 29 | |
michael@0 | 30 | |
michael@0 | 31 | inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 32 | Atomic32 old_value, |
michael@0 | 33 | Atomic32 new_value) { |
michael@0 | 34 | Atomic32 prev_value = *ptr; |
michael@0 | 35 | do { |
michael@0 | 36 | if (!pLinuxKernelCmpxchg(old_value, new_value, |
michael@0 | 37 | const_cast<Atomic32*>(ptr))) { |
michael@0 | 38 | return old_value; |
michael@0 | 39 | } |
michael@0 | 40 | prev_value = *ptr; |
michael@0 | 41 | } while (prev_value == old_value); |
michael@0 | 42 | return prev_value; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
michael@0 | 46 | Atomic32 new_value) { |
michael@0 | 47 | Atomic32 old_value; |
michael@0 | 48 | do { |
michael@0 | 49 | old_value = *ptr; |
michael@0 | 50 | } while (pLinuxKernelCmpxchg(old_value, new_value, |
michael@0 | 51 | const_cast<Atomic32*>(ptr))); |
michael@0 | 52 | return old_value; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 56 | Atomic32 increment) { |
michael@0 | 57 | return Barrier_AtomicIncrement(ptr, increment); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 61 | Atomic32 increment) { |
michael@0 | 62 | for (;;) { |
michael@0 | 63 | // Atomic exchange the old value with an incremented one. |
michael@0 | 64 | Atomic32 old_value = *ptr; |
michael@0 | 65 | Atomic32 new_value = old_value + increment; |
michael@0 | 66 | if (pLinuxKernelCmpxchg(old_value, new_value, |
michael@0 | 67 | const_cast<Atomic32*>(ptr)) == 0) { |
michael@0 | 68 | // The exchange took place as expected. |
michael@0 | 69 | return new_value; |
michael@0 | 70 | } |
michael@0 | 71 | // Otherwise, *ptr changed mid-loop and we need to retry. |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 77 | Atomic32 old_value, |
michael@0 | 78 | Atomic32 new_value) { |
michael@0 | 79 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 83 | Atomic32 old_value, |
michael@0 | 84 | Atomic32 new_value) { |
michael@0 | 85 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 89 | *ptr = value; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | inline void MemoryBarrier() { |
michael@0 | 93 | pLinuxKernelMemoryBarrier(); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 97 | *ptr = value; |
michael@0 | 98 | MemoryBarrier(); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 102 | MemoryBarrier(); |
michael@0 | 103 | *ptr = value; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
michael@0 | 107 | return *ptr; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
michael@0 | 111 | Atomic32 value = *ptr; |
michael@0 | 112 | MemoryBarrier(); |
michael@0 | 113 | return value; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
michael@0 | 117 | MemoryBarrier(); |
michael@0 | 118 | return *ptr; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | } // namespace base::subtle |
michael@0 | 122 | } // namespace base |
michael@0 | 123 | |
michael@0 | 124 | #endif // BASE_ATOMICOPS_INTERNALS_ARM_GCC_H_ |