鎖總線,其它CPU對內存的讀寫請求都會被阻塞,直到鎖釋放,因為鎖總線的開銷比較大,后來的處理器都采用鎖緩存替代鎖總線,在無法使用緩存鎖的時候會降級使用總線鎖
lock期間的寫操作會回寫已修改的數據到主內存,同時通過緩存一致性協議讓其它CPU相關緩存行失效
https://albk.tech/聊聊CPU的LOCK指令.html
==========
lock才會觸發到mesi
lock前綴的匯編指令會強制寫入主存,也可避免前后指令的CPU重排序,並及時讓其他核中的相應
緩存行失效,從而利用MESI達到符合預期的效果。
https://www.felixcloutier.com/x86/lock
https://kc.kexinshe.com/r/277831
8.2.2 Memory Ordering in P6 and More Recent Processor Families
• Reads or writes cannot be reordered with I/O instructions, locked instructions, or serializing instructions.
8.2.5 Strengthening or Weakening the Memory-Ordering Model
Program synchronization can also be carried out with serializing instructions (see Section 8.3). These instructions
are typically used at critical procedure or task boundaries to force completion of all previous instructions before a
jump to a new section of code or a context switch occurs. Like the I/O and locking instructions, the processor waits
until all previous instructions have been completed and all buffered writes have been drained to memory before
executing the serializing instruction.
Lock前綴,Lock不是一種內存屏障,但是它能完成類似內存屏障的功能。Lock會,可以理解為CPU指令級的一種鎖。它后面可以跟ADD, ADC, AND, BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR, SBB, SUB, XOR, XADD, and XCHG等指令。
在x86架構上,CAS被翻譯為”lock cmpxchg...“。cmpxchg是CAS的匯編指令。在CPU架構中依靠lock信號保證可見性並禁止重排序。
lock前綴是一個特殊的信號,執行過程如下:
對總線和緩存上鎖。
強制所有lock信號之前的指令,都在此之前被執行,並同步相關緩存。
執行lock后的指令(如cmpxchg)。
釋放對總線和緩存上的鎖。
強制所有lock信號之后的指令,都在此之后被執行,並同步相關緩存。
因此,lock信號雖然不是內存屏障,但具有mfence的語義(當然,還有排他性的語義)。
與內存屏障相比,lock信號要額外對總線和緩存上鎖,成本更高
https://monkeysayhi.github.io/2017/12/28/一文解決內存屏障/
=====