本文共 1200 字,大约阅读时间需要 4 分钟。
DMB: Data memory barrier
理解DMB指令,先看下面例子,在core 0和core1上同时跑两个不同的指令(如下表所示)
core 0 | core 1 |
---|---|
Write A; | Load B; |
Write B; | Load B; |
这里core0在执行两个指令,写A B两个值的时候,可能会发生乱序也可能Write A时发生Cache Miss,那么就会导致在cache中 A的最新值更新慢于B的最新值。于是在core1中的指令Load B就会拿到新值,而Load A 就会拿到旧值。如果A与B有相互关系的话,便可能产生死锁等问题。这里有一个典型的例子:https://lkml.org/lkml/2012/7/13/123
于是,就有了下面的解决方法:
core 0 | core 1 |
---|---|
Write A | Load B |
DMB; | Load A |
Write B |
在core0所执行的两条指令之间加入一个DMB. 这样,若core1在Load B时,拿到了最新值。那么Load A 也一定拿到了最新值。这就是DMB的作用:DMB前面的LOAD/STORE读写的最新值的acknowledgement在时间上一定先于DMB之后的指令。
SB 和DMB容易混淆。他们的区别在于:DMB可以继续执行之后的指令,只要这条指令不是内存访问指令。而DSB不管它后面的什么指令,都会强迫CPU等待它之前的指令执行完毕。其实在很多处理器设计的时候,DMB和DSB没有区别(DMB完成和DSB同样的功能)。他们以及ISB在arm reference中的解释如下[1]:
A Data Synchronization Barrier (DSB) completes when all instructions before this instruction complete.
A Data Memory Barrier (DMB) ensures that all explicit memory accesses before the DMB instruction complete before any explicit memory accesses after the DMB instruction start.
An Instruction Synchronization Barrier (ISB) flushes the pipeline in the processor, so that all instructions following the ISB are fetched from cache or memory, after the ISB has been completed.
ISB不仅做了DSB所做的事情,还将流水线清空[2]。于是他们的重量级排序可以是:ISB>DSB>DMB
转载地址:http://tdve.baihongyu.com/