codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * generic-msvc.h4 * Atomic operations support when using MSVC5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * NOTES:10 *11 * Documentation:12 * * Interlocked Variable Access13 * http://msdn.microsoft.com/en-us/library/ms684122%28VS.85%29.aspx14 *15 * src/include/port/atomics/generic-msvc.h16 *17 *-------------------------------------------------------------------------18 */19#include <intrin.h>20 21/* intentionally no include guards, should only be included by atomics.h */22#ifndef INSIDE_ATOMICS_H23#error "should be included via atomics.h"24#endif25 26#pragma intrinsic(_ReadWriteBarrier)27#define pg_compiler_barrier_impl() _ReadWriteBarrier()28 29#ifndef pg_memory_barrier_impl30#define pg_memory_barrier_impl() MemoryBarrier()31#endif32 33#if defined(HAVE_ATOMICS)34 35#define PG_HAVE_ATOMIC_U32_SUPPORT36typedef struct pg_atomic_uint3237{38 volatile uint32 value;39} pg_atomic_uint32;40 41#define PG_HAVE_ATOMIC_U64_SUPPORT42typedef struct pg_attribute_aligned(8) pg_atomic_uint6443{44 volatile uint64 value;45} pg_atomic_uint64;46 47 48#define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U3249static inline bool50pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr,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_U3262static inline uint3263pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)64{65 return InterlockedExchangeAdd(&ptr->value, add_);66}67 68/*69 * The non-intrinsics versions are only available in vista upwards, so use the70 * intrinsic version. Only supported on >486, but we require XP as a minimum71 * baseline, which doesn't support the 486, so we don't need to add checks for72 * that case.73 */74#pragma intrinsic(_InterlockedCompareExchange64)75 76#define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U6477static inline bool78pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,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 _WIN6491#pragma intrinsic(_InterlockedExchangeAdd64)92 93#define PG_HAVE_ATOMIC_FETCH_ADD_U6494static inline uint6495pg_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 */102 