blob: 7e5f191a53e3a23f265eec9b2f20bd0785d55c75 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- tsan_interface_atomic.cc --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "tsan_interface_atomic.h"
15#include "tsan_placement_new.h"
16#include "tsan_flags.h"
17#include "tsan_rtl.h"
18
19using namespace __tsan; // NOLINT
20
21class ScopedAtomic {
22 public:
23 ScopedAtomic(ThreadState *thr, uptr pc, const char *func)
24 : thr_(thr) {
25 CHECK_EQ(thr_->in_rtl, 1); // 1 due to our own ScopedInRtl member.
26 DPrintf("#%d: %s\n", thr_->tid, func);
27 }
28 ~ScopedAtomic() {
29 CHECK_EQ(thr_->in_rtl, 1);
30 }
31 private:
32 ThreadState *thr_;
33 ScopedInRtl in_rtl_;
34};
35
36// Some shortcuts.
37typedef __tsan_memory_order morder;
38typedef __tsan_atomic8 a8;
39typedef __tsan_atomic16 a16;
40typedef __tsan_atomic32 a32;
41typedef __tsan_atomic64 a64;
42const int mo_relaxed = __tsan_memory_order_relaxed;
43const int mo_consume = __tsan_memory_order_consume;
44const int mo_acquire = __tsan_memory_order_acquire;
45const int mo_release = __tsan_memory_order_release;
46const int mo_acq_rel = __tsan_memory_order_acq_rel;
47const int mo_seq_cst = __tsan_memory_order_seq_cst;
48
49static void AtomicStatInc(ThreadState *thr, uptr size, morder mo, StatType t) {
50 StatInc(thr, StatAtomic);
51 StatInc(thr, t);
52 StatInc(thr, size == 1 ? StatAtomic1
53 : size == 2 ? StatAtomic2
54 : size == 4 ? StatAtomic4
55 : StatAtomic8);
56 StatInc(thr, mo == mo_relaxed ? StatAtomicRelaxed
57 : mo == mo_consume ? StatAtomicConsume
58 : mo == mo_acquire ? StatAtomicAcquire
59 : mo == mo_release ? StatAtomicRelease
60 : mo == mo_acq_rel ? StatAtomicAcq_Rel
61 : StatAtomicSeq_Cst);
62}
63
64#define SCOPED_ATOMIC(func, ...) \
65 mo = flags()->force_seq_cst_atomics ? (morder)mo_seq_cst : mo; \
66 ThreadState *const thr = cur_thread(); \
67 const uptr pc = (uptr)__builtin_return_address(0); \
68 AtomicStatInc(thr, sizeof(*a), mo, StatAtomic##func); \
69 ScopedAtomic sa(thr, pc, __FUNCTION__); \
70 return Atomic##func(thr, pc, __VA_ARGS__); \
71/**/
72
73template<typename T>
74static T AtomicLoad(ThreadState *thr, uptr pc, const volatile T *a,
75 morder mo) {
76 CHECK(mo & (mo_relaxed | mo_consume | mo_acquire | mo_seq_cst));
77 T v = *a;
78 if (mo & (mo_consume | mo_acquire | mo_seq_cst))
79 Acquire(thr, pc, (uptr)a);
80 return v;
81}
82
83template<typename T>
84static void AtomicStore(ThreadState *thr, uptr pc, volatile T *a, T v,
85 morder mo) {
86 CHECK(mo & (mo_relaxed | mo_release | mo_seq_cst));
87 if (mo & (mo_release | mo_seq_cst))
88 Release(thr, pc, (uptr)a);
89 *a = v;
90}
91
92template<typename T>
93static T AtomicExchange(ThreadState *thr, uptr pc, volatile T *a, T v,
94 morder mo) {
95 if (mo & (mo_release | mo_acq_rel | mo_seq_cst))
96 Release(thr, pc, (uptr)a);
97 v = __sync_lock_test_and_set(a, v);
98 if (mo & (mo_consume | mo_acquire | mo_acq_rel | mo_seq_cst))
99 Acquire(thr, pc, (uptr)a);
100 return v;
101}
102
103template<typename T>
104static T AtomicFetchAdd(ThreadState *thr, uptr pc, volatile T *a, T v,
105 morder mo) {
106 if (mo & (mo_release | mo_acq_rel | mo_seq_cst))
107 Release(thr, pc, (uptr)a);
108 v = __sync_fetch_and_add(a, v);
109 if (mo & (mo_consume | mo_acquire | mo_acq_rel | mo_seq_cst))
110 Acquire(thr, pc, (uptr)a);
111 return v;
112}
113
114template<typename T>
115static bool AtomicCAS(ThreadState *thr, uptr pc,
116 volatile T *a, T *c, T v, morder mo) {
117 if (mo & (mo_release | mo_acq_rel | mo_seq_cst))
118 Release(thr, pc, (uptr)a);
119 T cc = *c;
120 T pr = __sync_val_compare_and_swap(a, cc, v);
121 if (mo & (mo_consume | mo_acquire | mo_acq_rel | mo_seq_cst))
122 Acquire(thr, pc, (uptr)a);
123 if (pr == cc)
124 return true;
125 *c = pr;
126 return false;
127}
128
129static void AtomicFence(ThreadState *thr, uptr pc, morder mo) {
130 __sync_synchronize();
131}
132
133a8 __tsan_atomic8_load(const volatile a8 *a, morder mo) {
134 SCOPED_ATOMIC(Load, a, mo);
135}
136
137a16 __tsan_atomic16_load(const volatile a16 *a, morder mo) {
138 SCOPED_ATOMIC(Load, a, mo);
139}
140
141a32 __tsan_atomic32_load(const volatile a32 *a, morder mo) {
142 SCOPED_ATOMIC(Load, a, mo);
143}
144
145a64 __tsan_atomic64_load(const volatile a64 *a, morder mo) {
146 SCOPED_ATOMIC(Load, a, mo);
147}
148
149void __tsan_atomic8_store(volatile a8 *a, a8 v, morder mo) {
150 SCOPED_ATOMIC(Store, a, v, mo);
151}
152
153void __tsan_atomic16_store(volatile a16 *a, a16 v, morder mo) {
154 SCOPED_ATOMIC(Store, a, v, mo);
155}
156
157void __tsan_atomic32_store(volatile a32 *a, a32 v, morder mo) {
158 SCOPED_ATOMIC(Store, a, v, mo);
159}
160
161void __tsan_atomic64_store(volatile a64 *a, a64 v, morder mo) {
162 SCOPED_ATOMIC(Store, a, v, mo);
163}
164
165a32 __tsan_atomic32_exchange(volatile a32 *a, a32 v, morder mo) {
166 SCOPED_ATOMIC(Exchange, a, v, mo);
167}
168
169a64 __tsan_atomic64_exchange(volatile a64 *a, a64 v, morder mo) {
170 SCOPED_ATOMIC(Exchange, a, v, mo);
171}
172
173a32 __tsan_atomic32_fetch_add(volatile a32 *a, a32 v, morder mo) {
174 SCOPED_ATOMIC(FetchAdd, a, v, mo);
175}
176
177a64 __tsan_atomic64_fetch_add(volatile a64 *a, a64 v, morder mo) {
178 SCOPED_ATOMIC(FetchAdd, a, v, mo);
179}
180
181int __tsan_atomic32_compare_exchange_strong(volatile a32 *a, a32 *c, a32 v,
182 morder mo) {
183 SCOPED_ATOMIC(CAS, a, c, v, mo);
184}
185
186int __tsan_atomic64_compare_exchange_strong(volatile a64 *a, a64 *c, a64 v,
187 morder mo) {
188 SCOPED_ATOMIC(CAS, a, c, v, mo);
189}
190
191void __tsan_atomic_thread_fence(morder mo) {
192 char* a;
193 SCOPED_ATOMIC(Fence, mo);
194}