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-2023, 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_FETCH_ADD_U32
62 static inline uint32
64 {
65  return InterlockedExchangeAdd(&ptr->value, add_);
66 }
67 
68 /*
69  * The non-intrinsics versions are only available in vista upwards, so use the
70  * intrinsic version. Only supported on >486, but we require XP as a minimum
71  * baseline, which doesn't support the 486, so we don't need to add checks for
72  * that case.
73  */
74 #pragma intrinsic(_InterlockedCompareExchange64)
75 
76 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64
77 static inline bool
79  uint64 *expected, uint64 newval)
80 {
81  bool ret;
82  uint64 current;
83  current = _InterlockedCompareExchange64(&ptr->value, newval, *expected);
84  ret = current == *expected;
85  *expected = current;
86  return ret;
87 }
88 
89 /* Only implemented on 64bit builds */
90 #ifdef _WIN64
91 #pragma intrinsic(_InterlockedExchangeAdd64)
92 
93 #define PG_HAVE_ATOMIC_FETCH_ADD_U64
94 static inline uint64
95 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
96 {
97  return _InterlockedExchangeAdd64(&ptr->value, add_);
98 }
99 #endif /* _WIN64 */
100 
101 #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:495
signed int int32
Definition: c.h:483
struct pg_atomic_uint64 pg_atomic_uint64
#define newval
static struct @148 value
volatile uint32 value
Definition: arch-ppc.h:31
volatile uint64 value
Definition: fallback.h:119