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
18namespace __sanitizer {
19
20enum memory_order {
21// If the __atomic atomic builtins are supported (Clang/GCC), use the
22// compiler provided macro values so that we can map the atomic operations
23// to __atomic_* directly.
24#ifdef __ATOMIC_SEQ_CST
25 memory_order_relaxed = __ATOMIC_RELAXED,
26 memory_order_consume = __ATOMIC_CONSUME,
27 memory_order_acquire = __ATOMIC_ACQUIRE,
28 memory_order_release = __ATOMIC_RELEASE,
29 memory_order_acq_rel = __ATOMIC_ACQ_REL,
30 memory_order_seq_cst = __ATOMIC_SEQ_CST
31#else
32 memory_order_relaxed = 1 << 0,
33 memory_order_consume = 1 << 1,
34 memory_order_acquire = 1 << 2,
35 memory_order_release = 1 << 3,
36 memory_order_acq_rel = 1 << 4,
37 memory_order_seq_cst = 1 << 5
38#endif
39};
40
41struct atomic_uint8_t {
42 typedef u8 Type;
43 volatile Type val_dont_use;
44};
45
46struct atomic_uint16_t {
47 typedef u16 Type;
48 volatile Type val_dont_use;
49};
50
51struct atomic_sint32_t {
52 typedef s32 Type;
53 volatile Type val_dont_use;
54};
55
56struct atomic_uint32_t {
57 typedef u32 Type;
58 volatile Type val_dont_use;
59};
60
61struct atomic_uint64_t {
62 typedef u64 Type;
63 // On 32-bit platforms u64 is not necessary aligned on 8 bytes.
64 alignas(8) volatile Type val_dont_use;
65};
66
67struct atomic_uintptr_t {
68 typedef uptr Type;
69 volatile Type val_dont_use;
70};
71
72} // namespace __sanitizer
73
74#if defined(__clang__) || defined(__GNUC__)
75# include "sanitizer_atomic_clang.h"
76#elif defined(_MSC_VER)
77# include "sanitizer_atomic_msvc.h"
78#else
79# error "Unsupported compiler"
80#endif
81
82namespace __sanitizer {
83
84// Clutter-reducing helpers.
85
86template<typename T>
87inline typename T::Type atomic_load_relaxed(const volatile T *a) {
88 return atomic_load(a, memory_order_relaxed);
89}
90
91template<typename T>
92inline void atomic_store_relaxed(volatile T *a, typename T::Type v) {
93 atomic_store(a, v, memory_order_relaxed);
94}
95
96} // namespace __sanitizer
97
98#endif // SANITIZER_ATOMIC_H
99