Skip to content

Commit f7d4d84

Browse files
budziqdtolnay
authored andcommitted
Added example to compiler_fence docs taken from unstable-book
1 parent bdc90e5 commit f7d4d84

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libcore/sync/atomic.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,40 @@ pub fn fence(order: Ordering) {
16821682
///
16831683
/// Panics if `order` is [`Relaxed`].
16841684
///
1685+
/// # Examples
1686+
///
1687+
/// Without `compiler_fence`, the `assert_eq!` in following code
1688+
/// is *not* guaranteed to succeed, despite everything happening in a single thread.
1689+
/// To see why, remember that the compiler is free to swap the stores to
1690+
/// `IMPORTANT_VARIABLE` and `IS_READ` since they are both
1691+
/// `Ordering::Relaxed`. If it does, and the signal handler is invoked right
1692+
/// after `IS_READY` is updated, then the signal handler will see
1693+
/// `IS_READY=1`, but `IMPORTANT_VARIABLE=0`.
1694+
/// Using a `compiler_fence` remedies this situation.
1695+
///
1696+
/// ```
1697+
/// use std::sync::atomic::{AtomicBool, AtomicUsize};
1698+
/// use std::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT};
1699+
/// use std::sync::atomic::Ordering;
1700+
/// use std::sync::atomic::compiler_fence;
1701+
///
1702+
/// static IMPORTANT_VARIABLE: AtomicUsize = ATOMIC_USIZE_INIT;
1703+
/// static IS_READY: AtomicBool = ATOMIC_BOOL_INIT;
1704+
///
1705+
/// fn main() {
1706+
/// IMPORTANT_VARIABLE.store(42, Ordering::Relaxed);
1707+
/// // prevent earlier writes from being moved beyond this point
1708+
/// compiler_fence(Ordering::Release);
1709+
/// IS_READY.store(true, Ordering::Relaxed);
1710+
/// }
1711+
///
1712+
/// fn signal_handler() {
1713+
/// if IS_READY.load(Ordering::Relaxed) {
1714+
/// assert_eq!(IMPORTANT_VARIABLE.load(Ordering::Relaxed), 42);
1715+
/// }
1716+
/// }
1717+
/// ```
1718+
///
16851719
/// [`fence`]: fn.fence.html
16861720
/// [`Ordering`]: enum.Ordering.html
16871721
/// [`Acquire`]: enum.Ordering.html#variant.Acquire

0 commit comments

Comments
 (0)