summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Merry2024-01-27 21:11:48 +0000
committerGravatar Merry2024-01-27 21:12:12 +0000
commit6527c0d2fc87fe74f1ae3e03a8dfdf2668c2c48c (patch)
tree15d7b3788f353d5c42b0d9e5f84eebc69b6cb010
parentMerge pull request #12818 from K900/small-fixes (diff)
downloadyuzu-6527c0d2fc87fe74f1ae3e03a8dfdf2668c2c48c.tar.gz
yuzu-6527c0d2fc87fe74f1ae3e03a8dfdf2668c2c48c.tar.xz
yuzu-6527c0d2fc87fe74f1ae3e03a8dfdf2668c2c48c.zip
atomic_ops: Reduce code duplication with templates
Also fixes builds on unusual toolchains where: - u32 is unsigned int - u64 is unsigned long long - uintptr_t is unsigned long
-rw-r--r--src/common/atomic_ops.h71
1 files changed, 26 insertions, 45 deletions
diff --git a/src/common/atomic_ops.h b/src/common/atomic_ops.h
index c18bb33c4..c809e6963 100644
--- a/src/common/atomic_ops.h
+++ b/src/common/atomic_ops.h
@@ -15,25 +15,34 @@ namespace Common {
15 15
16#if _MSC_VER 16#if _MSC_VER
17 17
18[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { 18template <typename T>
19[[nodiscard]] inline bool AtomicCompareAndSwap(volatile T* pointer, T value, T expected);
20template <typename T>
21[[nodiscard]] inline bool AtomicCompareAndSwap(volatile T* pointer, T value, T expected, T& actual);
22
23template [[nodiscard]] inline bool AtomicCompareAndSwap<u8>(volatile u8* pointer, u8 value,
24 u8 expected) {
19 const u8 result = 25 const u8 result =
20 _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); 26 _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected);
21 return result == expected; 27 return result == expected;
22} 28}
23 29
24[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { 30template [[nodiscard]] inline bool AtomicCompareAndSwap<u16>(volatile u16* pointer, u16 value,
31 u16 expected) {
25 const u16 result = 32 const u16 result =
26 _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); 33 _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected);
27 return result == expected; 34 return result == expected;
28} 35}
29 36
30[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { 37template [[nodiscard]] inline bool AtomicCompareAndSwap<u32>(volatile u32* pointer, u32 value,
38 u32 expected) {
31 const u32 result = 39 const u32 result =
32 _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); 40 _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected);
33 return result == expected; 41 return result == expected;
34} 42}
35 43
36[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { 44template [[nodiscard]] inline bool AtomicCompareAndSwap<u64>(volatile u64* pointer, u64 value,
45 u64 expected) {
37 const u64 result = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), 46 const u64 result = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer),
38 value, expected); 47 value, expected);
39 return result == expected; 48 return result == expected;
@@ -45,29 +54,29 @@ namespace Common {
45 reinterpret_cast<__int64*>(expected.data())) != 0; 54 reinterpret_cast<__int64*>(expected.data())) != 0;
46} 55}
47 56
48[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected, 57template [[nodiscard]] inline bool AtomicCompareAndSwap<u8>(volatile u8* pointer, u8 value,
49 u8& actual) { 58 u8 expected, u8& actual) {
50 actual = 59 actual =
51 _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); 60 _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected);
52 return actual == expected; 61 return actual == expected;
53} 62}
54 63
55[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected, 64template [[nodiscard]] inline bool AtomicCompareAndSwap<u16>(volatile u16* pointer, u16 value,
56 u16& actual) { 65 u16 expected, u16& actual) {
57 actual = 66 actual =
58 _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); 67 _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected);
59 return actual == expected; 68 return actual == expected;
60} 69}
61 70
62[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected, 71template [[nodiscard]] inline bool AtomicCompareAndSwap<u32>(volatile u32* pointer, u32 value,
63 u32& actual) { 72 u32 expected, u32& actual) {
64 actual = 73 actual =
65 _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); 74 _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected);
66 return actual == expected; 75 return actual == expected;
67} 76}
68 77
69[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected, 78template [[nodiscard]] inline bool AtomicCompareAndSwap<u64>(volatile u64* pointer, u64 value,
70 u64& actual) { 79 u64 expected, u64& actual) {
71 actual = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), value, 80 actual = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), value,
72 expected); 81 expected);
73 return actual == expected; 82 return actual == expected;
@@ -91,19 +100,8 @@ namespace Common {
91 100
92#else 101#else
93 102
94[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { 103template <typename T>
95 return __sync_bool_compare_and_swap(pointer, expected, value); 104[[nodiscard]] inline bool AtomicCompareAndSwap(volatile T* pointer, T value, T expected) {
96}
97
98[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) {
99 return __sync_bool_compare_and_swap(pointer, expected, value);
100}
101
102[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) {
103 return __sync_bool_compare_and_swap(pointer, expected, value);
104}
105
106[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) {
107 return __sync_bool_compare_and_swap(pointer, expected, value); 105 return __sync_bool_compare_and_swap(pointer, expected, value);
108} 106}
109 107
@@ -115,26 +113,9 @@ namespace Common {
115 return __sync_bool_compare_and_swap((unsigned __int128*)pointer, expected_a, value_a); 113 return __sync_bool_compare_and_swap((unsigned __int128*)pointer, expected_a, value_a);
116} 114}
117 115
118[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected, 116template <typename T>
119 u8& actual) { 117[[nodiscard]] inline bool AtomicCompareAndSwap(volatile T* pointer, T value, T expected,
120 actual = __sync_val_compare_and_swap(pointer, expected, value); 118 T& actual) {
121 return actual == expected;
122}
123
124[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected,
125 u16& actual) {
126 actual = __sync_val_compare_and_swap(pointer, expected, value);
127 return actual == expected;
128}
129
130[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected,
131 u32& actual) {
132 actual = __sync_val_compare_and_swap(pointer, expected, value);
133 return actual == expected;
134}
135
136[[nodiscard]] inline bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected,
137 u64& actual) {
138 actual = __sync_val_compare_and_swap(pointer, expected, value); 119 actual = __sync_val_compare_and_swap(pointer, expected, value);
139 return actual == expected; 120 return actual == expected;
140} 121}