@@ -1679,10 +1679,14 @@ pub fn fence(order: Ordering) {
1679
1679
1680
1680
/// A compiler memory fence.
1681
1681
///
1682
- /// `compiler_fence` does not emit any machine code, but prevents the compiler from re-ordering
1683
- /// memory operations across this point. Which reorderings are disallowed is dictated by the given
1684
- /// [`Ordering`]. Note that `compiler_fence` does *not* introduce inter-thread memory
1685
- /// synchronization; for that, a [`fence`] is needed.
1682
+ /// `compiler_fence` does not emit any machine code, but restricts the kinds
1683
+ /// of memory re-ordering the compiler is allowed to do. Specifically, depending on
1684
+ /// the given [`Ordering`] semantics, the compiler may be disallowed from moving reads
1685
+ /// or writes from before or after the call to the other side of the call to
1686
+ /// `compiler_fence`. Note that it does **not** prevent the *hardware*
1687
+ /// from doing such re-ordering. This is not a problem in a single-threaded,
1688
+ /// execution context, but when other threads may modify memory at the same
1689
+ /// time, stronger synchronization primitives such as [`fence`] are required.
1686
1690
///
1687
1691
/// The re-ordering prevented by the different ordering semantics are:
1688
1692
///
@@ -1691,6 +1695,16 @@ pub fn fence(order: Ordering) {
1691
1695
/// - with [`Acquire`], subsequent reads and writes cannot be moved ahead of preceding reads.
1692
1696
/// - with [`AcqRel`], both of the above rules are enforced.
1693
1697
///
1698
+ /// `compiler_fence` is generally only useful for preventing a thread from
1699
+ /// racing *with itself*. That is, if a given thread is executing one piece
1700
+ /// of code, and is then interrupted, and starts executing code elsewhere
1701
+ /// (while still in the same thread, and conceptually still on the same
1702
+ /// core). In traditional programs, this can only occur when a signal
1703
+ /// handler is registered. In more low-level code, such situations can also
1704
+ /// arise when handling interrupts, when implementing green threads with
1705
+ /// pre-emption, etc. Curious readers are encouraged to read the Linux kernel's
1706
+ /// discussion of [memory barriers].
1707
+ ///
1694
1708
/// # Panics
1695
1709
///
1696
1710
/// Panics if `order` is [`Relaxed`].
@@ -1736,6 +1750,7 @@ pub fn fence(order: Ordering) {
1736
1750
/// [`Release`]: enum.Ordering.html#variant.Release
1737
1751
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
1738
1752
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
1753
+ /// [memory barriers]: https://www.kernel.org/doc/Documentation/memory-barriers.txt
1739
1754
#[ inline]
1740
1755
#[ stable( feature = "compiler_fences" , since = "1.22.0" ) ]
1741
1756
pub fn compiler_fence ( order : Ordering ) {
0 commit comments