inc/xrt: Add Windows implementations of atomic intrinsics.

This commit is contained in:
Ryan Pavlik 2020-07-15 13:01:00 -05:00 committed by Jakob Bornecrantz
parent 7817d2ea04
commit 3ff56f4a3c
2 changed files with 46 additions and 8 deletions

View file

@ -17,6 +17,18 @@
#include <stdint.h>
#include <stdbool.h>
#ifdef _MSC_VER
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // !WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif // !NOMINMAX
#include <intrin.h>
// for atomic intrinsics
#include <windows.h>
#endif // _MSC_VER
/*!
* Array size helper.
@ -71,7 +83,6 @@
#elif defined(__clang__) || defined(__GNUC__)
#define XRT_DEBUGBREAK() __builtin_trap()
#elif defined(_MSC_VER)
#include <intrin.h>
#define XRT_DEBUGBREAK() __debugbreak()
#else
#error "compiler not supported"
@ -79,14 +90,41 @@
typedef volatile int32_t xrt_atomic_s32_t;
static inline int32_t
xrt_atomic_s32_inc_return(xrt_atomic_s32_t *p)
{
#if defined(__GNUC__)
#define xrt_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
#define xrt_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
#define xrt_atomic_cmpxchg(v, old, _new) \
__sync_val_compare_and_swap((v), (old), (_new))
return __sync_add_and_fetch(p, 1);
#elif defined(_MSC_VER)
return InterlockedIncrement((volatile LONG *)p);
#else
#error "compiler not supported"
#endif
}
static inline int32_t
xrt_atomic_s32_dec_return(xrt_atomic_s32_t *p)
{
#if defined(__GNUC__)
return __sync_sub_and_fetch(p, 1);
#elif defined(_MSC_VER)
return InterlockedDecrement((volatile LONG *)p);
#else
#error "compiler not supported"
#endif
}
static inline int32_t
xrt_atomic_s32_cmpxchg(xrt_atomic_s32_t *p, int32_t old_, int32_t new_)
{
#if defined(__GNUC__)
return __sync_val_compare_and_swap(p, old_, new_);
#elif defined(_MSC_VER)
return InterlockedCompareExchange((volatile LONG *)p, old_, new_);
#else
#error "compiler not supported"
#endif
}
/*!
* Get the holder from a pointer to a field.

View file

@ -28,7 +28,7 @@ extern "C" {
*/
struct xrt_reference
{
uint32_t count;
xrt_atomic_s32_t count;
};
/*!
@ -617,14 +617,14 @@ union xrt_output_value {
static inline bool
xrt_reference_dec(struct xrt_reference *xref)
{
int count = xrt_atomic_dec_return(&xref->count);
int32_t count = xrt_atomic_s32_dec_return(&xref->count);
return count == 0;
}
static inline void
xrt_reference_inc(struct xrt_reference *xref)
{
xrt_atomic_inc_return(&xref->count);
xrt_atomic_s32_inc_return(&xref->count);
}