static __inline int compare_and_swap(volatile size_t* x,size_t oldval,size_t newval) {
#ifndef WIN32
char result;
asm volatile(“lock; cmpxchgl %3, %0; setz %1” : “=m”(*x), “=q”(result) : “m” (*x), “r” (newval), “a” (oldval) : “memory”);
return result;
#else
#endif
}
#define atomic_cas(dst, old, new) compare_and_swap((dst), (old), (new))
//#define atomic_cas(dst, old, new) __sync_bool_compare_and_swap((dst), (old), (new))
#define atomic_lock(ptr) while(!atomic_cas(ptr,0,1))
#define atomic_unlock(ptr) while(!atomic_cas(ptr,1,0))
自旋锁实现
最新推荐文章于 2024-06-17 21:32:10 发布