1 //===-- sanitizer_atomic.h --------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef SANITIZER_ATOMIC_H
14 #define SANITIZER_ATOMIC_H
15
16 #include "sanitizer_internal_defs.h"
17
18 namespace __sanitizer {
19
20 enum memory_order {
21 memory_order_relaxed = 1 << 0,
22 memory_order_consume = 1 << 1,
23 memory_order_acquire = 1 << 2,
24 memory_order_release = 1 << 3,
25 memory_order_acq_rel = 1 << 4,
26 memory_order_seq_cst = 1 << 5
27 };
28
29 struct atomic_uint8_t {
30 typedef u8 Type;
31 volatile Type val_dont_use;
32 };
33
34 struct atomic_uint16_t {
35 typedef u16 Type;
36 volatile Type val_dont_use;
37 };
38
39 struct atomic_sint32_t {
40 typedef s32 Type;
41 volatile Type val_dont_use;
42 };
43
44 struct atomic_uint32_t {
45 typedef u32 Type;
46 volatile Type val_dont_use;
47 };
48
49 struct atomic_uint64_t {
50 typedef u64 Type;
51 // On 32-bit platforms u64 is not necessary aligned on 8 bytes.
52 volatile ALIGNED(8) Type val_dont_use;
53 };
54
55 struct atomic_uintptr_t {
56 typedef uptr Type;
57 volatile Type val_dont_use;
58 };
59
60 } // namespace __sanitizer
61
62 #if defined(__clang__) || defined(__GNUC__)
63 # include "sanitizer_atomic_clang.h"
64 #elif defined(_MSC_VER)
65 # include "sanitizer_atomic_msvc.h"
66 #else
67 # error "Unsupported compiler"
68 #endif
69
70 namespace __sanitizer {
71
72 // Clutter-reducing helpers.
73
74 template<typename T>
atomic_load_relaxed(const volatile T * a)75 inline typename T::Type atomic_load_relaxed(const volatile T *a) {
76 return atomic_load(a, memory_order_relaxed);
77 }
78
79 template<typename T>
atomic_store_relaxed(volatile T * a,typename T::Type v)80 inline void atomic_store_relaxed(volatile T *a, typename T::Type v) {
81 atomic_store(a, v, memory_order_relaxed);
82 }
83
84 } // namespace __sanitizer
85
86 #endif // SANITIZER_ATOMIC_H
87