为什么fetch_add使用锁前缀,而fetch_and在boost atomics中使用cmpxchg

why fetch_add uses lock prefix while fetch_and uses cmpxchg in boost atomics

本文关键字:fetch boost atomics and cmpxchg 前缀 add 为什么      更新时间:2023-10-16

我注意到,在fetch_add的boost::atomics库x86实现(其中一个不使用编译器内部函数)中,使用了前缀为lockadd指令:

static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
{
__asm__ __volatile__
(
"lock; xaddw %0, %1"
: "+q" (v), "+m" (storage)
:
: BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
);
return v;
}

fetch_andfetch_orfetch_xor是通过CAS指令实现的:

#define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)
__asm__ __volatile__
(
"xor %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER ", %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "nt"
".align 16nt"
"1: movw %[arg], %%dxnt"
op " %%ax, %%dxnt"
"lock; cmpxchgw %%dx, %[storage]nt"
"jne 1b"
: [res] "+a" (result), [storage] "+m" (storage)
: [arg] "q" (argument)
: BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER, "memory"
)
static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
{
storage_type res = storage;
BOOST_ATOMIC_DETAIL_CAS_LOOP("andw", v, res);
return res;
}

你知道为什么会这样吗?这是因为您不能在逐位操作中使用锁前缀吗(据我所知,这不是真的)?

正如Jester所指出的,fetch_add使用xadd指令返回函数效果之前的值。没有xandxor指令,因此使用CAS。