编译报错:{standard input}: Assembler messages:
{standard input}:1222: Error: unknown mnemonic 'xchgl' -- 'xchgl x1,[x19,112]'。
xchgl是x86上的汇编指令,作用是交换寄存器/内存变量和寄存器的值,如果交换的两个变量中有内存变量,会对内存变量增加原子锁操作。鲲鹏上可用GCC的原子操作接口__atomic_exchange_n替换。__atomic_exchange_n的第三个入参是内存屏障类型,使用者可以根据自身代码逻辑选择不同的屏障。对多线程访问临界区的逻辑不清晰时,建议使用__ATOMIC_SEQ_CST屏障,避免由屏障使用不当带来一致性问题。
x86实现样例:
inline int nBasicAtomicInt::fetchAndStoreOrdered(int newValue) { /* 原子操作,把_value的值和newValue交换,且返回_value原来的值 */ asm volatile("xchgl %0,%1" : "=r" (newValue), "+m" (m_value) : "0" (newValue) : "memory"); return newValue; }
鲲鹏上可替换成:
inline int nBasicAtomicInt::fetchAndStoreOrdered(int newValue) { /* 原子操作,把_value的值和newValue * 交换,且返回_value原来的值 */ return __atomic_exchange_n(&_q_value, newValue, __ATOMIC_SEQ_CST); }