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 
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 /* Should work on both MSVC and Borland. */
27 #pragma intrinsic(_ReadWriteBarrier)
28 #define pg_compiler_barrier_impl() _ReadWriteBarrier()
29 
30 #ifndef pg_memory_barrier_impl
31 #define pg_memory_barrier_impl() MemoryBarrier()
32 #endif
33 
34 #if defined(HAVE_ATOMICS)
35 
36 #define PG_HAVE_ATOMIC_U32_SUPPORT
37 typedef struct pg_atomic_uint32
38 {
39  volatile uint32 value;
41 
42 #define PG_HAVE_ATOMIC_U64_SUPPORT
43 typedef struct __declspec(align(8)) pg_atomic_uint64
44 {
45  volatile uint64 value;
46 } pg_atomic_uint64;
47 
48 
49 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32
50 static inline bool
52  uint32 *expected, uint32 newval)
53 {
54  bool ret;
55  uint32 current;
56  current = InterlockedCompareExchange(&ptr->value, newval, *expected);
57  ret = current == *expected;
58  *expected = current;
59  return ret;
60 }
61 
62 #define PG_HAVE_ATOMIC_FETCH_ADD_U32
63 static inline uint32
65 {
66  return InterlockedExchangeAdd(&ptr->value, add_);
67 }
68 
69 /*
70  * The non-intrinsics versions are only available in vista upwards, so use the
71  * intrinsic version. Only supported on >486, but we require XP as a minimum
72  * baseline, which doesn't support the 486, so we don't need to add checks for
73  * that case.
74  */
75 #pragma intrinsic(_InterlockedCompareExchange64)
76 
77 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64
78 static inline bool
79 pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
80  uint64 *expected, uint64 newval)
81 {
82  bool ret;
83  uint64 current;
84  current = _InterlockedCompareExchange64(&ptr->value, newval, *expected);
85  ret = current == *expected;
86  *expected = current;
87  return ret;
88 }
89 
90 /* Only implemented on itanium and 64bit builds */
91 #ifdef _WIN64
92 #pragma intrinsic(_InterlockedExchangeAdd64)
93 
94 #define PG_HAVE_ATOMIC_FETCH_ADD_U64
95 static inline uint64
96 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
97 {
98  return _InterlockedExchangeAdd64(&ptr->value, add_);
99 }
100 #endif /* _WIN64 */
101 
102 #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:120
volatile uint32 value
Definition: fallback.h:100
signed int int32
Definition: c.h:256
uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.c:148
unsigned int uint32
Definition: c.h:268
#define newval