10b57cec5SDimitry Andric //===-- sanitizer_atomic_clang.h --------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
100b57cec5SDimitry Andric // Not intended for direct inclusion. Include sanitizer_atomic.h.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #ifndef SANITIZER_ATOMIC_CLANG_H
150b57cec5SDimitry Andric #define SANITIZER_ATOMIC_CLANG_H
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric namespace __sanitizer {
180b57cec5SDimitry Andric
19*0fca6ea1SDimitry Andric // We use the compiler builtin atomic operations for loads and stores, which
20*0fca6ea1SDimitry Andric // generates correct code for all architectures, but may require libatomic
21*0fca6ea1SDimitry Andric // on platforms where e.g. 64-bit atomics are not supported natively.
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
240b57cec5SDimitry Andric // for mappings of the memory model to different processors.
250b57cec5SDimitry Andric
atomic_signal_fence(memory_order mo)26*0fca6ea1SDimitry Andric inline void atomic_signal_fence(memory_order mo) { __atomic_signal_fence(mo); }
27*0fca6ea1SDimitry Andric
atomic_thread_fence(memory_order mo)28*0fca6ea1SDimitry Andric inline void atomic_thread_fence(memory_order mo) { __atomic_thread_fence(mo); }
29*0fca6ea1SDimitry Andric
proc_yield(int cnt)30*0fca6ea1SDimitry Andric inline void proc_yield(int cnt) {
310b57cec5SDimitry Andric __asm__ __volatile__("" ::: "memory");
32*0fca6ea1SDimitry Andric #if defined(__i386__) || defined(__x86_64__)
33*0fca6ea1SDimitry Andric for (int i = 0; i < cnt; i++) __asm__ __volatile__("pause");
34*0fca6ea1SDimitry Andric __asm__ __volatile__("" ::: "memory");
35*0fca6ea1SDimitry Andric #endif
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric template <typename T>
atomic_load(const volatile T * a,memory_order mo)39*0fca6ea1SDimitry Andric inline typename T::Type atomic_load(const volatile T *a, memory_order mo) {
40*0fca6ea1SDimitry Andric DCHECK(mo == memory_order_relaxed || mo == memory_order_consume ||
41*0fca6ea1SDimitry Andric mo == memory_order_acquire || mo == memory_order_seq_cst);
42*0fca6ea1SDimitry Andric DCHECK(!((uptr)a % sizeof(*a)));
43*0fca6ea1SDimitry Andric return __atomic_load_n(&a->val_dont_use, mo);
44*0fca6ea1SDimitry Andric }
45*0fca6ea1SDimitry Andric
46*0fca6ea1SDimitry Andric template <typename T>
atomic_store(volatile T * a,typename T::Type v,memory_order mo)47*0fca6ea1SDimitry Andric inline void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
48*0fca6ea1SDimitry Andric DCHECK(mo == memory_order_relaxed || mo == memory_order_release ||
49*0fca6ea1SDimitry Andric mo == memory_order_seq_cst);
50*0fca6ea1SDimitry Andric DCHECK(!((uptr)a % sizeof(*a)));
51*0fca6ea1SDimitry Andric __atomic_store_n(&a->val_dont_use, v, mo);
52*0fca6ea1SDimitry Andric }
53*0fca6ea1SDimitry Andric
54*0fca6ea1SDimitry Andric template <typename T>
atomic_fetch_add(volatile T * a,typename T::Type v,memory_order mo)55*0fca6ea1SDimitry Andric inline typename T::Type atomic_fetch_add(volatile T *a, typename T::Type v,
56*0fca6ea1SDimitry Andric memory_order mo) {
57*0fca6ea1SDimitry Andric DCHECK(!((uptr)a % sizeof(*a)));
58*0fca6ea1SDimitry Andric return __atomic_fetch_add(&a->val_dont_use, v, mo);
59*0fca6ea1SDimitry Andric }
60*0fca6ea1SDimitry Andric
61*0fca6ea1SDimitry Andric template <typename T>
atomic_fetch_sub(volatile T * a,typename T::Type v,memory_order mo)62*0fca6ea1SDimitry Andric inline typename T::Type atomic_fetch_sub(volatile T *a, typename T::Type v,
63*0fca6ea1SDimitry Andric memory_order mo) {
640b57cec5SDimitry Andric (void)mo;
650b57cec5SDimitry Andric DCHECK(!((uptr)a % sizeof(*a)));
66*0fca6ea1SDimitry Andric return __atomic_fetch_sub(&a->val_dont_use, v, mo);
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric template <typename T>
atomic_exchange(volatile T * a,typename T::Type v,memory_order mo)70*0fca6ea1SDimitry Andric inline typename T::Type atomic_exchange(volatile T *a, typename T::Type v,
71*0fca6ea1SDimitry Andric memory_order mo) {
720b57cec5SDimitry Andric DCHECK(!((uptr)a % sizeof(*a)));
73*0fca6ea1SDimitry Andric return __atomic_exchange_n(&a->val_dont_use, v, mo);
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric template <typename T>
atomic_compare_exchange_strong(volatile T * a,typename T::Type * cmp,typename T::Type xchg,memory_order mo)77e8d8bef9SDimitry Andric inline bool atomic_compare_exchange_strong(volatile T *a, typename T::Type *cmp,
780b57cec5SDimitry Andric typename T::Type xchg,
790b57cec5SDimitry Andric memory_order mo) {
801fd87a68SDimitry Andric // Transitioned from __sync_val_compare_and_swap to support targets like
811fd87a68SDimitry Andric // SPARC V8 that cannot inline atomic cmpxchg. __atomic_compare_exchange
821fd87a68SDimitry Andric // can then be resolved from libatomic. __ATOMIC_SEQ_CST is used to best
831fd87a68SDimitry Andric // match the __sync builtin memory order.
841fd87a68SDimitry Andric return __atomic_compare_exchange(&a->val_dont_use, cmp, &xchg, false,
851fd87a68SDimitry Andric __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric template <typename T>
atomic_compare_exchange_weak(volatile T * a,typename T::Type * cmp,typename T::Type xchg,memory_order mo)89*0fca6ea1SDimitry Andric inline bool atomic_compare_exchange_weak(volatile T *a, typename T::Type *cmp,
900b57cec5SDimitry Andric typename T::Type xchg,
910b57cec5SDimitry Andric memory_order mo) {
920b57cec5SDimitry Andric return atomic_compare_exchange_strong(a, cmp, xchg, mo);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric } // namespace __sanitizer
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric #undef ATOMIC_ORDER
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric #endif // SANITIZER_ATOMIC_CLANG_H
100