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