PostgreSQL Source Code  git master
generic-msvc.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * generic-msvc.h
4  * Atomic operations support when using MSVC
5  *
6  * Portions Copyright (c) 1996-2024, 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 #if defined(HAVE_ATOMICS)
34 
35 #define PG_HAVE_ATOMIC_U32_SUPPORT
36 typedef struct pg_atomic_uint32
37 {
38  volatile uint32 value;
40 
41 #define PG_HAVE_ATOMIC_U64_SUPPORT
42 typedef struct pg_attribute_aligned(8) pg_atomic_uint64
43 {
44  volatile uint64 value;
46 
47 
48 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32
49 static inline bool
51  uint32 *expected, uint32 newval)
52 {
53  bool ret;
54  uint32 current;
55  current = InterlockedCompareExchange(&ptr->value, newval, *expected);
56  ret = current == *expected;
57  *expected = current;
58  return ret;
59 }
60 
61 #define PG_HAVE_ATOMIC_EXCHANGE_U32
62 static inline uint32
63 pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval)
64 {
65  return InterlockedExchange(&ptr->value, newval);
66 }
67 
68 #define PG_HAVE_ATOMIC_FETCH_ADD_U32
69 static inline uint32
71 {
72  return InterlockedExchangeAdd(&ptr->value, add_);
73 }
74 
75 /*
76  * The non-intrinsics versions are only available in vista upwards, so use the
77  * intrinsic version. Only supported on >486, but we require XP as a minimum
78  * baseline, which doesn't support the 486, so we don't need to add checks for
79  * that case.
80  */
81 #pragma intrinsic(_InterlockedCompareExchange64)
82 
83 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64
84 static inline bool
86  uint64 *expected, uint64 newval)
87 {
88  bool ret;
89  uint64 current;
90  current = _InterlockedCompareExchange64(&ptr->value, newval, *expected);
91  ret = current == *expected;
92  *expected = current;
93  return ret;
94 }
95 
96 /* Only implemented on 64bit builds */
97 #ifdef _WIN64
98 
99 #pragma intrinsic(_InterlockedExchange64)
100 
101 #define PG_HAVE_ATOMIC_EXCHANGE_U64
102 static inline uint64
103 pg_atomic_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 newval)
104 {
105  return _InterlockedExchange64(&ptr->value, newval);
106 }
107 
108 #pragma intrinsic(_InterlockedExchangeAdd64)
109 
110 #define PG_HAVE_ATOMIC_FETCH_ADD_U64
111 static inline uint64
112 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
113 {
114  return _InterlockedExchangeAdd64(&ptr->value, add_);
115 }
116 
117 #endif /* _WIN64 */
118 
119 #endif /* HAVE_ATOMICS */
struct pg_atomic_uint32 pg_atomic_uint32
bool pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.c:137
uint64 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.c:228
uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.c:165
bool pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 *expected, uint64 newval)
Definition: atomics.c:200
unsigned int uint32
Definition: c.h:506
signed int int32
Definition: c.h:494
struct pg_atomic_uint64 pg_atomic_uint64
#define newval
static struct @155 value
volatile uint32 value
Definition: arch-ppc.h:31
volatile uint64 value
Definition: fallback.h:119