TAS & Clear
TAS对一个bit位做test_and_set操作。
- x86平台
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
static inline bool pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr) { register char _res = 1; __asm__ __volatile__( " lock \n" " xchgb %0,%1 \n" : "+q"(_res), "+m"(ptr->value) : : "memory"); return _res == 0; } static inline void pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr) { /* * On a TSO architecture like x86 it's sufficient to use a compiler * barrier to achieve release semantics. */ __asm__ __volatile__("" ::: "memory"); ptr->value = 0; }
- AArch64平台
1 2 3 4 5 6 7 8 9 10
static inline bool pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr) { return __sync_lock_test_and_set(&ptr->value, 1) == 0; } static inline void pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr) { __sync_lock_release(&ptr->value); }
父主题: 原子操作