Line data Source code
1 : #ifndef CFD_THREADING_INTERNAL_H
2 : #define CFD_THREADING_INTERNAL_H
3 :
4 : #ifdef _WIN32
5 : #include <windows.h>
6 :
7 : typedef volatile LONG cfd_atomic_int;
8 :
9 : static inline int cfd_atomic_load(const cfd_atomic_int* ptr) {
10 : return *ptr;
11 : }
12 :
13 : /**
14 : * @brief Compare and Swap
15 : * @return 1 if swap occurred (success), 0 otherwise
16 : */
17 : static inline int cfd_atomic_cas(cfd_atomic_int* ptr, int expected, int desired) {
18 : // InterlockedCompareExchange(dest, exchange, comparand)
19 : // Returns the initial value of dest.
20 : // If return value == comparand (expected), then success.
21 : return (InterlockedCompareExchange(ptr, desired, expected) == expected);
22 : }
23 :
24 : static inline void cfd_atomic_store(cfd_atomic_int* ptr, int value) {
25 : InterlockedExchange(ptr, value);
26 : }
27 :
28 : #else
29 : #include <stdatomic.h>
30 :
31 : typedef atomic_int cfd_atomic_int;
32 :
33 296 : static inline int cfd_atomic_load(cfd_atomic_int* ptr) {
34 212 : return atomic_load(ptr);
35 : }
36 :
37 423 : static inline int cfd_atomic_cas(cfd_atomic_int* ptr, int expected, int desired) {
38 : // atomic_compare_exchange_strong takes &expected
39 423 : int exp = expected;
40 423 : return atomic_compare_exchange_strong(ptr, &exp, desired);
41 : }
42 :
43 : static inline void cfd_atomic_store(cfd_atomic_int* ptr, int value) {
44 : atomic_store(ptr, value);
45 : }
46 :
47 : #endif
48 :
49 : #endif // CFD_THREADING_INTERNAL_H
|