1 //===-- sanitizer_atomic_clang_other.h --------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 // Not intended for direct inclusion. Include sanitizer_atomic.h.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H
15 #define SANITIZER_ATOMIC_CLANG_OTHER_H
17 namespace __sanitizer {
20 INLINE void proc_yield(int cnt) {
21 __asm__ __volatile__("" ::: "memory");
25 INLINE typename T::Type atomic_load(
26 const volatile T *a, memory_order mo) {
27 DCHECK(mo & (memory_order_relaxed | memory_order_consume
28 | memory_order_acquire | memory_order_seq_cst));
29 DCHECK(!((uptr)a % sizeof(*a)));
32 if (sizeof(*a) < 8 || sizeof(void*) == 8) {
33 // Assume that aligned loads are atomic.
34 if (mo == memory_order_relaxed) {
36 } else if (mo == memory_order_consume) {
37 // Assume that processor respects data dependencies
38 // (and that compiler won't break them).
39 __asm__ __volatile__("" ::: "memory");
41 __asm__ __volatile__("" ::: "memory");
42 } else if (mo == memory_order_acquire) {
43 __asm__ __volatile__("" ::: "memory");
47 // E.g. on POWER we need a hw fence even before the store.
53 // 64-bit load on 32-bit platform.
54 // Gross, but simple and reliable.
55 // Assume that it is not in read-only memory.
56 v = __sync_fetch_and_add(
57 const_cast<typename T::Type volatile *>(&a->val_dont_use), 0);
63 INLINE void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
64 DCHECK(mo & (memory_order_relaxed | memory_order_release
65 | memory_order_seq_cst));
66 DCHECK(!((uptr)a % sizeof(*a)));
68 if (sizeof(*a) < 8 || sizeof(void*) == 8) {
69 // Assume that aligned loads are atomic.
70 if (mo == memory_order_relaxed) {
72 } else if (mo == memory_order_release) {
75 __asm__ __volatile__("" ::: "memory");
82 // 64-bit store on 32-bit platform.
83 // Gross, but simple and reliable.
84 typename T::Type cmp = a->val_dont_use;
87 cur = __sync_val_compare_and_swap(&a->val_dont_use, cmp, v);
88 if (cur == cmp || cur == v)
95 } // namespace __sanitizer
97 #endif // #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H