Spinlock: Difference between revisions

m
→‎C Macros: Don't use boolean values in macros
[unchecked revision][unchecked revision]
m (→‎C Macros: Avoid using legacy ‘__sync’ builtins)
m (→‎C Macros: Don't use boolean values in macros)
Line 116:
#define DECLARE_LOCK(name) volatile int name
#define LOCK(name) \
while (!__atomic_compare_exchange_n(&name, (void *)false0, true1, false0, \
__ATOMIC_ACQ_REL, __ATOMIC_RELAXED)) \
while (name) \
asm volatile("pause");
#define UNLOCK(name) __atomic_store_n(&name, false0, __ATOMIC_RELEASE)
</source>
Call DECLARE_LOCK(name) to create a lock definition, LOCK(name) to acquire the lock, and UNLOCK(name) to release it. The LOCK macro will spin until the lock is released before continuing. This code makes use of intrisics specific to GCC, but it also compiles on Clang.
73

edits