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