Atomics: Make naming more obvious about which value is being returned

This commit is contained in:
Sergey Sharybin 2016-11-15 12:16:26 +01:00
parent 46b5cdaa4d
commit 4ee08e9533
18 changed files with 78 additions and 78 deletions

@ -77,13 +77,13 @@
/* Function prototypes. */ /* Function prototypes. */
#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8) #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
ATOMIC_INLINE uint64_t atomic_add_uint64(uint64_t *p, uint64_t x); ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x);
ATOMIC_INLINE uint64_t atomic_sub_uint64(uint64_t *p, uint64_t x); ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x);
ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _new); ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _new);
#endif #endif
ATOMIC_INLINE uint32_t atomic_add_uint32(uint32_t *p, uint32_t x); ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x);
ATOMIC_INLINE uint32_t atomic_sub_uint32(uint32_t *p, uint32_t x); ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x);
ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new); ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new);
ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x); ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x);
@ -93,18 +93,18 @@ ATOMIC_INLINE uint32_t atomic_fetch_and_and_uint32(uint32_t *p, uint32_t x);
ATOMIC_INLINE uint8_t atomic_fetch_and_or_uint8(uint8_t *p, uint8_t b); ATOMIC_INLINE uint8_t atomic_fetch_and_or_uint8(uint8_t *p, uint8_t b);
ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b); ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b);
ATOMIC_INLINE size_t atomic_add_z(size_t *p, size_t x); ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x);
ATOMIC_INLINE size_t atomic_sub_z(size_t *p, size_t x); ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x);
ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new); ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new);
ATOMIC_INLINE unsigned atomic_add_u(unsigned *p, unsigned x); ATOMIC_INLINE unsigned atomic_add_and_fetch_u(unsigned *p, unsigned x);
ATOMIC_INLINE unsigned atomic_sub_u(unsigned *p, unsigned x); ATOMIC_INLINE unsigned atomic_sub_and_fetch_u(unsigned *p, unsigned x);
ATOMIC_INLINE unsigned atomic_cas_u(unsigned *v, unsigned old, unsigned _new); ATOMIC_INLINE unsigned atomic_cas_u(unsigned *v, unsigned old, unsigned _new);
/* WARNING! Float 'atomics' are really faked ones, those are actually closer to some kind of spinlock-sync'ed operation, /* WARNING! Float 'atomics' are really faked ones, those are actually closer to some kind of spinlock-sync'ed operation,
* which means they are only efficient if collisions are highly unlikely (i.e. if probability of two threads * which means they are only efficient if collisions are highly unlikely (i.e. if probability of two threads
* working on the same pointer at the same time is very low). */ * working on the same pointer at the same time is very low). */
ATOMIC_INLINE float atomic_add_fl(float *p, const float x); ATOMIC_INLINE float atomic_add_and_fetch_fl(float *p, const float x);
/******************************************************************************/ /******************************************************************************/
/* Include system-dependent implementations. */ /* Include system-dependent implementations. */

