diff options
| author | Michal Budzynski <budziq@gmail.com> | 2017-09-15 17:03:09 +0200 |
|---|---|---|
| committer | Michal Budzynski <budziq@gmail.com> | 2017-09-16 22:18:46 +0200 |
| commit | 9a60bb077c02d424b1031fc4ea93712dda111f2e (patch) | |
| tree | 2540d972920554e0a14de7653635e1a15c12bf7b | |
| parent | 8a11172d6b7707818142861f9405faeec7768af6 (diff) | |
| download | rust-9a60bb077c02d424b1031fc4ea93712dda111f2e.tar.gz rust-9a60bb077c02d424b1031fc4ea93712dda111f2e.zip | |
Added example to `compiler_fence` docs taken from unstable-book
| -rw-r--r-- | src/libcore/sync/atomic.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libcore/sync/atomic.rs b/src/libcore/sync/atomic.rs index c6291630a35..8e4625b7159 100644 --- a/src/libcore/sync/atomic.rs +++ b/src/libcore/sync/atomic.rs @@ -1695,6 +1695,40 @@ pub fn fence(order: Ordering) { /// /// Panics if `order` is [`Relaxed`]. /// +/// # Examples +/// +/// Without `compiler_fence`, the `assert_eq!` in following code +/// is *not* guaranteed to succeed, despite everything happening in a single thread. +/// To see why, remember that the compiler is free to swap the stores to +/// `IMPORTANT_VARIABLE` and `IS_READ` since they are both +/// `Ordering::Relaxed`. If it does, and the signal handler is invoked right +/// after `IS_READY` is updated, then the signal handler will see +/// `IS_READY=1`, but `IMPORTANT_VARIABLE=0`. +/// Using a `compiler_fence` remedies this situation. +/// +/// ``` +/// use std::sync::atomic::{AtomicBool, AtomicUsize}; +/// use std::sync::atomic::{ATOMIC_BOOL_INIT, ATOMIC_USIZE_INIT}; +/// use std::sync::atomic::Ordering; +/// use std::sync::atomic::compiler_fence; +/// +/// static IMPORTANT_VARIABLE: AtomicUsize = ATOMIC_USIZE_INIT; +/// static IS_READY: AtomicBool = ATOMIC_BOOL_INIT; +/// +/// fn main() { +/// IMPORTANT_VARIABLE.store(42, Ordering::Relaxed); +/// // prevent earlier writes from being moved beyond this point +/// compiler_fence(Ordering::Release); +/// IS_READY.store(true, Ordering::Relaxed); +/// } +/// +/// fn signal_handler() { +/// if IS_READY.load(Ordering::Relaxed) { +/// assert_eq!(IMPORTANT_VARIABLE.load(Ordering::Relaxed), 42); +/// } +/// } +/// ``` +/// /// [`fence`]: fn.fence.html /// [`Ordering`]: enum.Ordering.html /// [`Acquire`]: enum.Ordering.html#variant.Acquire |
