sniperHW/chuck

__sync_bool_compare_and_swap

Closed this issue · 4 comments

define COMPARE_AND_SWAP(PTR,OLD,NEW) \

({int __result;                                                     \
    do __result = __sync_val_compare_and_swap(PTR,OLD,NEW) == OLD?1:0; \
    while(0);                                                       \
    __result;})

可以直接修改为

define COMPARE_AND_SWAP(PTR,OLD,NEW) \

__sync_bool_compare_and_swap((PTR),(OLD),(NEW))

bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)
type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
These builtins perform an atomic compare and swap. That is, if the current value of *ptr is oldval, then write newval into *ptr.

The “bool” version returns true if the comparison is successful and newval was written. The “val” version returns the contents of *ptr before the operation.

只用bool的话,使用太受限制了

宏写出来和bool效果是一样的,对swap的返回值进行比较返回1或0,看起来是和bool的行为一样。
你是还要进行什么扩展吗》

不,是一直以来我看了函数名一直以为参数需要是bool类型的