1 //===-- sanitizer_atomic_clang_x86.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_X86_H
15 #define SANITIZER_ATOMIC_CLANG_X86_H
17 namespace __sanitizer
{
19 inline void proc_yield(int cnt
) {
20 __asm__
__volatile__("" ::: "memory");
21 for (int i
= 0; i
< cnt
; i
++)
22 __asm__
__volatile__("pause");
23 __asm__
__volatile__("" ::: "memory");
27 inline typename
T::Type
atomic_load(
28 const volatile T
*a
, memory_order mo
) {
29 DCHECK(mo
& (memory_order_relaxed
| memory_order_consume
30 | memory_order_acquire
| memory_order_seq_cst
));
31 DCHECK(!((uptr
)a
% sizeof(*a
)));
34 if (sizeof(*a
) < 8 || sizeof(void*) == 8) {
35 // Assume that aligned loads are atomic.
36 if (mo
== memory_order_relaxed
) {
38 } else if (mo
== memory_order_consume
) {
39 // Assume that processor respects data dependencies
40 // (and that compiler won't break them).
41 __asm__
__volatile__("" ::: "memory");
43 __asm__
__volatile__("" ::: "memory");
44 } else if (mo
== memory_order_acquire
) {
45 __asm__
__volatile__("" ::: "memory");
47 // On x86 loads are implicitly acquire.
48 __asm__
__volatile__("" ::: "memory");
50 // On x86 plain MOV is enough for seq_cst store.
51 __asm__
__volatile__("" ::: "memory");
53 __asm__
__volatile__("" ::: "memory");
56 // 64-bit load on 32-bit platform.
58 "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
59 "movq %%mm0, %0;" // (ptr could be read-only)
60 "emms;" // Empty mmx state/Reset FP regs
62 : "m" (a
->val_dont_use
)
63 : // mark the mmx registers as clobbered
65 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
66 #endif // #ifdef __MMX__
73 inline void atomic_store(volatile T
*a
, typename
T::Type v
, memory_order mo
) {
74 DCHECK(mo
& (memory_order_relaxed
| memory_order_release
75 | memory_order_seq_cst
));
76 DCHECK(!((uptr
)a
% sizeof(*a
)));
78 if (sizeof(*a
) < 8 || sizeof(void*) == 8) {
79 // Assume that aligned loads are atomic.
80 if (mo
== memory_order_relaxed
) {
82 } else if (mo
== memory_order_release
) {
83 // On x86 stores are implicitly release.
84 __asm__
__volatile__("" ::: "memory");
86 __asm__
__volatile__("" ::: "memory");
88 // On x86 stores are implicitly release.
89 __asm__
__volatile__("" ::: "memory");
94 // 64-bit store on 32-bit platform.
96 "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
98 "emms;" // Empty mmx state/Reset FP regs
99 : "=m" (a
->val_dont_use
)
101 : // mark the mmx registers as clobbered
103 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
104 #endif // #ifdef __MMX__
106 if (mo
== memory_order_seq_cst
)
107 __sync_synchronize();
111 } // namespace __sanitizer
113 #endif // #ifndef SANITIZER_ATOMIC_CLANG_X86_H