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) 2006-2008 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 | #ifndef BASE_ATOMICOPS_INTERNALS_X86_MSVC_H_ |
michael@0 | 8 | #define BASE_ATOMICOPS_INTERNALS_X86_MSVC_H_ |
michael@0 | 9 | |
michael@0 | 10 | #include <windows.h> |
michael@0 | 11 | |
michael@0 | 12 | namespace base { |
michael@0 | 13 | namespace subtle { |
michael@0 | 14 | |
michael@0 | 15 | inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 16 | Atomic32 old_value, |
michael@0 | 17 | Atomic32 new_value) { |
michael@0 | 18 | LONG result = InterlockedCompareExchange( |
michael@0 | 19 | reinterpret_cast<volatile LONG*>(ptr), |
michael@0 | 20 | static_cast<LONG>(new_value), |
michael@0 | 21 | static_cast<LONG>(old_value)); |
michael@0 | 22 | return static_cast<Atomic32>(result); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
michael@0 | 26 | Atomic32 new_value) { |
michael@0 | 27 | LONG result = InterlockedExchange( |
michael@0 | 28 | reinterpret_cast<volatile LONG*>(ptr), |
michael@0 | 29 | static_cast<LONG>(new_value)); |
michael@0 | 30 | return static_cast<Atomic32>(result); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 34 | Atomic32 increment) { |
michael@0 | 35 | return InterlockedExchangeAdd( |
michael@0 | 36 | reinterpret_cast<volatile LONG*>(ptr), |
michael@0 | 37 | static_cast<LONG>(increment)) + increment; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, |
michael@0 | 41 | Atomic32 increment) { |
michael@0 | 42 | return Barrier_AtomicIncrement(ptr, increment); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | #if defined(_MSC_VER) && (_MSC_VER < 1400) |
michael@0 | 46 | #error "We require at least vs2005 for MemoryBarrier" |
michael@0 | 47 | #endif |
michael@0 | 48 | inline void MemoryBarrier() { |
michael@0 | 49 | // We use MemoryBarrier from WinNT.h |
michael@0 | 50 | ::MemoryBarrier(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 54 | Atomic32 old_value, |
michael@0 | 55 | Atomic32 new_value) { |
michael@0 | 56 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
michael@0 | 60 | Atomic32 old_value, |
michael@0 | 61 | Atomic32 new_value) { |
michael@0 | 62 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 66 | *ptr = value; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 70 | NoBarrier_AtomicExchange(ptr, value); |
michael@0 | 71 | // acts as a barrier in this implementation |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
michael@0 | 75 | *ptr = value; // works w/o barrier for current Intel chips as of June 2005 |
michael@0 | 76 | // See comments in Atomic64 version of Release_Store() below. |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
michael@0 | 80 | return *ptr; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
michael@0 | 84 | Atomic32 value = *ptr; |
michael@0 | 85 | return value; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
michael@0 | 89 | MemoryBarrier(); |
michael@0 | 90 | return *ptr; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | #if defined(_WIN64) |
michael@0 | 94 | |
michael@0 | 95 | // 64-bit low-level operations on 64-bit platform. |
michael@0 | 96 | |
michael@0 | 97 | COMPILE_ASSERT(sizeof(Atomic64) == sizeof(PVOID), atomic_word_is_atomic); |
michael@0 | 98 | |
michael@0 | 99 | inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 100 | Atomic64 old_value, |
michael@0 | 101 | Atomic64 new_value) { |
michael@0 | 102 | PVOID result = InterlockedCompareExchangePointer( |
michael@0 | 103 | reinterpret_cast<volatile PVOID*>(ptr), |
michael@0 | 104 | reinterpret_cast<PVOID>(new_value), reinterpret_cast<PVOID>(old_value)); |
michael@0 | 105 | return reinterpret_cast<Atomic64>(result); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, |
michael@0 | 109 | Atomic64 new_value) { |
michael@0 | 110 | PVOID result = InterlockedExchangePointer( |
michael@0 | 111 | reinterpret_cast<volatile PVOID*>(ptr), |
michael@0 | 112 | reinterpret_cast<PVOID>(new_value)); |
michael@0 | 113 | return reinterpret_cast<Atomic64>(result); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, |
michael@0 | 117 | Atomic64 increment) { |
michael@0 | 118 | return InterlockedExchangeAdd64( |
michael@0 | 119 | reinterpret_cast<volatile LONGLONG*>(ptr), |
michael@0 | 120 | static_cast<LONGLONG>(increment)) + increment; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, |
michael@0 | 124 | Atomic64 increment) { |
michael@0 | 125 | return Barrier_AtomicIncrement(ptr, increment); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 129 | Atomic64 old_value, |
michael@0 | 130 | Atomic64 new_value) { |
michael@0 | 131 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, |
michael@0 | 135 | Atomic64 old_value, |
michael@0 | 136 | Atomic64 new_value) { |
michael@0 | 137 | return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 141 | *ptr = value; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 145 | NoBarrier_AtomicExchange(ptr, value); |
michael@0 | 146 | // acts as a barrier in this implementation |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { |
michael@0 | 150 | *ptr = value; // works w/o barrier for current Intel chips as of June 2005 |
michael@0 | 151 | |
michael@0 | 152 | // When new chips come out, check: |
michael@0 | 153 | // IA-32 Intel Architecture Software Developer's Manual, Volume 3: |
michael@0 | 154 | // System Programming Guide, Chatper 7: Multiple-processor management, |
michael@0 | 155 | // Section 7.2, Memory Ordering. |
michael@0 | 156 | // Last seen at: |
michael@0 | 157 | // http://developer.intel.com/design/pentium4/manuals/index_new.htm |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { |
michael@0 | 161 | return *ptr; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { |
michael@0 | 165 | Atomic64 value = *ptr; |
michael@0 | 166 | return value; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | inline Atomic64 Release_Load(volatile const Atomic64* ptr) { |
michael@0 | 170 | MemoryBarrier(); |
michael@0 | 171 | return *ptr; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | #endif // defined(_WIN64) |
michael@0 | 175 | |
michael@0 | 176 | } // namespace base::subtle |
michael@0 | 177 | } // namespace base |
michael@0 | 178 | |
michael@0 | 179 | #endif // BASE_ATOMICOPS_INTERNALS_X86_MSVC_H_ |