@ -56,25 +56,25 @@
/******************************************************************************/ /******************************************************************************/
/* size_t operations. */ /* size_t operations. */
ATOMIC_INLINE size_t atomic_add_z(size_t *p, size_t x) ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x)
{ {
assert(sizeof(size_t) == LG_SIZEOF_PTR); assert(sizeof(size_t) == LG_SIZEOF_PTR);
#if (LG_SIZEOF_PTR == 8) #if (LG_SIZEOF_PTR == 8)
return (size_t)atomic_add_uint64((uint64_t *)p, (uint64_t)x); return (size_t)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)x);
#elif (LG_SIZEOF_PTR == 4) #elif (LG_SIZEOF_PTR == 4)
return (size_t)atomic_add_uint32((uint32_t *)p, (uint32_t)x); return (size_t)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)x);
#endif #endif
} }
ATOMIC_INLINE size_t atomic_sub_z(size_t *p, size_t x) ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
{ {
assert(sizeof(size_t) == LG_SIZEOF_PTR); assert(sizeof(size_t) == LG_SIZEOF_PTR);
#if (LG_SIZEOF_PTR == 8) #if (LG_SIZEOF_PTR == 8)
return (size_t)atomic_add_uint64((uint64_t *)p, (uint64_t)-((int64_t)x)); return (size_t)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)-((int64_t)x));
#elif (LG_SIZEOF_PTR == 4) #elif (LG_SIZEOF_PTR == 4)
return (size_t)atomic_add_uint32((uint32_t *)p, (uint32_t)-((int32_t)x)); return (size_t)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)-((int32_t)x));
#endif #endif
} }
@ -91,25 +91,25 @@ ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new)
/******************************************************************************/ /******************************************************************************/
/* unsigned operations. */ /* unsigned operations. */
ATOMIC_INLINE unsigned atomic_add_u(unsigned *p, unsigned x) ATOMIC_INLINE unsigned atomic_add_and_fetch_u(unsigned *p, unsigned x)
{ {
assert(sizeof(unsigned) == LG_SIZEOF_INT); assert(sizeof(unsigned) == LG_SIZEOF_INT);
#if (LG_SIZEOF_INT == 8) #if (LG_SIZEOF_INT == 8)
return (unsigned)atomic_add_uint64((uint64_t *)p, (uint64_t)x); return (unsigned)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)x);
#elif (LG_SIZEOF_INT == 4) #elif (LG_SIZEOF_INT == 4)
return (unsigned)atomic_add_uint32((uint32_t *)p, (uint32_t)x); return (unsigned)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)x);
#endif #endif
} }
ATOMIC_INLINE unsigned atomic_sub_u(unsigned *p, unsigned x) ATOMIC_INLINE unsigned atomic_sub_and_fetch_u(unsigned *p, unsigned x)
{ {
assert(sizeof(unsigned) == LG_SIZEOF_INT); assert(sizeof(unsigned) == LG_SIZEOF_INT);
#if (LG_SIZEOF_INT == 8) #if (LG_SIZEOF_INT == 8)
return (unsigned)atomic_add_uint64((uint64_t *)p, (uint64_t)-((int64_t)x)); return (unsigned)atomic_add_and_fetch_uint64((uint64_t *)p, (uint64_t)-((int64_t)x));
#elif (LG_SIZEOF_INT == 4) #elif (LG_SIZEOF_INT == 4)
return (unsigned)atomic_add_uint32((uint32_t *)p, (uint32_t)-((int32_t)x)); return (unsigned)atomic_add_and_fetch_uint32((uint32_t *)p, (uint32_t)-((int32_t)x));
#endif #endif
} }
@ -127,7 +127,7 @@ ATOMIC_INLINE unsigned atomic_cas_u(unsigned *v, unsigned old, unsigned _new)
/******************************************************************************/ /******************************************************************************/
/* float operations. */ /* float operations. */
ATOMIC_INLINE float atomic_add_fl(float *p, const float x) ATOMIC_INLINE float atomic_add_and_fetch_fl(float *p, const float x)
{ {
assert(sizeof(float) == sizeof(uint32_t)); assert(sizeof(float) == sizeof(uint32_t));

@ -43,12 +43,12 @@
/******************************************************************************/ /******************************************************************************/
/* 64-bit operations. */ /* 64-bit operations. */
#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8) #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
ATOMIC_INLINE uint64_t atomic_add_uint64(uint64_t *p, uint64_t x) ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
{ {
return InterlockedExchangeAdd64((int64_t *)p, (int64_t)x) + x; return InterlockedExchangeAdd64((int64_t *)p, (int64_t)x) + x;
} }
ATOMIC_INLINE uint64_t atomic_sub_uint64(uint64_t *p, uint64_t x) ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x)
{ {
return InterlockedExchangeAdd64((int64_t *)p, -((int64_t)x)) - x; return InterlockedExchangeAdd64((int64_t *)p, -((int64_t)x)) - x;
} }
@ -61,12 +61,12 @@ ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _ne
/******************************************************************************/ /******************************************************************************/
/* 32-bit operations. */ /* 32-bit operations. */
ATOMIC_INLINE uint32_t atomic_add_uint32(uint32_t *p, uint32_t x) ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
{ {
return InterlockedExchangeAdd(p, x) + x; return InterlockedExchangeAdd(p, x) + x;
} }
ATOMIC_INLINE uint32_t atomic_sub_uint32(uint32_t *p, uint32_t x) ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
{ {
return InterlockedExchangeAdd(p, -((int32_t)x)) - x; return InterlockedExchangeAdd(p, -((int32_t)x)) - x;
} }

@ -58,12 +58,12 @@
/* 64-bit operations. */ /* 64-bit operations. */
#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8) #if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
# if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8)) # if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8))
ATOMIC_INLINE uint64_t atomic_add_uint64(uint64_t *p, uint64_t x) ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
{ {
return __sync_add_and_fetch(p, x); return __sync_add_and_fetch(p, x);
} }
ATOMIC_INLINE uint64_t atomic_sub_uint64(uint64_t *p, uint64_t x) ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x)
{ {
return __sync_sub_and_fetch(p, x); return __sync_sub_and_fetch(p, x);
} }
@ -73,7 +73,7 @@ ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _ne
return __sync_val_compare_and_swap(v, old, _new); return __sync_val_compare_and_swap(v, old, _new);
} }
# elif (defined(__amd64__) || defined(__x86_64__)) # elif (defined(__amd64__) || defined(__x86_64__))
ATOMIC_INLINE uint64_t atomic_add_uint64(uint64_t *p, uint64_t x) ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
{ {
asm volatile ( asm volatile (
"lock; xaddq %0, %1;" "lock; xaddq %0, %1;"
@ -83,7 +83,7 @@ ATOMIC_INLINE uint64_t atomic_add_uint64(uint64_t *p, uint64_t x)
return x; return x;
} }
ATOMIC_INLINE uint64_t atomic_sub_uint64(uint64_t *p, uint64_t x) ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x)
{ {
x = (uint64_t)(-(int64_t)x); x = (uint64_t)(-(int64_t)x);
asm volatile ( asm volatile (
@ -112,12 +112,12 @@ ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _ne
/******************************************************************************/ /******************************************************************************/
/* 32-bit operations. */ /* 32-bit operations. */
#if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4)) #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4))
ATOMIC_INLINE uint32_t atomic_add_uint32(uint32_t *p, uint32_t x) ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
{ {
return __sync_add_and_fetch(p, x); return __sync_add_and_fetch(p, x);
} }
ATOMIC_INLINE uint32_t atomic_sub_uint32(uint32_t *p, uint32_t x) ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
{ {
return __sync_sub_and_fetch(p, x); return __sync_sub_and_fetch(p, x);
} }
@ -127,7 +127,7 @@ ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _ne
return __sync_val_compare_and_swap(v, old, _new); return __sync_val_compare_and_swap(v, old, _new);
} }
#elif (defined(__i386__) || defined(__amd64__) || defined(__x86_64__)) #elif (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
ATOMIC_INLINE uint32_t atomic_add_uint32(uint32_t *p, uint32_t x) ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
{ {
uint32_t ret = x; uint32_t ret = x;
asm volatile ( asm volatile (
@ -138,7 +138,7 @@ ATOMIC_INLINE uint32_t atomic_add_uint32(uint32_t *p, uint32_t x)
return ret+x; return ret+x;
} }
ATOMIC_INLINE uint32_t atomic_sub_uint32(uint32_t *p, uint32_t x) ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
{ {
ret = (uint32_t)(-(int32_t)x); ret = (uint32_t)(-(int32_t)x);
asm volatile ( asm volatile (

@ -20,7 +20,7 @@ ccl_device_inline void kernel_write_pass_float(ccl_global float *buffer, int sam
{ {
ccl_global float *buf = buffer; ccl_global float *buf = buffer;
#if defined(__SPLIT_KERNEL__) && defined(__WORK_STEALING__) #if defined(__SPLIT_KERNEL__) && defined(__WORK_STEALING__)
atomic_add_float(buf, value); atomic_add_and_fetch_float(buf, value);
#else #else
*buf = (sample == 0)? value: *buf + value; *buf = (sample == 0)? value: *buf + value;
#endif // __SPLIT_KERNEL__ && __WORK_STEALING__ #endif // __SPLIT_KERNEL__ && __WORK_STEALING__
@ -33,9 +33,9 @@ ccl_device_inline void kernel_write_pass_float3(ccl_global float *buffer, int sa
ccl_global float *buf_y = buffer + 1; ccl_global float *buf_y = buffer + 1;
ccl_global float *buf_z = buffer + 2; ccl_global float *buf_z = buffer + 2;
atomic_add_float(buf_x, value.x); atomic_add_and_fetch_float(buf_x, value.x);
atomic_add_float(buf_y, value.y); atomic_add_and_fetch_float(buf_y, value.y);
atomic_add_float(buf_z, value.z); atomic_add_and_fetch_float(buf_z, value.z);
#else #else
ccl_global float3 *buf = (ccl_global float3*)buffer; ccl_global float3 *buf = (ccl_global float3*)buffer;
*buf = (sample == 0)? value: *buf + value; *buf = (sample == 0)? value: *buf + value;
@ -50,10 +50,10 @@ ccl_device_inline void kernel_write_pass_float4(ccl_global float *buffer, int sa
ccl_global float *buf_z = buffer + 2; ccl_global float *buf_z = buffer + 2;
ccl_global float *buf_w = buffer + 3; ccl_global float *buf_w = buffer + 3;
atomic_add_float(buf_x, value.x); atomic_add_and_fetch_float(buf_x, value.x);
atomic_add_float(buf_y, value.y); atomic_add_and_fetch_float(buf_y, value.y);
atomic_add_float(buf_z, value.z); atomic_add_and_fetch_float(buf_z, value.z);
atomic_add_float(buf_w, value.w); atomic_add_and_fetch_float(buf_w, value.w);
#else #else
ccl_global float4 *buf = (ccl_global float4*)buffer; ccl_global float4 *buf = (ccl_global float4*)buffer;
*buf = (sample == 0)? value: *buf + value; *buf = (sample == 0)? value: *buf + value;

@ -39,7 +39,7 @@ ATOMIC_INLINE void atomic_update_max_z(size_t *maximum_value, size_t value)
/* Float atomics implementation credits: /* Float atomics implementation credits:
* http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html * http://suhorukov.blogspot.in/2011/12/opencl-11-atomic-operations-on-floating.html
*/ */
ccl_device_inline void atomic_add_float(volatile ccl_global float *source, ccl_device_inline void atomic_add_and_fetch_float(volatile ccl_global float *source,
const float operand) const float operand)
{ {
union { union {

@ -29,13 +29,13 @@ public:
explicit Stats(static_init_t) {} explicit Stats(static_init_t) {}
void mem_alloc(size_t size) { void mem_alloc(size_t size) {
atomic_add_z(&mem_used, size); atomic_add_and_fetch_z(&mem_used, size);
atomic_update_max_z(&mem_peak, mem_used); atomic_update_max_z(&mem_peak, mem_used);
} }
void mem_free(size_t size) { void mem_free(size_t size) {
assert(mem_used >= size); assert(mem_used >= size);
atomic_sub_z(&mem_used, size); atomic_sub_and_fetch_z(&mem_used, size);
} }
size_t mem_used; size_t mem_used;

@ -505,8 +505,8 @@ static void make_memhead_header(MemHead *memh, size_t len, const char *str)
memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + len); memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + len);
memt->tag3 = MEMTAG3; memt->tag3 = MEMTAG3;
atomic_add_u(&totblock, 1); atomic_add_and_fetch_u(&totblock, 1);
atomic_add_z(&mem_in_use, len); atomic_add_and_fetch_z(&mem_in_use, len);
mem_lock_thread(); mem_lock_thread();
addtail(membase, &memh->next); addtail(membase, &memh->next);
@ -638,7 +638,7 @@ void *MEM_guarded_mapallocN(size_t len, const char *str)
if (memh != (MemHead *)-1) { if (memh != (MemHead *)-1) {
make_memhead_header(memh, len, str); make_memhead_header(memh, len, str);
memh->mmap = 1; memh->mmap = 1;
atomic_add_z(&mmap_in_use, len); atomic_add_and_fetch_z(&mmap_in_use, len);
mem_lock_thread(); mem_lock_thread();
peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem; peak_mem = mmap_in_use > peak_mem ? mmap_in_use : peak_mem;
mem_unlock_thread(); mem_unlock_thread();
@ -1007,8 +1007,8 @@ static void rem_memblock(MemHead *memh)
} }
mem_unlock_thread(); mem_unlock_thread();
atomic_sub_u(&totblock, 1); atomic_sub_and_fetch_u(&totblock, 1);
atomic_sub_z(&mem_in_use, memh->len); atomic_sub_and_fetch_z(&mem_in_use, memh->len);
#ifdef DEBUG_MEMDUPLINAME #ifdef DEBUG_MEMDUPLINAME
if (memh->need_free_name) if (memh->need_free_name)
@ -1016,7 +1016,7 @@ static void rem_memblock(MemHead *memh)
#endif #endif
if (memh->mmap) { if (memh->mmap) {
atomic_sub_z(&mmap_in_use, memh->len); atomic_sub_and_fetch_z(&mmap_in_use, memh->len);
#if defined(WIN32) #if defined(WIN32)
/* our windows mmap implementation is not thread safe */ /* our windows mmap implementation is not thread safe */
mem_lock_thread(); mem_lock_thread();

@ -142,11 +142,11 @@ void MEM_lockfree_freeN(void *vmemh)
return; return;
} }
atomic_sub_u(&totblock, 1); atomic_sub_and_fetch_u(&totblock, 1);
atomic_sub_z(&mem_in_use, len); atomic_sub_and_fetch_z(&mem_in_use, len);
if (MEMHEAD_IS_MMAP(memh)) { if (MEMHEAD_IS_MMAP(memh)) {
atomic_sub_z(&mmap_in_use, len); atomic_sub_and_fetch_z(&mmap_in_use, len);
#if defined(WIN32) #if defined(WIN32)
/* our windows mmap implementation is not thread safe */ /* our windows mmap implementation is not thread safe */
mem_lock_thread(); mem_lock_thread();
@ -287,8 +287,8 @@ void *MEM_lockfree_callocN(size_t len, const char *str)
if (LIKELY(memh)) { if (LIKELY(memh)) {
memh->len = len; memh->len = len;
atomic_add_u(&totblock, 1); atomic_add_and_fetch_u(&totblock, 1);
atomic_add_z(&mem_in_use, len); atomic_add_and_fetch_z(&mem_in_use, len);
update_maximum(&peak_mem, mem_in_use); update_maximum(&peak_mem, mem_in_use);
return PTR_FROM_MEMHEAD(memh); return PTR_FROM_MEMHEAD(memh);
@ -312,8 +312,8 @@ void *MEM_lockfree_mallocN(size_t len, const char *str)
} }
memh->len = len; memh->len = len;
atomic_add_u(&totblock, 1); atomic_add_and_fetch_u(&totblock, 1);
atomic_add_z(&mem_in_use, len); atomic_add_and_fetch_z(&mem_in_use, len);
update_maximum(&peak_mem, mem_in_use); update_maximum(&peak_mem, mem_in_use);
return PTR_FROM_MEMHEAD(memh); return PTR_FROM_MEMHEAD(memh);
@ -361,8 +361,8 @@ void *MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str
memh->len = len | (size_t) MEMHEAD_ALIGN_FLAG; memh->len = len | (size_t) MEMHEAD_ALIGN_FLAG;
memh->alignment = (short) alignment; memh->alignment = (short) alignment;
atomic_add_u(&totblock, 1); atomic_add_and_fetch_u(&totblock, 1);
atomic_add_z(&mem_in_use, len); atomic_add_and_fetch_z(&mem_in_use, len);
update_maximum(&peak_mem, mem_in_use); update_maximum(&peak_mem, mem_in_use);
return PTR_FROM_MEMHEAD(memh); return PTR_FROM_MEMHEAD(memh);
@ -396,9 +396,9 @@ void *MEM_lockfree_mapallocN(size_t len, const char *str)
if (memh != (MemHead *)-1) { if (memh != (MemHead *)-1) {
memh->len = len | (size_t) MEMHEAD_MMAP_FLAG; memh->len = len | (size_t) MEMHEAD_MMAP_FLAG;
atomic_add_u(&totblock, 1); atomic_add_and_fetch_u(&totblock, 1);
atomic_add_z(&mem_in_use, len); atomic_add_and_fetch_z(&mem_in_use, len);
atomic_add_z(&mmap_in_use, len); atomic_add_and_fetch_z(&mmap_in_use, len);
update_maximum(&peak_mem, mem_in_use); update_maximum(&peak_mem, mem_in_use);
update_maximum(&peak_mem, mmap_in_use); update_maximum(&peak_mem, mmap_in_use);

@ -3284,7 +3284,7 @@ void DAG_threaded_update_handle_node_updated(void *node_v,
for (itA = node->child; itA; itA = itA->next) { for (itA = node->child; itA; itA = itA->next) {
DagNode *child_node = itA->node; DagNode *child_node = itA->node;
if (child_node != node) { if (child_node != node) {
atomic_sub_uint32(&child_node->num_pending_parents, 1); atomic_sub_and_fetch_uint32(&child_node->num_pending_parents, 1);
if (child_node->num_pending_parents == 0) { if (child_node->num_pending_parents == 0) {
bool need_schedule; bool need_schedule;

@ -2264,7 +2264,7 @@ static void dynamic_paint_create_uv_surface_neighbor_cb(void *userdata, const in
* to non--1 *before* its tri_index is set (i.e. that it cannot be used a neighbour). * to non--1 *before* its tri_index is set (i.e. that it cannot be used a neighbour).
*/ */
tPoint->neighbour_pixel = ind - 1; tPoint->neighbour_pixel = ind - 1;
atomic_add_uint32(&tPoint->neighbour_pixel, 1); atomic_add_and_fetch_uint32(&tPoint->neighbour_pixel, 1);
tPoint->tri_index = i; tPoint->tri_index = i;
/* Now calculate pixel data for this pixel as it was on polygon surface */ /* Now calculate pixel data for this pixel as it was on polygon surface */
@ -2289,7 +2289,7 @@ static void dynamic_paint_create_uv_surface_neighbor_cb(void *userdata, const in
/* Increase the final number of active surface points if relevant. */ /* Increase the final number of active surface points if relevant. */
if (tPoint->tri_index != -1) if (tPoint->tri_index != -1)
atomic_add_uint32(active_points, 1); atomic_add_and_fetch_uint32(active_points, 1);
} }
} }

@ -238,7 +238,7 @@ static void mesh_calc_normals_poly_accum_task_cb(void *userdata, const int pidx)
/* accumulate */ /* accumulate */
for (int k = 3; k--; ) { for (int k = 3; k--; ) {
atomic_add_fl(&vnors[ml[i].v][k], pnor[k] * fac); atomic_add_and_fetch_fl(&vnors[ml[i].v][k], pnor[k] * fac);
} }
prev_edge = cur_edge; prev_edge = cur_edge;
} }

@ -977,7 +977,7 @@ static void pbvh_update_normals_accum_task_cb(void *userdata, const int n)
* Not exact equivalent though, since atomicity is only ensured for one component * Not exact equivalent though, since atomicity is only ensured for one component
* of the vector at a time, but here it shall not make any sensible difference. */ * of the vector at a time, but here it shall not make any sensible difference. */
for (int k = 3; k--; ) { for (int k = 3; k--; ) {
atomic_add_fl(&vnors[v][k], fn[k]); atomic_add_and_fetch_fl(&vnors[v][k], fn[k]);
} }
} }
} }

@ -237,7 +237,7 @@ static void task_pool_num_decrease(TaskPool *pool, size_t done)
BLI_assert(pool->num >= done); BLI_assert(pool->num >= done);
pool->num -= done; pool->num -= done;
atomic_sub_z(&pool->currently_running_tasks, done); atomic_sub_and_fetch_z(&pool->currently_running_tasks, done);
pool->done += done; pool->done += done;
if (pool->num == 0) if (pool->num == 0)
@ -292,7 +292,7 @@ static bool task_scheduler_thread_wait_pop(TaskScheduler *scheduler, Task **task
continue; continue;
} }
if (atomic_add_z(&pool->currently_running_tasks, 1) <= pool->num_threads || if (atomic_add_and_fetch_z(&pool->currently_running_tasks, 1) <= pool->num_threads ||
pool->num_threads == 0) pool->num_threads == 0)
{ {
*task = current_task; *task = current_task;
@ -301,7 +301,7 @@ static bool task_scheduler_thread_wait_pop(TaskScheduler *scheduler, Task **task
break; break;
} }
else { else {
atomic_sub_z(&pool->currently_running_tasks, 1); atomic_sub_and_fetch_z(&pool->currently_running_tasks, 1);
} }
} }
if (!found_task) if (!found_task)
@ -669,7 +669,7 @@ void BLI_task_pool_work_and_wait(TaskPool *pool)
/* if found task, do it, otherwise wait until other tasks are done */ /* if found task, do it, otherwise wait until other tasks are done */
if (found_task) { if (found_task) {
/* run task */ /* run task */
atomic_add_z(&pool->currently_running_tasks, 1); atomic_add_and_fetch_z(&pool->currently_running_tasks, 1);
work_task->run(pool, work_task->taskdata, 0); work_task->run(pool, work_task->taskdata, 0);
/* delete task */ /* delete task */

@ -383,7 +383,7 @@ void ExecutionGroup::finalizeChunkExecution(int chunkNumber, MemoryBuffer **memo
if (this->m_chunkExecutionStates[chunkNumber] == COM_ES_SCHEDULED) if (this->m_chunkExecutionStates[chunkNumber] == COM_ES_SCHEDULED)
this->m_chunkExecutionStates[chunkNumber] = COM_ES_EXECUTED; this->m_chunkExecutionStates[chunkNumber] = COM_ES_EXECUTED;
atomic_add_u(&this->m_chunksFinished, 1); atomic_add_and_fetch_u(&this->m_chunksFinished, 1);
if (memoryBuffers) { if (memoryBuffers) {
for (unsigned int index = 0; index < this->m_cachedMaxReadBufferOffset; index++) { for (unsigned int index = 0; index < this->m_cachedMaxReadBufferOffset; index++) {
MemoryBuffer *buffer = memoryBuffers[index]; MemoryBuffer *buffer = memoryBuffers[index];

@ -152,7 +152,7 @@ static void deg_task_run_func(TaskPool *pool,
} }
if ((rel->flag & DEPSREL_FLAG_CYCLIC) == 0) { if ((rel->flag & DEPSREL_FLAG_CYCLIC) == 0) {
BLI_assert(child->num_links_pending > 0); BLI_assert(child->num_links_pending > 0);
atomic_sub_uint32(&child->num_links_pending, 1); atomic_sub_and_fetch_uint32(&child->num_links_pending, 1);
} }
if (child->num_links_pending == 0) { if (child->num_links_pending == 0) {
bool is_scheduled = atomic_fetch_and_or_uint8( bool is_scheduled = atomic_fetch_and_or_uint8(
@ -287,7 +287,7 @@ static void schedule_node(TaskPool *pool, Depsgraph *graph, unsigned int layers,
{ {
if (dec_parents) { if (dec_parents) {
BLI_assert(node->num_links_pending > 0); BLI_assert(node->num_links_pending > 0);
atomic_sub_uint32(&node->num_links_pending, 1); atomic_sub_and_fetch_uint32(&node->num_links_pending, 1);
} }
if (node->num_links_pending == 0) { if (node->num_links_pending == 0) {

@ -2510,7 +2510,7 @@ static void filelist_readjob_do(
* Using an atomic operation to avoid having to lock thread... * Using an atomic operation to avoid having to lock thread...
* Note that we do not really need this here currently, since there is a single listing thread, but better * Note that we do not really need this here currently, since there is a single listing thread, but better
* remain consistent about threading! */ * remain consistent about threading! */
*((uint32_t *)entry->uuid) = atomic_add_uint32((uint32_t *)filelist->filelist_intern.curr_uuid, 1); *((uint32_t *)entry->uuid) = atomic_add_and_fetch_uint32((uint32_t *)filelist->filelist_intern.curr_uuid, 1);
/* Only thing we change in direntry here, so we need to free it first. */ /* Only thing we change in direntry here, so we need to free it first. */
MEM_freeN(entry->relpath); MEM_freeN(entry->relpath);

@ -544,12 +544,12 @@ HRESULT STDMETHODCALLTYPE PinnedMemoryAllocator::QueryInterface(REFIID /*iid*/,
ULONG STDMETHODCALLTYPE PinnedMemoryAllocator::AddRef(void) ULONG STDMETHODCALLTYPE PinnedMemoryAllocator::AddRef(void)
{ {
return atomic_add_uint32(&mRefCount, 1U); return atomic_add_and_fetch_uint32(&mRefCount, 1U);
} }
ULONG STDMETHODCALLTYPE PinnedMemoryAllocator::Release(void) ULONG STDMETHODCALLTYPE PinnedMemoryAllocator::Release(void)
{ {
uint32_t newCount = atomic_sub_uint32(&mRefCount, 1U); uint32_t newCount = atomic_sub_and_fetch_uint32(&mRefCount, 1U);
if (newCount == 0) if (newCount == 0)
delete this; delete this;
return (ULONG)newCount; return (ULONG)newCount;