1 /*-------------------------------------------------------------------------
4 * Atomic operations support when using MSVC
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
12 * * Interlocked Variable Access
13 * http://msdn.microsoft.com/en-us/library/ms684122%28VS.85%29.aspx
15 * src/include/port/atomics/generic-msvc.h
17 *-------------------------------------------------------------------------
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"
26#pragma intrinsic(_ReadWriteBarrier)
27 #define pg_compiler_barrier_impl() _ReadWriteBarrier()
29#ifndef pg_memory_barrier_impl
30 #define pg_memory_barrier_impl() MemoryBarrier()
33 #define PG_HAVE_ATOMIC_U32_SUPPORT
39 #define PG_HAVE_ATOMIC_U64_SUPPORT
46 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32
53 current = InterlockedCompareExchange(&ptr->
value,
newval, *expected);
54 ret = current == *expected;
59 #define PG_HAVE_ATOMIC_EXCHANGE_U32
66 #define PG_HAVE_ATOMIC_FETCH_ADD_U32
70 return InterlockedExchangeAdd(&ptr->
value, add_);
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
79#pragma intrinsic(_InterlockedCompareExchange64)
81 #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U64
88 current = _InterlockedCompareExchange64(&ptr->
value,
newval, *expected);
89 ret = current == *expected;
94/* Only implemented on 64bit builds */
97#pragma intrinsic(_InterlockedExchange64)
99#define PG_HAVE_ATOMIC_EXCHANGE_U64
103 return _InterlockedExchange64(&ptr->
value,
newval);
106#pragma intrinsic(_InterlockedExchangeAdd64)
108#define PG_HAVE_ATOMIC_FETCH_ADD_U64
112 return _InterlockedExchangeAdd64(&ptr->
value, add_);
uint64 pg_atomic_fetch_add_u64_impl(volatile pg_atomic_uint64 *ptr, int64 add_)
static bool pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
struct pg_attribute_aligned(8) pg_atomic_uint64
static uint32 pg_atomic_exchange_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 newval)
static uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)
struct pg_atomic_uint32 pg_atomic_uint32
static bool pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 *expected, uint64 newval)