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

查看:1234
本文介绍了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天全站免登陆