ARM64:LDXR/STXR 与 LDAXR/STLXR [英] ARM64: LDXR/STXR vs LDAXR/STLXR

查看:51
本文介绍了ARM64:LDXR/STXR 与 LDAXR/STLXR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 上,有两个类似的函数,OSAtomicAdd32OSAtomicAdd32Barrier.我想知道您什么时候需要 Barrier 变体.

On iOS, there are two similar functions, OSAtomicAdd32 and OSAtomicAdd32Barrier. I'm wondering when you would need the Barrier variant.

拆解后,它们是:

_OSAtomicAdd32:
ldxr    w8, [x1]
add     w8, w8, w0
stxr    w9, w8, [x1]
cbnz    w9, _OSAtomicAdd32
mov     x0, x8
ret     lr

_OSAtomicAdd32Barrier:
ldaxr   w8, [x1]
add     w8, w8, w0
stlxr   w9, w8, [x1]
cbnz    w9, _OSAtomicAdd32Barrier
mov     x0, x8
ret     lr

在哪些情况下您需要后者的 Load-Acquire/Store-Release 语义?LDXR/STXR 指令可以重新排序吗?如果可以,在没有障碍的情况下,原子更新是否有可能丢失"?从我读到的内容来看,这似乎不可能发生,如果是真的,那么您为什么需要 Barrier 变体?也许只有当您碰巧还需要 DMB 用于其他目的时?

In which scenarios would you need the Load-Acquire / Store-Release semantics of the latter? Can LDXR/STXR instructions be reordered? If they can, is it possible for an atomic update to be "lost" in the absence of a barrier? From what I've read, it doesn't seem like that can happen, and if true, then why would you need the Barrier variant? Perhaps only if you also happened to need a DMB for other purposes?

谢谢!

推荐答案

OSAtomicAdd32Barrier() 存在于那些使用 OSAtomicAdd() 进行原子增量以外的事情的人.具体来说,他们正在基于 OSAtomicAdd() 实现自己的多处理同步原语.例如,创建自己的互斥库.OSAtomicAdd32Barrier() 使用重屏障指令在原子操作的两侧强制执行内存排序.这在正常使用中是不可取的.

OSAtomicAdd32Barrier() exists for people that are using OSAtomicAdd() for something beyond just atomic increment. Specifically, they are implementing their own multi-processing synchronization primitives based on OSAtomicAdd(). For example, creating their own mutex library. OSAtomicAdd32Barrier() uses heavy barrier instructions to enforce memory ordering on both side of the atomic operation. This is not desirable in normal usage.

总结:

1) 如果您只想以线程安全的方式递增整数,请使用 OSAtomicAdd32()

1) If you just want to increment an integer in a thread-safe way, use OSAtomicAdd32()

2) 如果您被一堆旧代码困住了,这些代码愚蠢地认为 OSAtomicAdd32() 可以用作处理器间内存排序和推测屏障,请将其替换为 OSAtomicAdd32Barrier()

2) If you are stuck with a bunch of old code that foolishly assumes OSAtomicAdd32() can be used as an interprocessor memory ordering and speculation barrier, replace it with OSAtomicAdd32Barrier()

这篇关于ARM64:LDXR/STXR 与 LDAXR/STLXR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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