From b1c326ce564280638212d7602138ce21a6ab4ba5 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Thu, 18 Apr 2024 18:59:20 +0200 Subject: [PATCH] vkd3d-common: Introduce vkd3d_atomic_exchange_u32(). --- include/private/vkd3d_common.h | 18 ++++++++++++++++++ libs/vkd3d/resource.c | 12 ++++++------ libs/vkd3d/vkd3d_private.h | 20 -------------------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/include/private/vkd3d_common.h b/include/private/vkd3d_common.h index 88c563eb..3e853b06 100644 --- a/include/private/vkd3d_common.h +++ b/include/private/vkd3d_common.h @@ -442,6 +442,24 @@ static inline bool vkd3d_atomic_compare_exchange_u32(uint32_t volatile *x, uint3 #endif } +static inline uint32_t vkd3d_atomic_exchange_u32(uint32_t volatile *x, uint32_t val) +{ +#if HAVE_ATOMIC_EXCHANGE_N + return __atomic_exchange_n(x, val, __ATOMIC_SEQ_CST); +#elif defined(_WIN32) + return InterlockedExchange((LONG *)x, val); +#else + uint32_t expected; + + do + { + expected = *x; + } while (!vkd3d_atomic_compare_exchange_u32(x, expected, val)); + + return expected; +#endif +} + struct vkd3d_mutex { #ifdef _WIN32 diff --git a/libs/vkd3d/resource.c b/libs/vkd3d/resource.c index ff9721fb..e3d1661a 100644 --- a/libs/vkd3d/resource.c +++ b/libs/vkd3d/resource.c @@ -2356,10 +2356,10 @@ static void *vkd3d_desc_object_cache_get(struct vkd3d_desc_object_cache *cache) { vkd3d_atomic_decrement_u32(&cache->free_count); cache->heads[i].head = u.header->next; - vkd3d_atomic_exchange(&cache->heads[i].spinlock, 0); + vkd3d_atomic_exchange_u32(&cache->heads[i].spinlock, 0); return u.object; } - vkd3d_atomic_exchange(&cache->heads[i].spinlock, 0); + vkd3d_atomic_exchange_u32(&cache->heads[i].spinlock, 0); } /* Keeping a free count avoids uncertainty over when this loop should terminate, * which could result in excess allocations gradually increasing without limit. */ @@ -2389,7 +2389,7 @@ static void vkd3d_desc_object_cache_push(struct vkd3d_desc_object_cache *cache, head = cache->heads[i].head; u.header->next = head; cache->heads[i].head = u.object; - vkd3d_atomic_exchange(&cache->heads[i].spinlock, 0); + vkd3d_atomic_exchange_u32(&cache->heads[i].spinlock, 0); vkd3d_atomic_increment_u32(&cache->free_count); } @@ -2652,7 +2652,7 @@ void d3d12_desc_flush_vk_heap_updates_locked(struct d3d12_descriptor_heap *descr union d3d12_desc_object u; unsigned int i, next; - if ((i = vkd3d_atomic_exchange(&descriptor_heap->dirty_list_head, UINT_MAX)) == UINT_MAX) + if ((i = vkd3d_atomic_exchange_u32(&descriptor_heap->dirty_list_head, UINT_MAX)) == UINT_MAX) return; writes.null_vk_cbv_info.buffer = VK_NULL_HANDLE; @@ -2667,7 +2667,7 @@ void d3d12_desc_flush_vk_heap_updates_locked(struct d3d12_descriptor_heap *descr for (; i != UINT_MAX; i = next) { src = &descriptors[i]; - next = vkd3d_atomic_exchange(&src->next, 0); + next = vkd3d_atomic_exchange_u32(&src->next, 0); next = (int)next >> 1; /* A race exists here between updating src->next and getting the current object. The best @@ -2701,7 +2701,7 @@ static void d3d12_desc_mark_as_modified(struct d3d12_desc *dst, struct d3d12_des 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); + vkd3d_atomic_exchange_u32(&dst->next, (head << 1) | 1); } } diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h index f9b0a093..efe049a2 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 unsigned int vkd3d_atomic_exchange(unsigned int volatile *x, unsigned int val) -{ - return InterlockedExchange((LONG volatile *)x, val); -} - static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val) { return InterlockedExchangePointer(x, val); @@ -225,26 +220,11 @@ union vkd3d_thread_handle }; # if HAVE_ATOMIC_EXCHANGE_N -static inline unsigned int vkd3d_atomic_exchange(unsigned int volatile *x, unsigned int val) -{ - return __atomic_exchange_n(x, val, __ATOMIC_SEQ_CST); -} - 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 unsigned int vkd3d_atomic_exchange(unsigned int volatile *x, unsigned int val) -{ - unsigned int i; - do - { - i = *x; - } while (!__sync_bool_compare_and_swap(x, i, val)); - return i; -} - static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val) { void *p;