锁总线
MESI 协议(总线嗅探机制)+ 内存屏障
那如果当前访问的数据在寄存器上面呢
那如果是多条指令的顺序性呢,内存数据还在store buffer、invalid queue上面呢?
// (java 9) hotspot/src/os_cpu/linux_x86/vm/orderAccess_linux_x86.inline.hpp
// java实现的内存屏障
inline void OrderAccess::loadload() { compiler_barrier(); }
inline void OrderAccess::storestore() { compiler_barrier(); }
inline void OrderAccess::loadstore() { compiler_barrier(); }
inline void OrderAccess::storeload() { fence(); }
inline void OrderAccess::acquire() { compiler_barrier(); }
inline void OrderAccess::release() { compiler_barrier(); }
inline void OrderAccess::fence() {
if (os::is_MP()) {
// always use locked addl since mfence is sometimes expensive
#ifdef AMD64
__asm__ volatile ("lock; addl 0,0(%%rsp)" : : : "cc", "memory");
#else
__asm__ volatile ("lock; addl0,0(%%esp)" : : : "cc", "memory");
#endif
}
compiler_barrier();
}
volatile也不过如此 http://northsea.top/?p=185
既然CPU有缓存一致性协议(MESI),为什么JMM还需要volatile关键字?https://www.zhihu.com/question/296949412