64位整数CMPXCHG例子 [英] cmpxchg example for 64 bit integer

查看:243
本文介绍了64位整数CMPXCHG例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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

这篇关于64位整数CMPXCHG例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