~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to Common/Atomic_GCC.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Dolphin Emulator Project
 
2
// Licensed under GPLv2
 
3
// Refer to the license.txt file included.
 
4
 
 
5
// IWYU pragma: private, include "Common/Atomic.h"
 
6
 
 
7
#pragma once
 
8
 
 
9
#include "CommonTypes.h"
 
10
 
 
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."
 
13
//
 
14
// Some atomic operations can be combined with different types of memory
 
15
// barriers called "Acquire semantics" and "Release semantics", defined below.
 
16
//
 
17
// Acquire semantics: Future memory accesses cannot be relocated to before the
 
18
//                    operation.
 
19
//
 
20
// Release semantics: Past memory accesses cannot be relocated to after the
 
21
//                    operation.
 
22
//
 
23
// These barriers affect not only the compiler, but also the CPU.
 
24
 
 
25
namespace Common
 
26
{
 
27
 
 
28
inline void AtomicAdd(volatile u32& target, u32 value)
 
29
{
 
30
        __sync_add_and_fetch(&target, value);
 
31
}
 
32
 
 
33
inline void AtomicAnd(volatile u32& target, u32 value)
 
34
{
 
35
        __sync_and_and_fetch(&target, value);
 
36
}
 
37
 
 
38
inline void AtomicDecrement(volatile u32& target)
 
39
{
 
40
        __sync_add_and_fetch(&target, -1);
 
41
}
 
42
 
 
43
inline void AtomicIncrement(volatile u32& target)
 
44
{
 
45
        __sync_add_and_fetch(&target, 1);
 
46
}
 
47
 
 
48
inline void AtomicOr(volatile u32& target, u32 value)
 
49
{
 
50
        __sync_or_and_fetch(&target, value);
 
51
}
 
52
 
 
53
// Support clang versions older than 3.4.
 
54
#if __clang__ 
 
55
#if !__has_feature(cxx_atomic)
 
56
template <typename T>
 
57
_Atomic(T)* ToC11Atomic(volatile T* loc)
 
58
{
 
59
        return (_Atomic(T)*) loc;
 
60
}
 
61
 
 
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)
 
65
#endif
 
66
#endif
 
67
 
 
68
#ifndef __ATOMIC_RELAXED
 
69
#error __ATOMIC_RELAXED not defined; your compiler version is too old.
 
70
#endif
 
71
 
 
72
template <typename T>
 
73
inline T AtomicLoad(volatile T& src)
 
74
{
 
75
        return __atomic_load_n(&src, __ATOMIC_RELAXED);
 
76
}
 
77
 
 
78
template <typename T>
 
79
inline T AtomicLoadAcquire(volatile T& src)
 
80
{
 
81
        return __atomic_load_n(&src, __ATOMIC_ACQUIRE);
 
82
}
 
83
 
 
84
template <typename T, typename U>
 
85
inline void AtomicStore(volatile T& dest, U value)
 
86
{
 
87
        __atomic_store_n(&dest, value, __ATOMIC_RELAXED);
 
88
}
 
89
 
 
90
template <typename T, typename U>
 
91
inline void AtomicStoreRelease(volatile T& dest, U value)
 
92
{
 
93
        __atomic_store_n(&dest, value, __ATOMIC_RELEASE);
 
94
}
 
95
 
 
96
template <typename T, typename U>
 
97
inline T* AtomicExchangeAcquire(T* volatile& loc, U newval)
 
98
{
 
99
        return __atomic_exchange_n(&loc, newval, __ATOMIC_ACQ_REL);
 
100
}
 
101
 
 
102
}