| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* generic-msvc.h |
| 4 |
* Atomic operations support when using MSVC |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* NOTES: |
| 10 |
* |
| 11 |
* Documentation: |
| 12 |
* * Interlocked Variable Access |
| 13 |
* http://msdn.microsoft.com/en-us/library/ms684122%28VS.85%29.aspx |
| 14 |
* |
| 15 |
* src/include/port/atomics/generic-msvc.h |
| 16 |
* |
| 17 |
*------------------------------------------------------------------------- |
| 18 |
*/ |
| 19 |
#include <intrin.h> |
| 20 |
|
| 21 |
/* intentionally no include guards, should only be included by atomics.h */ |
| 22 |
#ifndef INSIDE_ATOMICS_H |
| 23 |
#error "should be included via atomics.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#pragma intrinsic(_ReadWriteBarrier) |
| 27 |
#define pg_compiler_barrier_impl() _ReadWriteBarrier() |
| 28 |
|
| 29 |
#ifndef pg_memory_barrier_impl |
| 30 |
#define pg_memory_barrier_impl() MemoryBarrier() |
| 31 |
#endif |
| 32 |
|
| 33 |
#define PG_HAVE_ATOMIC_U32_SUPPORT |
| 34 |
typedef struct pg_atomic_uint32 |
| 35 |
{ |
| 36 |
volatile uint32 value; |
| 37 |
} pg_atomic_uint32; |
| 38 |
|
| 39 |
#define PG_HAVE_ATOMIC_U64_SUPPORT |
| 40 |
typedef struct pg_attribute_aligned(8) pg_atomic_uint64 |
| 41 |
{ |
| 42 |
volatile uint64 value; |
| 43 |
} pg_atomic_uint64; |
| 44 |
|
| 45 |
|
| 46 |
#define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32 |
| 47 |
static inline bool |
| 48 |
pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, |
| 49 |
uint32 *expected, uint32 newval) |
| 50 |
{ |
| 51 |
bool ret; |
| 52 |
uint32 current; |
| 53 |
current = InterlockedCompareExchange(&ptr->value, newval, *expected); |
| 54 |
ret = current == *expected; |
| 55 |
*expected = current; |
| 56 |
return ret; |
| 57 |
} |
| 58 |
|
| 59 |
#define PG_HAVE_ATOMIC_EXCHANGE_U32 |
| 60 |
static inline uint32 |
| 61 |
pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval) |
| 62 |
{ |
| 63 |
return InterlockedExchange(&ptr->value, newval); |
| 64 |
} |
| 65 |
|
| 66 |
#define PG_HAVE_ATOMIC_FETCH_ADD_U32 |
| 67 |
static inline uint32 |
| 68 |
pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_) |
| 69 |
{ |
| 70 |
return InterlockedExchangeAdd(&ptr->value, add_); |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* The non-intrinsics versions are only available in vista upwards, so use the |
| 75 |
* intrinsic version. Only supported on >486, but we require XP as a minimum |
| 76 |
* baseline, which doesn't support the 486, so we don't need to add checks for |
| 77 |
* that case. |
| 78 |
*/ |
| 79 |
#pragma intrinsic(_InterlockedCompareExchange64) |
| 80 |
|
| 81 |
#define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64 |
| 82 |
static inline bool |
| 83 |
pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, |
| 84 |
uint64 *expected, uint64 newval) |
| 85 |
{ |
| 86 |
bool ret; |
| 87 |
uint64 current; |
| 88 |
current = _InterlockedCompareExchange64(&ptr->value, newval, *expected); |
| 89 |
ret = current == *expected; |
| 90 |
*expected = current; |
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
/* Only implemented on 64bit builds */ |
| 95 |
#ifdef _WIN64 |
| 96 |
|
| 97 |
#pragma intrinsic(_InterlockedExchange64) |
| 98 |
|
| 99 |
#define PG_HAVE_ATOMIC_EXCHANGE_U64 |
| 100 |
static inline uint64 |
| 101 |
pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 newval) |
| 102 |
{ |
| 103 |
return _InterlockedExchange64(&ptr->value, newval); |
| 104 |
} |
| 105 |
|
| 106 |
#pragma intrinsic(_InterlockedExchangeAdd64) |
| 107 |
|
| 108 |
#define PG_HAVE_ATOMIC_FETCH_ADD_U64 |
| 109 |
static inline uint64 |
| 110 |
pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_) |
| 111 |
{ |
| 112 |
return _InterlockedExchangeAdd64(&ptr->value, add_); |
| 113 |
} |
| 114 |
|
| 115 |
#endif /* _WIN64 */ |