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