Debian Codegraph @

srtcore/atomic_msvc.h

raw
1 //-----------------------------------------------------------------------------
2 // This is free and unencumbered software released into the public domain.
3 //
4 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute
5 // this software, either in source code form or as a compiled binary, for any
6 // purpose, commercial or non-commercial, and by any means.
7 //
8 // In jurisdictions that recognize copyright laws, the author or authors of
9 // this software dedicate any and all copyright interest in the software to the
10 // public domain. We make this dedication for the benefit of the public at
11 // large and to the detriment of our heirs and successors. We intend this
12 // dedication to be an overt act of relinquishment in perpetuity of all present
13 // and future rights to this software under copyright law.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 // For more information, please refer to <http://unlicense.org/>
23 //-----------------------------------------------------------------------------
24
25 // SRT Project information:
26 // This file was adopted from a Public Domain project from
27 // https://github.com/mbitsnbites/atomic
28 // Only namespaces were changed to adopt it for SRT project.
29
30 #ifndef SRT_SYNC_ATOMIC_MSVC_H_
31 #define SRT_SYNC_ATOMIC_MSVC_H_
32
33 // Define which functions we need (don't include <intrin.h>).
34 extern "C" {
35 short _InterlockedIncrement16(short volatile*);
36 long _InterlockedIncrement(long volatile*);
37 __int64 _InterlockedIncrement64(__int64 volatile*);
38
39 short _InterlockedDecrement16(short volatile*);
40 long _InterlockedDecrement(long volatile*);
41 __int64 _InterlockedDecrement64(__int64 volatile*);
42
43 char _InterlockedExchange8(char volatile*, char);
44 short _InterlockedExchange16(short volatile*, short);
45 long __cdecl _InterlockedExchange(long volatile*, long);
46 __int64 _InterlockedExchange64(__int64 volatile*, __int64);
47
48 char _InterlockedCompareExchange8(char volatile*, char, char);
49 short _InterlockedCompareExchange16(short volatile*, short, short);
50 long __cdecl _InterlockedCompareExchange(long volatile*, long, long);
51 __int64 _InterlockedCompareExchange64(__int64 volatile*, __int64, __int64);
52 };
53
54 // Define which functions we want to use as inline intriniscs.
55 #pragma intrinsic(_InterlockedIncrement)
56 #pragma intrinsic(_InterlockedIncrement16)
57
58 #pragma intrinsic(_InterlockedDecrement)
59 #pragma intrinsic(_InterlockedDecrement16)
60
61 #pragma intrinsic(_InterlockedCompareExchange)
62 #pragma intrinsic(_InterlockedCompareExchange8)
63 #pragma intrinsic(_InterlockedCompareExchange16)
64
65 #pragma intrinsic(_InterlockedExchange)
66 #pragma intrinsic(_InterlockedExchange8)
67 #pragma intrinsic(_InterlockedExchange16)
68
69 #if defined(_M_X64)
70 #pragma intrinsic(_InterlockedIncrement64)
71 #pragma intrinsic(_InterlockedDecrement64)
72 #pragma intrinsic(_InterlockedCompareExchange64)
73 #pragma intrinsic(_InterlockedExchange64)
74 #endif // _M_X64
75
76 namespace srt {
77 namespace sync {
78 namespace msvc {
79 template <typename T, size_t N = sizeof(T)>
80 struct interlocked {
81 };
82
83 template <typename T>
84 struct interlocked<T, 1> {
85 static inline T increment(T volatile* x) {
86 // There's no _InterlockedIncrement8().
87 char old_val, new_val;
88 do {
89 old_val = static_cast<char>(*x);
90 new_val = old_val + static_cast<char>(1);
91 } while (_InterlockedCompareExchange8(reinterpret_cast<volatile char*>(x),
92 new_val,
93 old_val) != old_val);
94 return static_cast<T>(new_val);
95 }
96
97 static inline T decrement(T volatile* x) {
98 // There's no _InterlockedDecrement8().
99 char old_val, new_val;
100 do {
101 old_val = static_cast<char>(*x);
102 new_val = old_val - static_cast<char>(1);
103 } while (_InterlockedCompareExchange8(reinterpret_cast<volatile char*>(x),
104 new_val,
105 old_val) != old_val);
106 return static_cast<T>(new_val);
107 }
108
109 static inline T compare_exchange(T volatile* x,
110 const T new_val,
111 const T expected_val) {
112 return static_cast<T>(
113 _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(x),
114 static_cast<const char>(new_val),
115 static_cast<const char>(expected_val)));
116 }
117
118 static inline T exchange(T volatile* x, const T new_val) {
119 return static_cast<T>(_InterlockedExchange8(
120 reinterpret_cast<volatile char*>(x), static_cast<const char>(new_val)));
121 }
122
123 static inline T or_fetch(T volatile* x, const T val) {
124 return static_cast<T>(_InterlockedOr8(
125 reinterpret_cast<volatile char*>(x), static_cast<const char>(val)));
126 }
127 };
128
129 template <typename T>
130 struct interlocked<T, 2> {
131 static inline T increment(T volatile* x) {
132 return static_cast<T>(
133 _InterlockedIncrement16(reinterpret_cast<volatile short*>(x)));
134 }
135
136 static inline T decrement(T volatile* x) {
137 return static_cast<T>(
138 _InterlockedDecrement16(reinterpret_cast<volatile short*>(x)));
139 }
140
141 static inline T compare_exchange(T volatile* x,
142 const T new_val,
143 const T expected_val) {
144 return static_cast<T>(
145 _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(x),
146 static_cast<const short>(new_val),
147 static_cast<const short>(expected_val)));
148 }
149
150 static inline T exchange(T volatile* x, const T new_val) {
151 return static_cast<T>(
152 _InterlockedExchange16(reinterpret_cast<volatile short*>(x),
153 static_cast<const short>(new_val)));
154 }
155 };
156
157 template <typename T>
158 struct interlocked<T, 4> {
159 static inline T increment(T volatile* x) {
160 return static_cast<T>(
161 _InterlockedIncrement(reinterpret_cast<volatile long*>(x)));
162 }
163
164 static inline T decrement(T volatile* x) {
165 return static_cast<T>(
166 _InterlockedDecrement(reinterpret_cast<volatile long*>(x)));
167 }
168
169 static inline T compare_exchange(T volatile* x,
170 const T new_val,
171 const T expected_val) {
172 return static_cast<T>(
173 _InterlockedCompareExchange(reinterpret_cast<volatile long*>(x),
174 static_cast<const long>(new_val),
175 static_cast<const long>(expected_val)));
176 }
177
178 static inline T exchange(T volatile* x, const T new_val) {
179 return static_cast<T>(_InterlockedExchange(
180 reinterpret_cast<volatile long*>(x), static_cast<const long>(new_val)));
181 }
182 };
183
184 template <typename T>
185 struct interlocked<T, 8> {
186 static inline T increment(T volatile* x) {
187 #if defined(_M_X64)
188 return static_cast<T>(
189 _InterlockedIncrement64(reinterpret_cast<volatile __int64*>(x)));
190 #else
191 // There's no _InterlockedIncrement64() for 32-bit x86.
192 __int64 old_val, new_val;
193 do {
194 old_val = static_cast<__int64>(*x);
195 new_val = old_val + static_cast<__int64>(1);
196 } while (_InterlockedCompareExchange64(
197 reinterpret_cast<volatile __int64*>(x), new_val, old_val) !=
198 old_val);
199 return static_cast<T>(new_val);
200 #endif // _M_X64
201 }
202
203 static inline T decrement(T volatile* x) {
204 #if defined(_M_X64)
205 return static_cast<T>(
206 _InterlockedDecrement64(reinterpret_cast<volatile __int64*>(x)));
207 #else
208 // There's no _InterlockedDecrement64() for 32-bit x86.
209 __int64 old_val, new_val;
210 do {
211 old_val = static_cast<__int64>(*x);
212 new_val = old_val - static_cast<__int64>(1);
213 } while (_InterlockedCompareExchange64(
214 reinterpret_cast<volatile __int64*>(x), new_val, old_val) !=
215 old_val);
216 return static_cast<T>(new_val);
217 #endif // _M_X64
218 }
219
220 static inline T compare_exchange(T volatile* x,
221 const T new_val,
222 const T expected_val) {
223 return static_cast<T>(_InterlockedCompareExchange64(
224 reinterpret_cast<volatile __int64*>(x),
225 static_cast<const __int64>(new_val),
226 static_cast<const __int64>(expected_val)));
227 }
228
229 static inline T exchange(T volatile* x, const T new_val) {
230 #if defined(_M_X64)
231 return static_cast<T>(
232 _InterlockedExchange64(reinterpret_cast<volatile __int64*>(x),
233 static_cast<const __int64>(new_val)));
234 #else
235 // There's no _InterlockedExchange64 for 32-bit x86.
236 __int64 old_val;
237 do {
238 old_val = static_cast<__int64>(*x);
239 } while (_InterlockedCompareExchange64(
240 reinterpret_cast<volatile __int64*>(x), new_val, old_val) !=
241 old_val);
242 return static_cast<T>(old_val);
243 #endif // _M_X64
244 }
245 };
246 } // namespace msvc
247 } // namespace sync
248 } // namespace srt
249
250 #endif // ATOMIC_ATOMIC_MSVC_H_