File: Atomic_GCC.h

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (88 lines) | stat: -rw-r--r-- 2,110 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

// IWYU pragma: private, include "Common/Atomic.h"

#pragma once

#include "Common/Common.h"
#include "Common/CommonTypes.h"

// Atomic operations are performed in a single step by the CPU. It is
// impossible for other threads to see the operation "half-done."
//
// Some atomic operations can be combined with different types of memory
// barriers called "Acquire semantics" and "Release semantics", defined below.
//
// Acquire semantics: Future memory accesses cannot be relocated to before the
//                    operation.
//
// Release semantics: Past memory accesses cannot be relocated to after the
//                    operation.
//
// These barriers affect not only the compiler, but also the CPU.

namespace Common
{

inline void AtomicAdd(volatile u32& target, u32 value)
{
	__sync_add_and_fetch(&target, value);
}

inline void AtomicAnd(volatile u32& target, u32 value)
{
	__sync_and_and_fetch(&target, value);
}

inline void AtomicDecrement(volatile u32& target)
{
	__sync_add_and_fetch(&target, -1);
}

inline void AtomicIncrement(volatile u32& target)
{
	__sync_add_and_fetch(&target, 1);
}

inline void AtomicOr(volatile u32& target, u32 value)
{
	__sync_or_and_fetch(&target, value);
}

#ifndef __ATOMIC_RELAXED
#error __ATOMIC_RELAXED not defined; your compiler version is too old.
#endif

template <typename T>
inline T AtomicLoad(volatile T& src)
{
	return __atomic_load_n(&src, __ATOMIC_RELAXED);
}

template <typename T>
inline T AtomicLoadAcquire(volatile T& src)
{
	return __atomic_load_n(&src, __ATOMIC_ACQUIRE);
}

template <typename T, typename U>
inline void AtomicStore(volatile T& dest, U value)
{
	__atomic_store_n(&dest, value, __ATOMIC_RELAXED);
}

template <typename T, typename U>
inline void AtomicStoreRelease(volatile T& dest, U value)
{
	__atomic_store_n(&dest, value, __ATOMIC_RELEASE);
}

template <typename T, typename U>
inline T* AtomicExchangeAcquire(T* volatile& loc, U newval)
{
	return __atomic_exchange_n(&loc, newval, __ATOMIC_ACQ_REL);
}

}