diff options
author | Mai Lavelle <mai.lavelle@gmail.com> | 2017-08-23 07:40:04 +0300 |
---|---|---|
committer | Mai Lavelle <mai.lavelle@gmail.com> | 2017-08-23 13:54:25 +0300 |
commit | 2540741dee789f752687198c6f272a995d45073e (patch) | |
tree | 4d03f3ce032f67fdb3704b461bc51c3d02b6e7e1 /intern/atomic | |
parent | 5c60721c9e3d5af2bd4f80b3013fe0c804cf7531 (diff) |
Fix implementation of atomic update max and move to a central location
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.
Diffstat (limited to 'intern/atomic')
-rw-r--r-- | intern/atomic/atomic_ops.h | 1 | ||||
-rw-r--r-- | intern/atomic/intern/atomic_ops_ext.h | 11 |
2 files changed, 12 insertions, 0 deletions
diff --git a/intern/atomic/atomic_ops.h b/intern/atomic/atomic_ops.h index 1e9528f9ed9..72813c39ac2 100644 --- a/intern/atomic/atomic_ops.h +++ b/intern/atomic/atomic_ops.h @@ -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); diff --git a/intern/atomic/intern/atomic_ops_ext.h b/intern/atomic/intern/atomic_ops_ext.h index b72c94563fc..8d5f2e5dad7 100644 --- a/intern/atomic/intern/atomic_ops_ext.h +++ b/intern/atomic/intern/atomic_ops_ext.h @@ -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) |