On x86, lock
-prefixed instructions such as lock cmpxchg
provide barrier semantics in addition to their atomic operation: for normal memory access on write-back memory regions, reads and writes are not re-ordered across lock
-prefixed instructions, per section 8.2.2 of Volume 3 of the Intel SDM:
Reads or writes cannot be reordered with I/O instructions, locked instructions, or serializing instructions.
This section applies only to write-back memory types. In the same list, you find an exception where it notes that weakly ordered stores are not ordered:
- Reads are not reordered with other reads.
- Writes are not reordered with older reads.
- Writes to memory are not reordered with other writes, with the following exceptions: —
streaming stores (writes) executed with the non-temporal move instructions (MOVNTI, MOVNTQ, MOVNTDQ, MOVNTPS, and MOVNTPD); and —
string operations (see Section 8.2.4.1).
Note that there is no exception made for non-temporal instructions in any other items in the list, e.g., in the item referring to lock-prefixed instructions.
In various other sections of the guide, it is mentioned that the mfence
and/or sfence
instructions can be used to order memory when weakly ordered (non-temporal) instructions are used. These sections generally don't mention lock
-prefixed instruction as an alternative.
All that leaves me uncertain: do lock
-prefixed instructions provide the same full barrier that mfence
provides between weakly ordered (non-temporal) instructions on WB memory? The same question applies again but to any type of access on WC memory.