Exclusive|Exclusive monitors

https://developer.arm.com/documentation/dht0008/a/arm-synchronization-primitives/exclusive-accesses/ldrex-and-strex
Exclusive monitors 独占监视器
An exclusive monitor is a simple state machine, with the possible states open and exclusive. To support synchronization between processors, a system must implement two sets of monitors, local and global. A Load-Exclusive operation updates the monitors to exclusive state. A Store-Exclusive operation accesses the monitor(s) to determine whether it can complete successfully. A Store-Exclusive can succeed only if all accessed exclusive monitors are in the exclusive state.
独占监视器是一个简单状态机,可能的状态有open和exclusive。为了支持两个进程之间的同步,一个系统必须实现两个监视器,本地监视器和全局监视器。一个ldrEX操作将监视器置为独占状态。一个strEX需要访问监视器来决定它是否能成功执行。只有在strex所访问的所有独占监视器都是独占状态的时候,它才能执行成功。
Local monitors Each processor that supports exclusive accesses has a local monitor. Exclusive accesses to memory locations marked as Non-shareable are checked only against this local monitor. Exclusive accesses to memory locations marked as Shareable are checked against both the local monitor and the global monitor.
每个处理器都有一个本地的独占监视器。独占访问被标记为非共享内存的时候,只用检查本地监视器状态。独占访问被标记为恭喜内存的时候,需要检查本地监视器和全局监视器。
For example, if software executing on the Cortex-A8 processor in Figure 1.1 must enforce synchronization between applications executing locally, it can do this using a mutex placed in Non-shareable memory. The resulting Load-Exclusive and Store-Exclusive instructions only access the local monitor.
例如,如果在A8处理器上的软件之间进行同步,可以使用非共享内存中的mutex来实现。ldrex和strex将只会访问本地监视器。
A local monitor can be implemented to tag an address for exclusive use, or it can contain a state machine that only tracks the issuing of Load-Exclusive and Store-Exclusive instructions. This means a Store-Exclusive to a Shareable location might succeed even if the preceding Load-Exclusive was from a completely different location. For this reason, portable code must not make assumptions about exclusive accesses performing address checking.
本地监视器可以在将要独占的内存地址打上tag,也可以实现成包含一个追踪ldrex和strex指令的状态机。这也意味着对于共享内存来说,即使ldrex和strex来自完全不同的位置,strex操作也可能成功。因此,可移植的代码不得对互斥访问做地址上的假设和检查。
Note

If the location is cacheable, the synchronization might take place without any external bus transactions, and without the result being visible to external observers, for example other processors in the system.
如果该位置是可缓存的,那么同步操作可能不需要执行任何外部总线,这也意味着对于外部观察者来说可能是不可见的,例如系统上的另外一个处理器。
The global monitor 全局监视器
A global monitor tracks exclusive accesses to memory regions marked as Shareable. Any Store-Exclusive operation that targets Shareable memory must check its local monitor and the global monitor to determine whether it can update memory.
全局监视器跟踪共享内存的独占状态。任何strex的操作,必须检查它自己的本地监视器和全局监视器,来决定它是否可以更新内存。
For example, if software executing on one processor in Figure 1.1 must synchronize its operation with software executing on the other processor, it can do this using a mutex placed in Shareable memory. The resulting Load-Exclusive and Store-Exclusive instructions access both the local monitor and the global monitor.
例如,在一个处理器上的软件必须将操作同步到另一个处理器上,它可以使用共享内存中的mutex锁。ldrex和strex指令将访问本地监视器和全局监视器。
It is also possible for a global monitor, or part of the global monitor, to be implemented combined with the local monitor, for example in a system implementing cache coherency management. See Use in multi-core systems.
The global monitor can tag one address for each processor in the system that supports exclusive accesses. When a processor performs a Load-Exclusive to a Shareable location, the global monitor tags the accessed address for exclusive use by that processor. The following events reset the global monitor entry for processor N to open state:
全局监视器可以将一个地址为每个支持独占的处理器打上标记。当一个处理器要对一个共享位置执行ldrex操作,全局监视器会将这个地址标记为该处理器独占。以下事件将全局监视器重置为open状态:
  • processor N performs an exclusive load from a different location
  • 处理器N从另一个位置执行ldrex加载
  • a different processor successfully performs a store, or a Store-Exclusive, to the location tagged for exclusive use by processor N.
  • 另外一个处理器对一个标记为处理器N独占使用的地址,成功执行一次str或strex
Other events can clear a global exclusive monitor, but they are implementation defined and portable code must not rely on them.
【Exclusive|Exclusive monitors】其他事件也可以清除全局独占监视器,但是这依赖处理器的实现,可移植代码不应该依靠这些事件。
Note
If a region configured as Shareable is not associated with a global monitor, Store-Exclusive operations to that region always fail, returning 0 in the destination register.
如果一个配置成共享的区域,没有和全局监视器关联,则对这个区域的strex操作总是会失败,向目的寄存器返回0。
Exclusives Reservation Granule 独占保留粒度
When an exclusive monitor tags an address, the minimum region that can be tagged for exclusive access is called the Exclusives Reservation Granule (ERG). The ERG is implementation defined, in the range 8-2048 bytes, in multiples of two bytes. Portable code must not assume anything about ERG size.
当一个独占监视器标记了一个地址,可以标记用于最小访问的地址,叫做ERG独占保留粒度。ERG是实现定义的,范围在8-2048bytes之间,是2bytes的整倍数。可移植代码不应该假设任何ERG大小。
Resetting monitors 重置监视器
When an operating system performs a context switch, it must reset the local monitor to open state, to prevent false positives occurring. ARMv6K introduced the Clear-Exclusive instruction, CLREX, to reset the local monitor.
当一个操作系统施行一次上下文切换,它必须将本地监视器重置为open状态,防止假事件发生。
Note
In ARMv6 base architecture and ARMv6T2, the local monitor must be reset by performing a dummy Store-Exclusive to a dedicated address.
The state of monitors is architecturally undefined after a Data Abort exception. Therefore, ARM recommends that the exception handling code executes a CLREX or dummy Store-Exclusive instruction.
在发生一次date abort异常后,监视器的状态在架构上是未定义的。因此ARM推荐异常处理代码执行一次clrex或一个假的strex指令。
If a context switch schedules out a process after the process has performed a Load-Exclusive but before it performs the Store-Exclusive, the Store-Exclusive returns a false negative result when the process resumes, and memory is not updated. This does not affect program functionality, because the process can retry the operation immediately.
如果在一个程序执行了ldrex后,执行strex之前,被上下文调度器切换走,当流程恢复时,strex指令返回一个错误的否定结论,并且内存没有被更新。这不影响程序功能,因为程序可以立即重试这个操作。
For these reasons ARM recommends that:
由于这些原因,ARM推荐:
  • the Load-Exclusive and Store-Exclusive are no more than 128 bytes apart
  • ldrex和strex指令相距不要超过128bytes
  • no explicit cache maintenance operations or data accesses are performed between the Load-Exclusive and the Store-Exclusive.
  • 在ldrex和strex之间,不执行显示的cache维护和数据访问。

    推荐阅读