我希望能够为Java无锁程序员翻译有关内存屏障的相关内容。我认为,这个层次介于仅学习volatile和从x86手册学习Store/Load缓冲区的工作之间。
我花了一些时间阅读了许多博客/食谱,并得出了以下总结。请有更多知识的人查看摘要,以查看我是否遗漏或列出了一些错误。
LFENCE:
最后,如果SFENCE和MFENCE都会清空存储缓冲区(使缓存行失效并等待其他CPU的确认),为什么一个操作是无操作,而另一个操作非常昂贵?
谢谢。
(从谷歌的机械同情论坛转帖)
我花了一些时间阅读了许多博客/食谱,并得出了以下总结。请有更多知识的人查看摘要,以查看我是否遗漏或列出了一些错误。
LFENCE:
Name : LFENCE/Load Barrier/Acquire Fence
Barriers : LoadLoad + LoadStore
Details : Given sequence {Load1, LFENCE, Load2, Store1}, the
barrier ensures that Load1 can't be moved south and
Load2 and Store1 can't be moved north of the
barrier.
Note that Load2 and Store1 can still be reordered.
Buffer Effect : Causes the contents of the LoadBuffer
(pending loads) to be processed for that CPU.This
makes program state exposed from other CPUs visible
to this CPU before Load2 and Store1 are executed.
Cost on x86 : Either very cheap or a no-op.
Java instructions: Reading a volatile variable, Unsafe.loadFence()
SFENCE
Name : SFENCE/Store Barrier/Release Fence
Barriers : StoreStore + LoadStore
Details : Given sequence {Load1, Store1, SFENCE, Store2,Load2}
the barrier ensures that Load1 and Store1 can't be
moved south and Store2 can't be moved north of the
barrier.
Note that Load1 and Store1 can still be reordered AND
Load2 can be moved north of the barrier.
Buffer Effect : Causes the contents of the StoreBuffer flushed to
cache for the CPU on which it is issued.
This will make program state visible to other CPUs
before Store2 and Load1 are executed.
Cost on x86 : Either very cheap or a no-op.
Java instructions: lazySet(), Unsafe.storeFence(), Unsafe.putOrdered*()
MFENCE
Name : MFENCE/Full Barrier/Fence
Barriers : StoreLoad
Details : Obtains the effects of the other three barrier.
Given sequence {Load1, Store1, MFENCE, Store2,Load2},
the barrier ensures that Load1 and Store1 can't be
moved south and Store2 and Load2 can't be moved north
of the barrier.
Note that Load1 and Store1 can still be reordered AND
Store2 and Load2 can still be reordered.
Buffer Effect : Causes the contents of the LoadBuffer (pending loads)
to be processed for that CPU.
AND
Causes the contents of the StoreBuffer flushed to
cache for the CPU on which it is issued.
Cost on x86 : The most expensive kind.
Java instructions: Writing to a volatile, Unsafe.fullFence(), Locks
最后,如果SFENCE和MFENCE都会清空存储缓冲区(使缓存行失效并等待其他CPU的确认),为什么一个操作是无操作,而另一个操作非常昂贵?
谢谢。
(从谷歌的机械同情论坛转帖)