| 1 |
|
| 2 |
s |
| 3 |
s |
| 4 |
C |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifndef MSCVER_STDATOMIC_H_ |
| 11 |
#define MSCVER_STDATOMIC_H_ |
| 12 |
|
| 13 |
#if !defined(__cplusplus) && defined(_MSC_VER) |
| 14 |
|
| 15 |
#pragma warning(push) |
| 16 |
#pragma warning(disable:4067) |
| 17 |
|
| 18 |
#if defined(__clang__) && __has_include_next(<stdatomic.h>) |
| 19 |
|
| 20 |
# include_next <stdatomic.h> |
| 21 |
#else |
| 22 |
|
| 23 |
#include <windows.h> |
| 24 |
|
| 25 |
#include <stddef.h> |
| 26 |
#include <stdint.h> |
| 27 |
|
| 28 |
typedef volatile ULONG __declspec(align(32)) atomic_bool; |
| 29 |
typedef volatile ULONG __declspec(align(32)) atomic_int; |
| 30 |
typedef volatile ULONG __declspec(align(32)) atomic_uint; |
| 31 |
typedef volatile ULONG __declspec(align(32)) atomic_ulong; |
| 32 |
typedef volatile USHORT __declspec(align(16)) atomic_uint_fast16_t; |
| 33 |
typedef volatile ULONG64 __declspec(align(32)) atomic_uint_fast64_t; |
| 34 |
|
| 35 |
typedef enum { |
| 36 |
memory_order_relaxed, |
| 37 |
memory_order_acquire, |
| 38 |
memory_order_release |
| 39 |
} msvc_atomic_memory_order; |
| 40 |
|
| 41 |
#define atomic_init(p_a, v) atomic_store(p_a, v) |
| 42 |
|
| 43 |
static inline ULONG atomic_load_32(volatile ULONG *p_a) |
| 44 |
{ |
| 45 |
return (ULONG)InterlockedCompareExchange((LONG volatile *)p_a, 0, 0); |
| 46 |
} |
| 47 |
|
| 48 |
static inline ULONG64 atomic_load_64(volatile ULONG64 *p_a) |
| 49 |
{ |
| 50 |
return (ULONG64)InterlockedCompareExchange64((LONG64 volatile *)p_a, 0, 0); |
| 51 |
} |
| 52 |
|
| 53 |
#define atomic_load(p_a) \ |
| 54 |
(sizeof(*(p_a)) == 8 ? \ |
| 55 |
(uint64_t)atomic_load_64((volatile ULONG64 *)(p_a)) : \ |
| 56 |
(uint64_t)atomic_load_32((volatile ULONG *)(p_a))) |
| 57 |
|
| 58 |
static inline void atomic_store_32(volatile ULONG *p_a, ULONG v) |
| 59 |
{ |
| 60 |
InterlockedExchange((LONG volatile *)p_a, (LONG)v); |
| 61 |
} |
| 62 |
|
| 63 |
static inline void atomic_store_64(volatile ULONG64 *p_a, ULONG64 v) |
| 64 |
{ |
| 65 |
InterlockedExchange64((LONG64 volatile *)p_a, (LONG64)v); |
| 66 |
} |
| 67 |
|
| 68 |
#define atomic_store(p_a, v) \ |
| 69 |
do { \ |
| 70 |
if (sizeof(*(p_a)) == 8) \ |
| 71 |
atomic_store_64((volatile ULONG64 *)(p_a), (ULONG64)(v)); \ |
| 72 |
else \ |
| 73 |
atomic_store_32((volatile ULONG *)(p_a), (ULONG)(v)); \ |
| 74 |
} while (0) |
| 75 |
|
| 76 |
#define atomic_load_explicit(p_a, mo) atomic_load(p_a) |
| 77 |
#define atomic_store_explicit(p_a, v, mo) atomic_store(p_a, v) |
| 78 |
|
| 79 |
static inline ULONG atomic_fetch_add_32(volatile ULONG *p_a, ULONG inc) |
| 80 |
{ |
| 81 |
return (ULONG)InterlockedExchangeAdd((LONG volatile *)p_a, (LONG)inc); |
| 82 |
} |
| 83 |
|
| 84 |
static inline ULONG64 atomic_fetch_add_64(volatile ULONG64 *p_a, ULONG64 inc) |
| 85 |
{ |
| 86 |
return (ULONG64)InterlockedExchangeAdd64((LONG64 volatile *)p_a, (LONG64)inc); |
| 87 |
} |
| 88 |
|
| 89 |
#define atomic_fetch_add(p_a, inc) \ |
| 90 |
(sizeof(*(p_a)) == 8 ? \ |
| 91 |
(uint64_t)atomic_fetch_add_64((volatile ULONG64 *)(p_a), (ULONG64)(inc)) : \ |
| 92 |
(uint64_t)atomic_fetch_add_32((volatile ULONG *)(p_a), (ULONG)(inc))) |
| 93 |
|
| 94 |
static inline ULONG atomic_fetch_sub_32(volatile ULONG *p_a, ULONG dec) |
| 95 |
{ |
| 96 |
return (ULONG)InterlockedExchangeAdd((LONG volatile *)p_a, -(LONG)dec); |
| 97 |
} |
| 98 |
|
| 99 |
static inline ULONG64 atomic_fetch_sub_64(volatile ULONG64 *p_a, ULONG64 dec) |
| 100 |
{ |
| 101 |
return (ULONG64)InterlockedExchangeAdd64((LONG64 volatile *)p_a, -(LONG64)dec); |
| 102 |
} |
| 103 |
|
| 104 |
#define atomic_fetch_sub(p_a, dec) \ |
| 105 |
(sizeof(*(p_a)) == 8 ? \ |
| 106 |
(uint64_t)atomic_fetch_sub_64((volatile ULONG64 *)(p_a), (ULONG64)(dec)) : \ |
| 107 |
(uint64_t)atomic_fetch_sub_32((volatile ULONG *)(p_a), (ULONG)(dec))) |
| 108 |
|
| 109 |
#define atomic_fetch_add_explicit(p_a, inc, mo) atomic_fetch_add(p_a, inc) |
| 110 |
#define atomic_fetch_sub_explicit(p_a, inc, mo) atomic_fetch_sub(p_a, inc) |
| 111 |
|
| 112 |
static inline int atomic_compare_exchange_weak_32(volatile ULONG *obj, ULONG *expected, ULONG desired) |
| 113 |
{ |
| 114 |
ULONG old = *expected; |
| 115 |
*expected = (ULONG)InterlockedCompareExchange((LONG volatile *)obj, (LONG)desired, (LONG)old); |
| 116 |
return old == *expected; |
| 117 |
} |
| 118 |
|
| 119 |
static inline int atomic_compare_exchange_weak_64(volatile ULONG64 *obj, ULONG64 *expected, ULONG64 desired) |
| 120 |
{ |
| 121 |
ULONG64 old = *expected; |
| 122 |
*expected = (ULONG64)InterlockedCompareExchange64((LONG64 volatile *)obj, (LONG64)desired, (LONG64)old); |
| 123 |
return old == *expected; |
| 124 |
} |
| 125 |
|
| 126 |
#define atomic_compare_exchange_weak(obj, expected, desired) \ |
| 127 |
(sizeof(*(obj)) == 8 ? \ |
| 128 |
atomic_compare_exchange_weak_64((volatile ULONG64 *)(obj), (ULONG64 *)(expected), (ULONG64)(desired)) : \ |
| 129 |
atomic_compare_exchange_weak_32((volatile ULONG *)(obj), (ULONG *)(expected), (ULONG)(desired))) |
| 130 |
|
| 131 |
#endif |
| 132 |
|
| 133 |
#pragma warning(pop) |
| 134 |
|
| 135 |
#endif |
| 136 |
|
| 137 |
#endif |