我使用CMPXCHG(比较和交换)在i686的架构,32位比较和交换如下:
静态INT CAS(INT * PTR,诠释OLDVAL,诠释的newval){ unsigned char型RET; __asm__ __volatile__( 锁\\ n cmpxchgl%2%1 \\ n SETE%0 \\ n := Q(RET),= M(* PTR) :R(的newval),M(* PTR),一(OLDVAL) :内存); //上述装配失败的情况下返回0 如果(RET)返回0;}什么是相当于x86_64体系为64位的比较和交换
静态INT CAS(长* PTR,OLDVAL长,长的newval){ unsigned char型RET; //? 如果(RET)返回0;}解决方案
x86_64的有 cmpxchgq (Q为四字)指令为8字节(64位)比较和交换
I am using cmpxchg (compare-and-exchange) in i686 architecture for 32 bit compare and swap as follows.
static int CAS(int *ptr, int oldVal, int newVal) { unsigned char ret; __asm__ __volatile__ ( " lock\n" " cmpxchgl %2,%1\n" " sete %0\n" : "=q" (ret), "=m" (*ptr) : "r" (newVal), "m" (*ptr), "a" (oldVal) : "memory"); //above assembly returns 0 in case of failure if (ret) return 0; }What is the equivalent for x86_64 architecture for 64 bit compare and swap
static int CAS(long *ptr, long oldVal, long newVal) { unsigned char ret; // ? if (ret) return 0; }解决方案
x86_64 has the cmpxchgq ("q" for quadword) instruction for 8-byte (64 bit) compare and swap