blob: fe5984fd1b11b55a7676bb884d0c4db4cbe6845d [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
deanm@google.com1579ec72008-08-05 18:57:36 +09004
5// For atomic operations on reference counts, see atomic_refcount.h.
6// For atomic operations on sequence numbers, see atomic_sequence_num.h.
7
8// The routines exported by this module are subtle. If you use them, even if
9// you get the code right, it will depend on careful reasoning about atomicity
10// and memory ordering; it will be less readable, and harder to maintain. If
11// you plan to use these routines, you should have a good reason, such as solid
12// evidence that performance would otherwise suffer, or there being no
13// alternative. You should assume only properties explicitly guaranteed by the
14// specifications in this file. You are almost certainly _not_ writing code
15// just for the x86; if you assume x86 semantics, x86 hardware bugs and
16// implementations on other archtectures will cause your code to break. If you
17// do not know what you are doing, avoid these routines, and use a Mutex.
18//
19// It is incorrect to make direct assignments to/from an atomic variable.
20// You should use one of the Load or Store routines. The NoBarrier
21// versions are provided when no barriers are needed:
22// NoBarrier_Store()
23// NoBarrier_Load()
24// Although there are currently no compiler enforcement, you are encouraged
25// to use these.
26//
27
28#ifndef BASE_ATOMICOPS_H_
29#define BASE_ATOMICOPS_H_
30
31#include "base/basictypes.h"
deanm@google.com30bbf752008-08-05 22:01:35 +090032#include "base/port.h"
deanm@google.com1579ec72008-08-05 18:57:36 +090033
34namespace base {
35namespace subtle {
36
37// Bug 1308991. We need this for /Wp64, to mark it safe for AtomicWord casting.
deanm@google.com30bbf752008-08-05 22:01:35 +090038#ifndef OS_WIN
39#define __w64
40#endif
deanm@google.com1579ec72008-08-05 18:57:36 +090041typedef __w64 int32 Atomic32;
deanm@google.com30bbf752008-08-05 22:01:35 +090042#ifdef CPU_ARCH_64_BITS
deanm@google.com1579ec72008-08-05 18:57:36 +090043typedef int64 Atomic64;
44#endif
45
46// Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
47// Atomic64 routines below, depending on your architecture.
48typedef intptr_t AtomicWord;
49
50// Atomically execute:
51// result = *ptr;
52// if (*ptr == old_value)
53// *ptr = new_value;
54// return result;
55//
56// I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
57// Always return the old value of "*ptr"
58//
59// This routine implies no memory barriers.
60Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
61 Atomic32 old_value,
62 Atomic32 new_value);
63
64// Atomically store new_value into *ptr, returning the previous value held in
65// *ptr. This routine implies no memory barriers.
66Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
67
68// Atomically increment *ptr by "increment". Returns the new value of
69// *ptr with the increment applied. This routine implies no memory barriers.
70Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
71
72Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
73 Atomic32 increment);
74
75// These following lower-level operations are typically useful only to people
76// implementing higher-level synchronization operations like spinlocks,
77// mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
78// a store with appropriate memory-ordering instructions. "Acquire" operations
79// ensure that no later memory access can be reordered ahead of the operation.
80// "Release" operations ensure that no previous memory access can be reordered
81// after the operation. "Barrier" operations have both "Acquire" and "Release"
82// semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
83// access.
84Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
85 Atomic32 old_value,
86 Atomic32 new_value);
87Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
88 Atomic32 old_value,
89 Atomic32 new_value);
90
91void MemoryBarrier();
92void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
93void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
94void Release_Store(volatile Atomic32* ptr, Atomic32 value);
95
96Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
97Atomic32 Acquire_Load(volatile const Atomic32* ptr);
98Atomic32 Release_Load(volatile const Atomic32* ptr);
99
100// 64-bit atomic operations (only available on 64-bit processors).
deanm@google.com30bbf752008-08-05 22:01:35 +0900101#ifdef CPU_ARCH_64_BITS
deanm@google.com1579ec72008-08-05 18:57:36 +0900102Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
103 Atomic64 old_value,
104 Atomic64 new_value);
105Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
106Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
107Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
108
109Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
110 Atomic64 old_value,
111 Atomic64 new_value);
112Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
113 Atomic64 old_value,
114 Atomic64 new_value);
115void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
116void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
117void Release_Store(volatile Atomic64* ptr, Atomic64 value);
118Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
119Atomic64 Acquire_Load(volatile const Atomic64* ptr);
120Atomic64 Release_Load(volatile const Atomic64* ptr);
deanm@google.com30bbf752008-08-05 22:01:35 +0900121#endif // CPU_ARCH_64_BITS
deanm@google.com1579ec72008-08-05 18:57:36 +0900122
123} // namespace base::subtle
124} // namespace base
125
126// Include our platform specific implementation.
deanm@google.com30bbf752008-08-05 22:01:35 +0900127#if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY)
deanm@google.com1579ec72008-08-05 18:57:36 +0900128#include "base/atomicops_internals_x86_msvc.h"
deanm@google.com602a3cf2008-08-06 23:42:40 +0900129#elif defined(OS_MACOSX) && defined(ARCH_CPU_X86_FAMILY)
deanm@google.com1579ec72008-08-05 18:57:36 +0900130#include "base/atomicops_internals_x86_macosx.h"
deanm@google.com30bbf752008-08-05 22:01:35 +0900131#elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_FAMILY)
deanm@google.com1579ec72008-08-05 18:57:36 +0900132#include "base/atomicops_internals_x86_gcc.h"
133#else
134#error "Atomic operations are not supported on your platform"
135#endif
136
137#endif // BASE_ATOMICOPS_H_
license.botf003cfe2008-08-24 09:55:55 +0900138