14.2.4 What Can You Trust?
Keywords: perfbook_html

##
14.2.4 What Can You Trust?

You most definitely cannot trust your intuition.

What can you trust?

It turns out that there are a few reasonably simple rules that allow you to make good use of memory barriers. This section derives those rules, for those who wish to get to the bottom of the memory-barrier story, at least from the viewpoint of portable code. If you just want to be told what the rules are rather than suffering through the actual derivation, please feel free to skip to Section .

The exact semantics of memory barriers vary wildly from one CPU to another, so portable code must rely only on the least-common-denominator semantics of memory barriers.

Fortunately, all CPUs impose the following rules:

1. All accesses by a given CPU will appear to that CPU to have occurred in program order.
2. All CPUs' accesses to a single variable will be consistent with some global ordering of stores to that variable.
3. Memory barriers will operate in a pair-wise fashion.
4. Operations will be provided from which exclusive locking primitives may be constructed.

Therefore, if you need to use memory barriers in portable code, you can rely on all of these properties.14.1Each of these properties is described in the following sections.

Subsections

- 14.2.4.1 Self-References Are Ordered
- 14.2.4.2 Single-Variable Memory Consistency
- 14.2.4.3 Pair-Wise Memory Barriers
- 14.2.4.4 Pair-Wise Memory Barriers: Portable Combinations
  - 14.2.4.4.1 Pairing 1.
  - 14.2.4.4.2 Pairing 2.
  - 14.2.4.4.3 Pairing 3.
- 14.2.4.5 Pair-Wise Memory Barriers: Semi-Portable Combinations
  - 14.2.4.5.1 Ears to Mouths.
  - 14.2.4.5.2 Stores ``Pass in the Night''.
- 14.2.4.6 Pair-Wise Memory Barriers: Non-Portable Combinations
  - 14.2.4.6.1 Ears to Ears.
  - 14.2.4.6.2 Mouth to Mouth, Ear to Ear.
  - 14.2.4.6.3 Only One Store.
- 14.2.4.7 Semantics Sufficient to Implement Locking