mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-01 04:36:07 +00:00
inc/xrt: Add Windows implementations of atomic intrinsics.
This commit is contained in:
parent
7817d2ea04
commit
3ff56f4a3c
|
@ -17,6 +17,18 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.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.
|
* Array size helper.
|
||||||
|
@ -71,7 +83,6 @@
|
||||||
#elif defined(__clang__) || defined(__GNUC__)
|
#elif defined(__clang__) || defined(__GNUC__)
|
||||||
#define XRT_DEBUGBREAK() __builtin_trap()
|
#define XRT_DEBUGBREAK() __builtin_trap()
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
#include <intrin.h>
|
|
||||||
#define XRT_DEBUGBREAK() __debugbreak()
|
#define XRT_DEBUGBREAK() __debugbreak()
|
||||||
#else
|
#else
|
||||||
#error "compiler not supported"
|
#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__)
|
#if defined(__GNUC__)
|
||||||
#define xrt_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
|
return __sync_add_and_fetch(p, 1);
|
||||||
#define xrt_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
|
#elif defined(_MSC_VER)
|
||||||
#define xrt_atomic_cmpxchg(v, old, _new) \
|
return InterlockedIncrement((volatile LONG *)p);
|
||||||
__sync_val_compare_and_swap((v), (old), (_new))
|
|
||||||
#else
|
#else
|
||||||
#error "compiler not supported"
|
#error "compiler not supported"
|
||||||
#endif
|
#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.
|
* Get the holder from a pointer to a field.
|
||||||
|
|
|
@ -28,7 +28,7 @@ extern "C" {
|
||||||
*/
|
*/
|
||||||
struct xrt_reference
|
struct xrt_reference
|
||||||
{
|
{
|
||||||
uint32_t count;
|
xrt_atomic_s32_t count;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -617,14 +617,14 @@ union xrt_output_value {
|
||||||
static inline bool
|
static inline bool
|
||||||
xrt_reference_dec(struct xrt_reference *xref)
|
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;
|
return count == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
xrt_reference_inc(struct xrt_reference *xref)
|
xrt_reference_inc(struct xrt_reference *xref)
|
||||||
{
|
{
|
||||||
xrt_atomic_inc_return(&xref->count);
|
xrt_atomic_s32_inc_return(&xref->count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue