vkd3d-common: Introduce vkd3d_atomic_exchange_u32().

This commit is contained in:
Henri Verbeet 2024-04-18 18:59:20 +02:00 committed by Alexandre Julliard
parent 5c8a90a6c9
commit b1c326ce56
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 24 additions and 26 deletions

View File

@ -442,6 +442,24 @@ static inline bool vkd3d_atomic_compare_exchange_u32(uint32_t volatile *x, uint3
#endif #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 struct vkd3d_mutex
{ {
#ifdef _WIN32 #ifdef _WIN32

View File

@ -2356,10 +2356,10 @@ static void *vkd3d_desc_object_cache_get(struct vkd3d_desc_object_cache *cache)
{ {
vkd3d_atomic_decrement_u32(&cache->free_count); vkd3d_atomic_decrement_u32(&cache->free_count);
cache->heads[i].head = u.header->next; 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; 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, /* Keeping a free count avoids uncertainty over when this loop should terminate,
* which could result in excess allocations gradually increasing without limit. */ * 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; head = cache->heads[i].head;
u.header->next = head; u.header->next = head;
cache->heads[i].head = u.object; 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); 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; union d3d12_desc_object u;
unsigned int i, next; 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; return;
writes.null_vk_cbv_info.buffer = VK_NULL_HANDLE; 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) for (; i != UINT_MAX; i = next)
{ {
src = &descriptors[i]; src = &descriptors[i];
next = vkd3d_atomic_exchange(&src->next, 0); next = vkd3d_atomic_exchange_u32(&src->next, 0);
next = (int)next >> 1; next = (int)next >> 1;
/* A race exists here between updating src->next and getting the current object. The best /* 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)) while (!vkd3d_atomic_compare_exchange_u32(&descriptor_heap->dirty_list_head, head, i))
{ {
head = descriptor_heap->dirty_list_head; head = descriptor_heap->dirty_list_head;
vkd3d_atomic_exchange(&dst->next, (head << 1) | 1); vkd3d_atomic_exchange_u32(&dst->next, (head << 1) | 1);
} }
} }

View File

@ -204,11 +204,6 @@ union vkd3d_thread_handle
void *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) static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val)
{ {
return InterlockedExchangePointer(x, val); return InterlockedExchangePointer(x, val);
@ -225,26 +220,11 @@ union vkd3d_thread_handle
}; };
# if HAVE_ATOMIC_EXCHANGE_N # 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) static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val)
{ {
return __atomic_exchange_n(x, val, __ATOMIC_SEQ_CST); return __atomic_exchange_n(x, val, __ATOMIC_SEQ_CST);
} }
# elif HAVE_SYNC_BOOL_COMPARE_AND_SWAP # 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) static inline void *vkd3d_atomic_exchange_pointer(void * volatile *x, void *val)
{ {
void *p; void *p;