OSCompareAndSwap 是否能够像 CMPXCHG8B 一样不受 ABA 问题的影响?

发布于 2024-08-26 00:05:15 字数 53 浏览 2 评论 0原文

OSCompareAndSwap 是否像 CMPXCHG8B 一样不受 ABA 问题的影响?

Is OSCompareAndSwap is immune to ABA problem like CMPXCHG8B?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-09-02 00:05:15

这一切都取决于实施。 OSCompareAndSwap* 只是一个保证原子 CAS 运算符的接口(如果 CPU 支持它)。

对于 x86 此函数64 位的实现是这样

_OSCompareAndSwap64:
    pushl       %edi
    pushl       %ebx

    movl         4+8(%esp), %eax    #; low 32-bits of oldValue
    movl         8+8(%esp), %edx    #; high 32-bits of oldValue
    movl        12+8(%esp), %ebx    #; low 32-bits of newValue
    movl        16+8(%esp), %ecx    #; high 32-bits of newValue
    movl        20+8(%esp), %edi    #; ptr
    lock
    cmpxchg8b   0(%edi)     #; CAS (eax:edx, ebx:ecx implicit)
    sete        %al         #; did CAS succeed? (TZ=1)
    movzbl      %al, %eax       #; clear out the high bytes

    popl        %ebx
    popl        %edi
    ret

的,所以你的答案可能是“是”。

It all depends on the implementation. OSCompareAndSwap* is only an interface which guarantee an atomic CAS operator (if the CPU supports it).

For x86 this function for 64-bit is implemented as

_OSCompareAndSwap64:
    pushl       %edi
    pushl       %ebx

    movl         4+8(%esp), %eax    #; low 32-bits of oldValue
    movl         8+8(%esp), %edx    #; high 32-bits of oldValue
    movl        12+8(%esp), %ebx    #; low 32-bits of newValue
    movl        16+8(%esp), %ecx    #; high 32-bits of newValue
    movl        20+8(%esp), %edi    #; ptr
    lock
    cmpxchg8b   0(%edi)     #; CAS (eax:edx, ebx:ecx implicit)
    sete        %al         #; did CAS succeed? (TZ=1)
    movzbl      %al, %eax       #; clear out the high bytes

    popl        %ebx
    popl        %edi
    ret

so the answer for you probably is "yes".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文