1
// Copyright 2013 Dolphin Emulator Project
2
// Licensed under GPLv2
3
// Refer to the license.txt file included.
5
// IWYU pragma: private, include "Common/Atomic.h"
9
#include "CommonTypes.h"
11
// Atomic operations are performed in a single step by the CPU. It is
12
// impossible for other threads to see the operation "half-done."
14
// Some atomic operations can be combined with different types of memory
15
// barriers called "Acquire semantics" and "Release semantics", defined below.
17
// Acquire semantics: Future memory accesses cannot be relocated to before the
20
// Release semantics: Past memory accesses cannot be relocated to after the
23
// These barriers affect not only the compiler, but also the CPU.
28
inline void AtomicAdd(volatile u32& target, u32 value)
30
__sync_add_and_fetch(&target, value);
33
inline void AtomicAnd(volatile u32& target, u32 value)
35
__sync_and_and_fetch(&target, value);
38
inline void AtomicDecrement(volatile u32& target)
40
__sync_add_and_fetch(&target, -1);
43
inline void AtomicIncrement(volatile u32& target)
45
__sync_add_and_fetch(&target, 1);
48
inline void AtomicOr(volatile u32& target, u32 value)
50
__sync_or_and_fetch(&target, value);
53
// Support clang versions older than 3.4.
55
#if !__has_feature(cxx_atomic)
57
_Atomic(T)* ToC11Atomic(volatile T* loc)
59
return (_Atomic(T)*) loc;
62
#define __atomic_load_n(p, m) __c11_atomic_load(ToC11Atomic(p), m)
63
#define __atomic_store_n(p, v, m) __c11_atomic_store(ToC11Atomic(p), v, m)
64
#define __atomic_exchange_n(p, v, m) __c11_atomic_exchange(ToC11Atomic(p), v, m)
68
#ifndef __ATOMIC_RELAXED
69
#error __ATOMIC_RELAXED not defined; your compiler version is too old.
73
inline T AtomicLoad(volatile T& src)
75
return __atomic_load_n(&src, __ATOMIC_RELAXED);
79
inline T AtomicLoadAcquire(volatile T& src)
81
return __atomic_load_n(&src, __ATOMIC_ACQUIRE);
84
template <typename T, typename U>
85
inline void AtomicStore(volatile T& dest, U value)
87
__atomic_store_n(&dest, value, __ATOMIC_RELAXED);
90
template <typename T, typename U>
91
inline void AtomicStoreRelease(volatile T& dest, U value)
93
__atomic_store_n(&dest, value, __ATOMIC_RELEASE);
96
template <typename T, typename U>
97
inline T* AtomicExchangeAcquire(T* volatile& loc, U newval)
99
return __atomic_exchange_n(&loc, newval, __ATOMIC_ACQ_REL);