Module: vkd3d Branch: master Commit: 5c8a90a6c995d221033af863a7f667ec6c9f7bed URL: https://gitlab.winehq.org/wine/vkd3d/-/commit/5c8a90a6c995d221033af863a7f667...
Author: Henri Verbeet hverbeet@codeweavers.com Date: Thu Apr 18 18:23:41 2024 +0200
vkd3d-common: Introduce vkd3d_atomic_compare_exchange_u32().
---
include/private/vkd3d_common.h | 11 +++++++++++ libs/vkd3d/resource.c | 8 ++++---- libs/vkd3d/vkd3d_private.h | 16 +--------------- 3 files changed, 16 insertions(+), 19 deletions(-)
diff --git a/include/private/vkd3d_common.h b/include/private/vkd3d_common.h index de6878a4..88c563eb 100644 --- a/include/private/vkd3d_common.h +++ b/include/private/vkd3d_common.h @@ -431,6 +431,17 @@ static inline uint32_t vkd3d_atomic_increment_u32(uint32_t volatile *x) return vkd3d_atomic_add_fetch_u32(x, 1); }
+static inline bool vkd3d_atomic_compare_exchange_u32(uint32_t volatile *x, uint32_t expected, uint32_t val) +{ +#if HAVE_SYNC_BOOL_COMPARE_AND_SWAP + return __sync_bool_compare_and_swap(x, expected, val); +#elif defined(_WIN32) + return InterlockedCompareExchange((LONG *)x, val, expected) == expected; +#else +# error "vkd3d_atomic_compare_exchange_u32() not implemented for this platform" +#endif +} + struct vkd3d_mutex { #ifdef _WIN32 diff --git a/libs/vkd3d/resource.c b/libs/vkd3d/resource.c index 6ee3afa4..ff9721fb 100644 --- a/libs/vkd3d/resource.c +++ b/libs/vkd3d/resource.c @@ -2350,7 +2350,7 @@ static void *vkd3d_desc_object_cache_get(struct vkd3d_desc_object_cache *cache) i = vkd3d_atomic_increment_u32(&cache->next_index) & HEAD_INDEX_MASK; for (;;) { - if (vkd3d_atomic_compare_exchange(&cache->heads[i].spinlock, 0, 1)) + if (vkd3d_atomic_compare_exchange_u32(&cache->heads[i].spinlock, 0, 1)) { if ((u.object = cache->heads[i].head)) { @@ -2381,7 +2381,7 @@ static void vkd3d_desc_object_cache_push(struct vkd3d_desc_object_cache *cache, i = vkd3d_atomic_increment_u32(&cache->next_index) & HEAD_INDEX_MASK; for (;;) { - if (vkd3d_atomic_compare_exchange(&cache->heads[i].spinlock, 0, 1)) + if (vkd3d_atomic_compare_exchange_u32(&cache->heads[i].spinlock, 0, 1)) break; i = (i + 1) & HEAD_INDEX_MASK; } @@ -2695,10 +2695,10 @@ static void d3d12_desc_mark_as_modified(struct d3d12_desc *dst, struct d3d12_des head = descriptor_heap->dirty_list_head;
/* Only one thread can swap the value away from zero. */ - if (!vkd3d_atomic_compare_exchange(&dst->next, 0, (head << 1) | 1)) + if (!vkd3d_atomic_compare_exchange_u32(&dst->next, 0, (head << 1) | 1)) return; /* Now it is safe to modify 'next' to another nonzero value if necessary. */ - while (!vkd3d_atomic_compare_exchange(&descriptor_heap->dirty_list_head, head, i)) + while (!vkd3d_atomic_compare_exchange_u32(&descriptor_heap->dirty_list_head, head, i)) { head = descriptor_heap->dirty_list_head; vkd3d_atomic_exchange(&dst->next, (head << 1) | 1); diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h index bf02a76f..f9b0a093 100644 --- a/libs/vkd3d/vkd3d_private.h +++ b/libs/vkd3d/vkd3d_private.h @@ -204,11 +204,6 @@ union vkd3d_thread_handle void *handle; };
-static inline bool vkd3d_atomic_compare_exchange(unsigned int volatile *x, unsigned int cmp, unsigned int xchg) -{ - return InterlockedCompareExchange((LONG volatile *)x, xchg, cmp) == cmp; -} - static inline unsigned int vkd3d_atomic_exchange(unsigned int volatile *x, unsigned int val) { return InterlockedExchange((LONG volatile *)x, val); @@ -229,15 +224,6 @@ union vkd3d_thread_handle void *handle; };
-# if HAVE_SYNC_BOOL_COMPARE_AND_SWAP -static inline bool vkd3d_atomic_compare_exchange(unsigned int volatile *x, unsigned int cmp, unsigned int xchg) -{ - return __sync_bool_compare_and_swap(x, cmp, xchg); -} -# else -# error "vkd3d_atomic_compare_exchange() not implemented for this platform" -# endif - # if HAVE_ATOMIC_EXCHANGE_N static inline unsigned int vkd3d_atomic_exchange(unsigned int volatile *x, unsigned int val) { @@ -735,7 +721,7 @@ static inline bool vkd3d_view_incref(void *desc) if (refcount <= 0) return false; } - while (!vkd3d_atomic_compare_exchange(&h->refcount, refcount, refcount + 1)); + while (!vkd3d_atomic_compare_exchange_u32(&h->refcount, refcount, refcount + 1));
return true; }