blob: ea33e489285d8d5fc2f62ad8f2b54d64650fc8bb [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004
5// The routines exported by this module are subtle. If you use them, even if
6// you get the code right, it will depend on careful reasoning about atomicity
7// and memory ordering; it will be less readable, and harder to maintain. If
8// you plan to use these routines, you should have a good reason, such as solid
9// evidence that performance would otherwise suffer, or there being no
10// alternative. You should assume only properties explicitly guaranteed by the
11// specifications in this file. You are almost certainly _not_ writing code
12// just for the x86; if you assume x86 semantics, x86 hardware bugs and
13// implementations on other archtectures will cause your code to break. If you
14// do not know what you are doing, avoid these routines, and use a Mutex.
15//
16// It is incorrect to make direct assignments to/from an atomic variable.
17// You should use one of the Load or Store routines. The NoBarrier
18// versions are provided when no barriers are needed:
19// NoBarrier_Store()
20// NoBarrier_Load()
21// Although there are currently no compiler enforcement, you are encouraged
22// to use these.
23//
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025#ifndef V8_BASE_ATOMICOPS_H_
26#define V8_BASE_ATOMICOPS_H_
Ben Murdochb0fe1622011-05-05 13:52:32 +010027
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028#include <stdint.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029#include "src/base/build_config.h"
30
31#if defined(_WIN32) && defined(V8_HOST_ARCH_64_BIT)
32// windows.h #defines this (only on x64). This causes problems because the
33// public API also uses MemoryBarrier at the public name for this fence. So, on
34// X64, undef it, and call its documented
35// (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684208.aspx)
36// implementation directly.
37#undef MemoryBarrier
38#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +010039
40namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041namespace base {
Ben Murdochb0fe1622011-05-05 13:52:32 +010042
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043typedef char Atomic8;
Ben Murdochb0fe1622011-05-05 13:52:32 +010044typedef int32_t Atomic32;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045#if defined(__native_client__)
46typedef int64_t Atomic64;
47#elif defined(V8_HOST_ARCH_64_BIT)
Ben Murdochb0fe1622011-05-05 13:52:32 +010048// We need to be able to go between Atomic64 and AtomicWord implicitly. This
49// means Atomic64 and AtomicWord should be the same type on 64-bit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050#if defined(__ILP32__)
Ben Murdochb0fe1622011-05-05 13:52:32 +010051typedef int64_t Atomic64;
52#else
53typedef intptr_t Atomic64;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054#endif // defined(V8_HOST_ARCH_64_BIT)
55#endif // defined(__native_client__)
Ben Murdochb0fe1622011-05-05 13:52:32 +010056
57// Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
58// Atomic64 routines below, depending on your architecture.
59typedef intptr_t AtomicWord;
60
61// Atomically execute:
62// result = *ptr;
63// if (*ptr == old_value)
64// *ptr = new_value;
65// return result;
66//
67// I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
68// Always return the old value of "*ptr"
69//
70// This routine implies no memory barriers.
71Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
72 Atomic32 old_value,
73 Atomic32 new_value);
74
75// Atomically store new_value into *ptr, returning the previous value held in
76// *ptr. This routine implies no memory barriers.
77Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
78
79// Atomically increment *ptr by "increment". Returns the new value of
80// *ptr with the increment applied. This routine implies no memory barriers.
81Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
82
83Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
84 Atomic32 increment);
85
86// These following lower-level operations are typically useful only to people
87// implementing higher-level synchronization operations like spinlocks,
88// mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
89// a store with appropriate memory-ordering instructions. "Acquire" operations
90// ensure that no later memory access can be reordered ahead of the operation.
91// "Release" operations ensure that no previous memory access can be reordered
92// after the operation. "Barrier" operations have both "Acquire" and "Release"
93// semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
94// access.
95Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
96 Atomic32 old_value,
97 Atomic32 new_value);
98Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
99 Atomic32 old_value,
100 Atomic32 new_value);
101
102void MemoryBarrier();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103void NoBarrier_Store(volatile Atomic8* ptr, Atomic8 value);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100104void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
105void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
106void Release_Store(volatile Atomic32* ptr, Atomic32 value);
107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108Atomic8 NoBarrier_Load(volatile const Atomic8* ptr);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100109Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
110Atomic32 Acquire_Load(volatile const Atomic32* ptr);
111Atomic32 Release_Load(volatile const Atomic32* ptr);
112
113// 64-bit atomic operations (only available on 64-bit processors).
114#ifdef V8_HOST_ARCH_64_BIT
115Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
116 Atomic64 old_value,
117 Atomic64 new_value);
118Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
119Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
120Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
121
122Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
123 Atomic64 old_value,
124 Atomic64 new_value);
125Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
126 Atomic64 old_value,
127 Atomic64 new_value);
128void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
129void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
130void Release_Store(volatile Atomic64* ptr, Atomic64 value);
131Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
132Atomic64 Acquire_Load(volatile const Atomic64* ptr);
133Atomic64 Release_Load(volatile const Atomic64* ptr);
134#endif // V8_HOST_ARCH_64_BIT
135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136} // namespace base
137} // namespace v8
Ben Murdochb0fe1622011-05-05 13:52:32 +0100138
139// Include our platform specific implementation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140#if defined(THREAD_SANITIZER)
141#include "src/base/atomicops_internals_tsan.h"
142#elif defined(_MSC_VER) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64)
143#include "src/base/atomicops_internals_x86_msvc.h"
144#elif defined(__APPLE__)
145#include "src/base/atomicops_internals_mac.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400146#elif defined(__native_client__)
147#include "src/base/atomicops_internals_portable.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148#elif defined(__GNUC__) && V8_HOST_ARCH_ARM64
149#include "src/base/atomicops_internals_arm64_gcc.h"
150#elif defined(__GNUC__) && V8_HOST_ARCH_ARM
151#include "src/base/atomicops_internals_arm_gcc.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152#elif defined(__GNUC__) && V8_HOST_ARCH_PPC
153#include "src/base/atomicops_internals_ppc_gcc.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154#elif defined(__GNUC__) && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64)
155#include "src/base/atomicops_internals_x86_gcc.h"
156#elif defined(__GNUC__) && V8_HOST_ARCH_MIPS
157#include "src/base/atomicops_internals_mips_gcc.h"
158#elif defined(__GNUC__) && V8_HOST_ARCH_MIPS64
159#include "src/base/atomicops_internals_mips64_gcc.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +0100160#elif defined(__GNUC__) && V8_HOST_ARCH_S390
161#include "src/base/atomicops_internals_s390_gcc.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +0100162#else
163#error "Atomic operations are not supported on your platform"
164#endif
165
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166// On some platforms we need additional declarations to make
167// AtomicWord compatible with our other Atomic* types.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000168#if defined(__APPLE__) || defined(__OpenBSD__) || defined(V8_OS_AIX)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169#include "src/base/atomicops_internals_atomicword_compat.h"
170#endif
171
172#endif // V8_BASE_ATOMICOPS_H_