Skip to content

Commit 58afc6b

Browse files
committed
compiler_fence: fix example
1 parent fc0094f commit 58afc6b

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

library/core/src/sync/atomic.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -3727,33 +3727,33 @@ pub fn fence(order: Ordering) {
37273727
///
37283728
/// # Examples
37293729
///
3730-
/// Without `compiler_fence`, the `assert_eq!` in following code
3731-
/// is *not* guaranteed to succeed, despite everything happening in a single thread.
3732-
/// To see why, remember that the compiler is free to swap the stores to
3733-
/// `IMPORTANT_VARIABLE` and `IS_READY` since they are both
3734-
/// `Ordering::Relaxed`. If it does, and the signal handler is invoked right
3735-
/// after `IS_READY` is updated, then the signal handler will see
3736-
/// `IS_READY=1`, but `IMPORTANT_VARIABLE=0`.
3737-
/// Using a `compiler_fence` remedies this situation.
3730+
/// Without the two `compiler_fence`, the read of `IMPORTANT_VARIABLE` in `signal_handler`
3731+
/// is *undefined behavior* due to a data race, despite everything happening in a single thread.
3732+
/// This is because the signal handler is considered to run concurrently with its associated
3733+
/// thread, and explicit synchronization is required to pass data between a thread and its
3734+
/// signal handler. The code below uses two `compiler_fence` to establish the usual
3735+
/// release-acquire synchronization pattern (see [`fence`] for an image).
37383736
///
37393737
/// ```
3740-
/// use std::sync::atomic::{AtomicBool, AtomicUsize};
3738+
/// use std::sync::atomic::AtomicBool;
37413739
/// use std::sync::atomic::Ordering;
37423740
/// use std::sync::atomic::compiler_fence;
37433741
///
3744-
/// static IMPORTANT_VARIABLE: AtomicUsize = AtomicUsize::new(0);
3742+
/// static mut IMPORTANT_VARIABLE: usize = 0;
37453743
/// static IS_READY: AtomicBool = AtomicBool::new(false);
37463744
///
37473745
/// fn main() {
3748-
/// IMPORTANT_VARIABLE.store(42, Ordering::Relaxed);
3749-
/// // prevent earlier writes from being moved beyond this point
3746+
/// unsafe { IMPORTANT_VARIABLE = 42 };
3747+
/// // Marks earlier writes as being released with future relaxed stores.
37503748
/// compiler_fence(Ordering::Release);
37513749
/// IS_READY.store(true, Ordering::Relaxed);
37523750
/// }
37533751
///
37543752
/// fn signal_handler() {
37553753
/// if IS_READY.load(Ordering::Relaxed) {
3756-
/// assert_eq!(IMPORTANT_VARIABLE.load(Ordering::Relaxed), 42);
3754+
/// // Acquires writes that were released with relaxed stores that we read from.
3755+
/// compiler_fence(Ordering::Acquire);
3756+
/// assert_eq!(unsafe { IMPORTANT_VARIABLE }, 42);
37573757
/// }
37583758
/// }
37593759
/// ```

0 commit comments

Comments
 (0)