Skip to content

Instantly share code, notes, and snippets.

@leiless
Last active October 30, 2018 09:38
Show Gist options
  • Save leiless/1900aca5f8f85cff1c8e74974cf390cc to your computer and use it in GitHub Desktop.
Save leiless/1900aca5f8f85cff1c8e74974cf390cc to your computer and use it in GitHub Desktop.
/**
* Strong version of atomic compare-and-swap
* @p pointer to cas with
* @o old value
* @n new value
* @return true if success false o.w.
* see: http://donghao.org/2015/01/30/128bit-atomic-operation-in-arm64
*
* NOTE: GCC-compatible available only
*/
#define atomic_cas(p, o, n) ({ \
__typeof(*(p)) t = (o); \
__atomic_compare_exchange_n(p, &t, n, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
})
static int __k = 0;
#define spin_lock() while (!atomic_cas(&__k, 0, 1)) continue
#define spin_unlock() { \
int ok = atomic_cas(&__k, 1, 0); \
assert(ok); \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment