sniperHW / chuck

high performance and easily use asynchronous network library for C/Lua
52 stars 32 forks source link

__sync_bool_compare_and_swap #3

Closed zhuoyikang closed 9 years ago

zhuoyikang commented 9 years ago

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))
sniperHW commented 9 years ago

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.
sniperHW commented 9 years ago

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

zhuoyikang commented 9 years ago

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

sniperHW commented 9 years ago

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