Fix implementation of atomic update max and move to a central location
authorMai Lavelle <mai.lavelle@gmail.com>
Wed, 23 Aug 2017 04:40:04 +0000 (00:40 -0400)
committerMai Lavelle <mai.lavelle@gmail.com>
Wed, 23 Aug 2017 10:54:25 +0000 (06:54 -0400)
While unlikely to have had any serious effects because of limited use, the
previous implementation was not actually atomic due to a data race and
incorrectly coded CAS loop. We also had duplicates of this code in a few
places, it's now been moved to a single location with all other atomic
operations.

intern/atomic/atomic_ops.h
intern/atomic/intern/atomic_ops_ext.h
intern/cycles/util/util_atomic.h
intern/cycles/util/util_stats.h
intern/guardedalloc/intern/mallocn_lockfree_impl.c

index 1e9528f9ed9eec093f5bbe07e80617ed22ec1ca6..72813c39ac20c672f19c78cc419a969859b9ea8a 100644 (file)
@@ -100,6 +100,7 @@ ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x);
 ATOMIC_INLINE size_t atomic_fetch_and_add_z(size_t *p, size_t x);
 ATOMIC_INLINE size_t atomic_fetch_and_sub_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_fetch_and_update_max_z(size_t *p, size_t x); /* Uses CAS loop, see warning below. */
 
 ATOMIC_INLINE unsigned int atomic_add_and_fetch_u(unsigned int *p, unsigned int x);
 ATOMIC_INLINE unsigned int atomic_sub_and_fetch_u(unsigned int *p, unsigned int x);
index b72c94563fc5c69e5e2b5b08fcce8a3844318637..8d5f2e5dad7ca7240775674ac0a307e522453fcd 100644 (file)
@@ -111,6 +111,17 @@ ATOMIC_INLINE size_t atomic_cas_z(size_t *v, size_t old, size_t _new)
 #endif
 }
 
+ATOMIC_INLINE size_t atomic_fetch_and_update_max_z(size_t *p, size_t x)
+{
+       size_t prev_value;
+       while((prev_value = *p) < x) {
+               if(atomic_cas_z(p, prev_value, x) == prev_value) {
+                       break;
+               }
+       }
+       return prev_value;
+}
+
 /******************************************************************************/
 /* unsigned operations. */
 ATOMIC_INLINE unsigned int atomic_add_and_fetch_u(unsigned int *p, unsigned int x)
index 643af87a65f3c56e07ddc01f0b9fc0041d39a213..f3c7ae546a0ace4aec938eb394da306255b86fdd 100644 (file)
 /* Using atomic ops header from Blender. */
 #include "atomic_ops.h"
 
-ATOMIC_INLINE void atomic_update_max_z(size_t *maximum_value, size_t value)
-{
-       size_t prev_value = *maximum_value;
-       while(prev_value < value) {
-               if(atomic_cas_z(maximum_value, prev_value, value) != prev_value) {
-                       break;
-               }
-       }
-}
-
 #define atomic_add_and_fetch_float(p, x) atomic_add_and_fetch_fl((p), (x))
 
 #define atomic_fetch_and_inc_uint32(p) atomic_fetch_and_add_uint32((p), 1)
index baba549753d03dd076e14f05be771c8e45c94966..7667f58eb7d3da070da62454d4604b2d4a409b57 100644 (file)
@@ -30,7 +30,7 @@ public:
 
        void mem_alloc(size_t size) {
                atomic_add_and_fetch_z(&mem_used, size);
-               atomic_update_max_z(&mem_peak, mem_used);
+               atomic_fetch_and_update_max_z(&mem_peak, mem_used);
        }
 
        void mem_free(size_t size) {
index b4838cdca182d12b8ac5452438697e62b1fb3423..66573b91acec813297bb65f4650727dc416b74fb 100644 (file)
@@ -76,12 +76,7 @@ enum {
 MEM_INLINE void update_maximum(size_t *maximum_value, size_t value)
 {
 #ifdef USE_ATOMIC_MAX
-       size_t prev_value = *maximum_value;
-       while (prev_value < value) {
-               if (atomic_cas_z(maximum_value, prev_value, value) != prev_value) {
-                       break;
-               }
-       }
+       atomic_fetch_and_update_max_z(maximum_value, value);
 #else
        *maximum_value = value > *maximum_value ? value : *maximum_value;
 #endif