Skip to content

Commit 5f62c0c

Browse files
committed
Added more text from unstable-book to compiler_fence docs
1 parent 9a60bb0 commit 5f62c0c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/libcore/sync/atomic.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,10 +1679,14 @@ pub fn fence(order: Ordering) {
16791679

16801680
/// A compiler memory fence.
16811681
///
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.
16861690
///
16871691
/// The re-ordering prevented by the different ordering semantics are:
16881692
///
@@ -1691,6 +1695,16 @@ pub fn fence(order: Ordering) {
16911695
/// - with [`Acquire`], subsequent reads and writes cannot be moved ahead of preceding reads.
16921696
/// - with [`AcqRel`], both of the above rules are enforced.
16931697
///
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+
///
16941708
/// # Panics
16951709
///
16961710
/// Panics if `order` is [`Relaxed`].
@@ -1736,6 +1750,7 @@ pub fn fence(order: Ordering) {
17361750
/// [`Release`]: enum.Ordering.html#variant.Release
17371751
/// [`AcqRel`]: enum.Ordering.html#variant.AcqRel
17381752
/// [`Relaxed`]: enum.Ordering.html#variant.Relaxed
1753+
/// [memory barriers]: https://www.kernel.org/doc/Documentation/memory-barriers.txt
17391754
#[inline]
17401755
#[stable(feature = "compiler_fences", since = "1.22.0")]
17411756
pub fn compiler_fence(order: Ordering) {

0 commit comments

Comments
 (0)