@@ -3727,33 +3727,33 @@ pub fn fence(order: Ordering) {
3727
3727
///
3728
3728
/// # Examples
3729
3729
///
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).
3738
3736
///
3739
3737
/// ```
3740
- /// use std::sync::atomic::{ AtomicBool, AtomicUsize} ;
3738
+ /// use std::sync::atomic::AtomicBool;
3741
3739
/// use std::sync::atomic::Ordering;
3742
3740
/// use std::sync::atomic::compiler_fence;
3743
3741
///
3744
- /// static IMPORTANT_VARIABLE: AtomicUsize = AtomicUsize::new(0) ;
3742
+ /// static mut IMPORTANT_VARIABLE: usize = 0 ;
3745
3743
/// static IS_READY: AtomicBool = AtomicBool::new(false);
3746
3744
///
3747
3745
/// 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.
3750
3748
/// compiler_fence(Ordering::Release);
3751
3749
/// IS_READY.store(true, Ordering::Relaxed);
3752
3750
/// }
3753
3751
///
3754
3752
/// fn signal_handler() {
3755
3753
/// 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);
3757
3757
/// }
3758
3758
/// }
3759
3759
/// ```
0 commit comments