PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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 #include <windows.h>
21 
22 /* intentionally no include guards, should only be included by atomics.h */
23 #ifndef INSIDE_ATOMICS_H
24 #error "should be included via atomics.h"
25 #endif
26 
27 /* Should work on both MSVC and Borland. */
28 #pragma intrinsic(_ReadWriteBarrier)
29 #define pg_compiler_barrier_impl() _ReadWriteBarrier()
30 
31 #ifndef pg_memory_barrier_impl
32 #define pg_memory_barrier_impl() MemoryBarrier()
33 #endif
34 
35 #if defined(HAVE_ATOMICS)
36 
37 #define PG_HAVE_ATOMIC_U32_SUPPORT
38 typedef struct pg_atomic_uint32
39 {
40  volatile uint32 value;
42 
43 #define PG_HAVE_ATOMIC_U64_SUPPORT
44 typedef struct __declspec(align(8)) pg_atomic_uint64
45 {
46  volatile uint64 value;
47 } pg_atomic_uint64;
48 
49 
50 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32
51 static inline bool
53  uint32 *expected, uint32 newval)
54 {
55  bool ret;
56  uint32 current;
57  current = InterlockedCompareExchange(&ptr->value, newval, *expected);
58  ret = current == *expected;
59  *expected = current;
60  return ret;
61 }
62 
63 #define PG_HAVE_ATOMIC_FETCH_ADD_U32
64 static inline uint32
66 {
67  return InterlockedExchangeAdd(&ptr->value, add_);
68 }
69 
70 /*
71  * The non-intrinsics versions are only available in vista upwards, so use the
72  * intrinsic version. Only supported on >486, but we require XP as a minimum
73  * baseline, which doesn't support the 486, so we don't need to add checks for
74  * that case.
75  */
76 #pragma intrinsic(_InterlockedCompareExchange64)
77 
78 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64
79 static inline bool
80 pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
81  uint64 *expected, uint64 newval)
82 {
83  bool ret;
84  uint64 current;
85  current = _InterlockedCompareExchange64(&ptr->value, newval, *expected);
86  ret = current == *expected;
87  *expected = current;
88  return ret;
89 }
90 
91 /* Only implemented on itanium and 64bit builds */
92 #ifdef _WIN64
93 #pragma intrinsic(_InterlockedExchangeAdd64)
94 
95 #define PG_HAVE_ATOMIC_FETCH_ADD_U64
96 static inline uint64
97 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
98 {
99  return _InterlockedExchangeAdd64(&ptr->value, add_);
100 }
101 #endif /* _WIN64 */
102 
103 #endif /* HAVE_ATOMICS */
static struct @76 value
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:121
volatile uint32 value
Definition: fallback.h:100
signed int int32
Definition: c.h:253
uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.c:149
unsigned int uint32
Definition: c.h:265
#define newval