diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/vppinfra/atomics.h | 3 | ||||
-rw-r--r-- | src/vppinfra/lock.h | 12 |
2 files changed, 13 insertions, 2 deletions
diff --git a/src/vppinfra/atomics.h b/src/vppinfra/atomics.h index df8e534b683..ce2177df5c2 100644 --- a/src/vppinfra/atomics.h +++ b/src/vppinfra/atomics.h @@ -37,11 +37,14 @@ #define clib_atomic_cmp_and_swap(addr,old,new) __sync_val_compare_and_swap(addr, old, new) #define clib_atomic_bool_cmp_and_swap(addr,old,new) __sync_bool_compare_and_swap(addr, old, new) +#define clib_atomic_cmp_and_swap_acq_relax_n(addr,exp,new,weak) __atomic_compare_exchange_n ((addr), (exp), (new), (weak), __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) + #define clib_atomic_test_and_set(a) __atomic_exchange_n(a, 1, __ATOMIC_ACQUIRE) #define clib_atomic_release(a) __atomic_store_n(a, 0, __ATOMIC_RELEASE) #define clib_atomic_fence_rel() __atomic_thread_fence(__ATOMIC_RELEASE); +#define clib_atomic_load_relax_n(a) __atomic_load_n((a), __ATOMIC_RELAXED) #define clib_atomic_load_acq_n(a) __atomic_load_n((a), __ATOMIC_ACQUIRE) #define clib_atomic_store_rel_n(a, b) __atomic_store_n ((a), (b), __ATOMIC_RELEASE) diff --git a/src/vppinfra/lock.h b/src/vppinfra/lock.h index cc6a7f086b1..c1f0b87dfc6 100644 --- a/src/vppinfra/lock.h +++ b/src/vppinfra/lock.h @@ -17,6 +17,7 @@ #define included_clib_lock_h #include <vppinfra/clib.h> +#include <vppinfra/atomics.h> #if __x86_64__ #define CLIB_PAUSE() __builtin_ia32_pause () @@ -76,8 +77,15 @@ clib_spinlock_free (clib_spinlock_t * p) static_always_inline void clib_spinlock_lock (clib_spinlock_t * p) { - while (clib_atomic_test_and_set (&(*p)->lock)) - CLIB_PAUSE (); + u32 free = 0; + while (!clib_atomic_cmp_and_swap_acq_relax_n (&(*p)->lock, &free, 1, 0)) + { + /* atomic load limits number of compare_exchange executions */ + while (clib_atomic_load_relax_n (&(*p)->lock)) + CLIB_PAUSE (); + /* on failure, compare_exchange writes (*p)->lock into free */ + free = 0; + } CLIB_LOCK_DBG (p); } |