vkd3d-common: Introduce vkd3d_atomic_exchange_ptr().

This commit is contained in:
Henri Verbeet 2024-04-18 19:15:52 +02:00 committed by Alexandre Julliard
parent b1c326ce56
commit af33caf036
Notes: Alexandre Julliard 2024-04-25 00:15:41 +02:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Alexandre Julliard (@julliard)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/829
3 changed files with 32 additions and 38 deletions

View File

@ -442,6 +442,17 @@ static inline bool vkd3d_atomic_compare_exchange_u32(uint32_t volatile *x, uint3
#endif
}
static inline bool vkd3d_atomic_compare_exchange_ptr(void * volatile *x, void *expected, void *val)
{
#if HAVE_SYNC_BOOL_COMPARE_AND_SWAP
return __sync_bool_compare_and_swap(x, expected, val);
#elif defined(_WIN32)
return InterlockedCompareExchangePointer(x, val, expected) == expected;
#else
# error "vkd3d_atomic_compare_exchange_ptr() not implemented for this platform"
#endif
}
static inline uint32_t vkd3d_atomic_exchange_u32(uint32_t volatile *x, uint32_t val)
{
#if HAVE_ATOMIC_EXCHANGE_N
@ -460,6 +471,24 @@ static inline uint32_t vkd3d_atomic_exchange_u32(uint32_t volatile *x, uint32_t
#endif
}
static inline void *vkd3d_atomic_exchange_ptr(void * volatile *x, void *val)
{
#if HAVE_ATOMIC_EXCHANGE_N
return __atomic_exchange_n(x, val, __ATOMIC_SEQ_CST);
#elif defined(_WIN32)
return InterlockedExchangePointer(x, val);
#else
void *expected;
do
{
expected = *x;
} while (!vkd3d_atomic_compare_exchange_ptr(x, expected, val));
return expected;
#endif
}
struct vkd3d_mutex
{
#ifdef _WIN32

View File

@ -2473,7 +2473,7 @@ void vkd3d_view_decref(void *view, struct d3d12_device *device)
static inline void d3d12_desc_replace(struct d3d12_desc *dst, void *view, struct d3d12_device *device)
{
if ((view = vkd3d_atomic_exchange_pointer(&dst->s.u.object, view)))
if ((view = vkd3d_atomic_exchange_ptr(&dst->s.u.object, view)))
vkd3d_view_decref(view, device);
}

View File

@ -197,49 +197,14 @@ struct vkd3d_instance
unsigned int refcount;
};
#ifdef _WIN32
union vkd3d_thread_handle
{
void *handle;
};
static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val)
{
return InterlockedExchangePointer(x, val);
}
#else /* _WIN32 */
#include <pthread.h>
union vkd3d_thread_handle
{
#ifndef _WIN32
pthread_t pthread;
#endif
void *handle;
};
# if HAVE_ATOMIC_EXCHANGE_N
static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val)
{
return __atomic_exchange_n(x, val, __ATOMIC_SEQ_CST);
}
# elif HAVE_SYNC_BOOL_COMPARE_AND_SWAP
static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val)
{
void *p;
do
{
p = *x;
} while (!__sync_bool_compare_and_swap(x, p, val));
return p;
}
# else
# error "vkd3d_atomic_exchange() not implemented for this platform"
# endif
#endif /* _WIN32 */
HRESULT vkd3d_create_thread(struct vkd3d_instance *instance,
PFN_vkd3d_thread thread_main, void *data, union vkd3d_thread_handle *thread);
HRESULT vkd3d_join_thread(struct vkd3d_instance *instance, union vkd3d_thread_handle *thread);