Skip to content

Commit f0072bf

Browse files
authored
Rollup merge of #129856 - RalfJung:compiler_fence, r=thomcc
compiler_fence documentation: emphasize synchronization, not reordering Our `fence` docs have at some point been update to explain that they are about synchronization, not about "preventing reordering". This updates the `compiler_fence` docs n the same vein, mostly by referring to the `fence` docs. The old docs make it sound like I can put a compiler_fence in the middle of a bunch of non-atomic operations and that would achieve any kind of guarantee. It does not, atomic operations are still required to do synchronization. I also slightly tweaked the `fence` docs, to put the synchronization first and the "prevent reordering" second. Cc `@rust-lang/opsem` `@chorman0773` `@m-ou-se` Fixes #129189 Fixes #54962
2 parents c6410f5 + 32a30dd commit f0072bf

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

library/core/src/sync/atomic.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -3570,10 +3570,9 @@ unsafe fn atomic_umin<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
35703570

35713571
/// An atomic fence.
35723572
///
3573-
/// Depending on the specified order, a fence prevents the compiler and CPU from
3574-
/// reordering certain types of memory operations around it.
3575-
/// That creates synchronizes-with relationships between it and atomic operations
3576-
/// or fences in other threads.
3573+
/// Fences create synchronization between themselves and atomic operations or fences in other
3574+
/// threads. To achieve this, a fence prevents the compiler and CPU from reordering certain types of
3575+
/// memory operations around it.
35773576
///
35783577
/// A fence 'A' which has (at least) [`Release`] ordering semantics, synchronizes
35793578
/// with a fence 'B' with (at least) [`Acquire`] semantics, if and only if there
@@ -3594,6 +3593,12 @@ unsafe fn atomic_umin<T: Copy>(dst: *mut T, val: T, order: Ordering) -> T {
35943593
/// }
35953594
/// ```
35963595
///
3596+
/// Note that in the example above, it is crucial that the accesses to `x` are atomic. Fences cannot
3597+
/// be used to establish synchronization among non-atomic accesses in different threads. However,
3598+
/// thanks to the happens-before relationship between A and B, any non-atomic accesses that
3599+
/// happen-before A are now also properly synchronized with any non-atomic accesses that
3600+
/// happen-after B.
3601+
///
35973602
/// Atomic operations with [`Release`] or [`Acquire`] semantics can also synchronize
35983603
/// with a fence.
35993604
///
@@ -3659,33 +3664,30 @@ pub fn fence(order: Ordering) {
36593664
}
36603665
}
36613666

3662-
/// A compiler memory fence.
3667+
/// A "compiler-only" atomic fence.
36633668
///
3664-
/// `compiler_fence` does not emit any machine code, but restricts the kinds
3665-
/// of memory re-ordering the compiler is allowed to do. Specifically, depending on
3666-
/// the given [`Ordering`] semantics, the compiler may be disallowed from moving reads
3667-
/// or writes from before or after the call to the other side of the call to
3668-
/// `compiler_fence`. Note that it does **not** prevent the *hardware*
3669-
/// from doing such re-ordering. This is not a problem in a single-threaded,
3670-
/// execution context, but when other threads may modify memory at the same
3671-
/// time, stronger synchronization primitives such as [`fence`] are required.
3669+
/// Like [`fence`], this function establishes synchronization with other atomic operations and
3670+
/// fences. However, unlike [`fence`], `compiler_fence` only establishes synchronization with
3671+
/// operations *in the same thread*. This may at first sound rather useless, since code within a
3672+
/// thread is typically already totally ordered and does not need any further synchronization.
3673+
/// However, there are cases where code can run on the same thread without being ordered:
3674+
/// - The most common case is that of a *signal handler*: a signal handler runs in the same thread
3675+
/// as the code it interrupted, but it is not ordered with respect to that code. `compiler_fence`
3676+
/// can be used to establish synchronization between a thread and its signal handler, the same way
3677+
/// that `fence` can be used to establish synchronization across threads.
3678+
/// - Similar situations can arise in embedded programming with interrupt handlers, or in custom
3679+
/// implementations of preemptive green threads. In general, `compiler_fence` can establish
3680+
/// synchronization with code that is guaranteed to run on the same hardware CPU.
36723681
///
3673-
/// The re-ordering prevented by the different ordering semantics are:
3682+
/// See [`fence`] for how a fence can be used to achieve synchronization. Note that just like
3683+
/// [`fence`], synchronization still requires atomic operations to be used in both threads -- it is
3684+
/// not possible to perform synchronization entirely with fences and non-atomic operations.
36743685
///
3675-
/// - with [`SeqCst`], no re-ordering of reads and writes across this point is allowed.
3676-
/// - with [`Release`], preceding reads and writes cannot be moved past subsequent writes.
3677-
/// - with [`Acquire`], subsequent reads and writes cannot be moved ahead of preceding reads.
3678-
/// - with [`AcqRel`], both of the above rules are enforced.
3686+
/// `compiler_fence` does not emit any machine code, but restricts the kinds of memory re-ordering
3687+
/// the compiler is allowed to do. `compiler_fence` corresponds to [`atomic_signal_fence`] in C and
3688+
/// C++.
36793689
///
3680-
/// `compiler_fence` is generally only useful for preventing a thread from
3681-
/// racing *with itself*. That is, if a given thread is executing one piece
3682-
/// of code, and is then interrupted, and starts executing code elsewhere
3683-
/// (while still in the same thread, and conceptually still on the same
3684-
/// core). In traditional programs, this can only occur when a signal
3685-
/// handler is registered. In more low-level code, such situations can also
3686-
/// arise when handling interrupts, when implementing green threads with
3687-
/// pre-emption, etc. Curious readers are encouraged to read the Linux kernel's
3688-
/// discussion of [memory barriers].
3690+
/// [`atomic_signal_fence`]: https://en.cppreference.com/w/cpp/atomic/atomic_signal_fence
36893691
///
36903692
/// # Panics
36913693
///
@@ -3723,8 +3725,6 @@ pub fn fence(order: Ordering) {
37233725
/// }
37243726
/// }
37253727
/// ```
3726-
///
3727-
/// [memory barriers]: https://www.kernel.org/doc/Documentation/memory-barriers.txt
37283728
#[inline]
37293729
#[stable(feature = "compiler_fences", since = "1.21.0")]
37303730
#[rustc_diagnostic_item = "compiler_fence"]

0 commit comments

Comments
 (0)