• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- sanitizer_atomic_clang.h --------------------------------*- 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/AddressSanitizer runtime.
11 // Not intended for direct inclusion. Include sanitizer_atomic.h.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef SANITIZER_ATOMIC_CLANG_H
16 #define SANITIZER_ATOMIC_CLANG_H
17 
18 namespace __sanitizer {
19 
atomic_signal_fence(memory_order)20 INLINE void atomic_signal_fence(memory_order) {
21   __asm__ __volatile__("" ::: "memory");
22 }
23 
atomic_thread_fence(memory_order)24 INLINE void atomic_thread_fence(memory_order) {
25   __sync_synchronize();
26 }
27 
proc_yield(int cnt)28 INLINE void proc_yield(int cnt) {
29   __asm__ __volatile__("" ::: "memory");
30 #if defined(__i386__) || defined(__x86_64__)
31   for (int i = 0; i < cnt; i++)
32     __asm__ __volatile__("pause");
33 #endif
34   __asm__ __volatile__("" ::: "memory");
35 }
36 
37 template<typename T>
atomic_load(const volatile T * a,memory_order mo)38 INLINE typename T::Type atomic_load(
39     const volatile T *a, memory_order mo) {
40   DCHECK(mo & (memory_order_relaxed | memory_order_consume
41       | memory_order_acquire | memory_order_seq_cst));
42   DCHECK(!((uptr)a % sizeof(*a)));
43   typename T::Type v;
44   if (mo == memory_order_relaxed) {
45     v = a->val_dont_use;
46   } else {
47     atomic_signal_fence(memory_order_seq_cst);
48     v = a->val_dont_use;
49     atomic_signal_fence(memory_order_seq_cst);
50   }
51   return v;
52 }
53 
54 template<typename T>
atomic_store(volatile T * a,typename T::Type v,memory_order mo)55 INLINE void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
56   DCHECK(mo & (memory_order_relaxed | memory_order_release
57       | memory_order_seq_cst));
58   DCHECK(!((uptr)a % sizeof(*a)));
59   if (mo == memory_order_relaxed) {
60     a->val_dont_use = v;
61   } else {
62     atomic_signal_fence(memory_order_seq_cst);
63     a->val_dont_use = v;
64     atomic_signal_fence(memory_order_seq_cst);
65   }
66   if (mo == memory_order_seq_cst)
67     atomic_thread_fence(memory_order_seq_cst);
68 }
69 
70 template<typename T>
atomic_fetch_add(volatile T * a,typename T::Type v,memory_order mo)71 INLINE typename T::Type atomic_fetch_add(volatile T *a,
72     typename T::Type v, memory_order mo) {
73   (void)mo;
74   DCHECK(!((uptr)a % sizeof(*a)));
75   return __sync_fetch_and_add(&a->val_dont_use, v);
76 }
77 
78 template<typename T>
atomic_fetch_sub(volatile T * a,typename T::Type v,memory_order mo)79 INLINE typename T::Type atomic_fetch_sub(volatile T *a,
80     typename T::Type v, memory_order mo) {
81   (void)mo;
82   DCHECK(!((uptr)a % sizeof(*a)));
83   return __sync_fetch_and_add(&a->val_dont_use, -v);
84 }
85 
86 template<typename T>
atomic_exchange(volatile T * a,typename T::Type v,memory_order mo)87 INLINE typename T::Type atomic_exchange(volatile T *a,
88     typename T::Type v, memory_order mo) {
89   DCHECK(!((uptr)a % sizeof(*a)));
90   if (mo & (memory_order_release | memory_order_acq_rel | memory_order_seq_cst))
91     __sync_synchronize();
92   v = __sync_lock_test_and_set(&a->val_dont_use, v);
93   if (mo == memory_order_seq_cst)
94     __sync_synchronize();
95   return v;
96 }
97 
98 template<typename T>
atomic_compare_exchange_strong(volatile T * a,typename T::Type * cmp,typename T::Type xchg,memory_order mo)99 INLINE bool atomic_compare_exchange_strong(volatile T *a,
100                                            typename T::Type *cmp,
101                                            typename T::Type xchg,
102                                            memory_order mo) {
103   typedef typename T::Type Type;
104   Type cmpv = *cmp;
105   Type prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg);
106   if (prev == cmpv)
107     return true;
108   *cmp = prev;
109   return false;
110 }
111 
112 template<typename T>
atomic_compare_exchange_weak(volatile T * a,typename T::Type * cmp,typename T::Type xchg,memory_order mo)113 INLINE bool atomic_compare_exchange_weak(volatile T *a,
114                                            typename T::Type *cmp,
115                                            typename T::Type xchg,
116                                            memory_order mo) {
117   return atomic_compare_exchange_strong(a, cmp, xchg, mo);
118 }
119 
120 }  // namespace __sanitizer
121 
122 #endif  // SANITIZER_ATOMIC_CLANG_H
123