Skip to content

Commit 91a9866

Browse files
topecongiroSeiichi Uchida
authored and
Seiichi Uchida
committed
Add an example for 'fence'
1 parent f8107c0 commit 91a9866

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/libcore/sync/atomic.rs

+52-3
Original file line numberDiff line numberDiff line change
@@ -1550,12 +1550,30 @@ unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
15501550

15511551
/// An atomic fence.
15521552
///
1553-
/// A fence 'A' which has [`Release`] ordering semantics, synchronizes with a
1554-
/// fence 'B' with (at least) [`Acquire`] semantics, if and only if there exists
1555-
/// atomic operations X and Y, both operating on some atomic object 'M' such
1553+
/// Depending on the specified order, a fence prevents the compiler and CPU from
1554+
/// reordering certain types of memory operations around it.
1555+
/// That creates synchronizes-with relationships between it and atomic operations
1556+
/// or fences in other threads.
1557+
///
1558+
/// A fence 'A' which has (at least) [`Release`] ordering semantics, synchronizes
1559+
/// with a fence 'B' with (at least) [`Acquire`] semantics, if and only if there
1560+
/// exist operations X and Y, both operating on some atomic object 'M' such
15561561
/// that A is sequenced before X, Y is synchronized before B and Y observes
15571562
/// the change to M. This provides a happens-before dependence between A and B.
15581563
///
1564+
/// ```text
1565+
/// Thread 1 Thread 2
1566+
///
1567+
/// fence(Release); A --------------
1568+
/// x.store(3, Relaxed); X --------- |
1569+
/// | |
1570+
/// | |
1571+
/// -------------> Y if x.load(Relaxed) == 3 {
1572+
/// |-------> B fence(Acquire);
1573+
/// ...
1574+
/// }
1575+
/// ```
1576+
///
15591577
/// Atomic operations with [`Release`] or [`Acquire`] semantics can also synchronize
15601578
/// with a fence.
15611579
///
@@ -1569,6 +1587,37 @@ unsafe fn atomic_xor<T>(dst: *mut T, val: T, order: Ordering) -> T {
15691587
///
15701588
/// Panics if `order` is [`Relaxed`].
15711589
///
1590+
/// # Examples
1591+
///
1592+
/// ```
1593+
/// use std::sync::atomic::AtomicBool;
1594+
/// use std::sync::atomic::fence;
1595+
/// use std::sync::atomic::Ordering;
1596+
///
1597+
/// // A mutual exclusion primitive based on spinlock.
1598+
/// pub struct Mutex {
1599+
/// flag: AtomicBool,
1600+
/// }
1601+
///
1602+
/// impl Mutex {
1603+
/// pub fn new() -> Mutex {
1604+
/// Mutex {
1605+
/// flag: AtomicBool::new(false),
1606+
/// }
1607+
/// }
1608+
///
1609+
/// pub fn lock(&self) {
1610+
/// while !self.flag.compare_and_swap(false, true, Ordering::Relaxed) {}
1611+
/// // This fence syncronizes-with store in `unlock`.
1612+
/// fence(Ordering::Acquire);
1613+
/// }
1614+
///
1615+
/// pub fn unlock(&self) {
1616+
/// self.flag.store(false, Ordering::Release);
1617+
/// }
1618+
/// }
1619+
/// ```
1620+
///
15721621
/// [`Ordering`]: enum.Ordering.html
15731622
/// [`Acquire`]: enum.Ordering.html#variant.Acquire
15741623
/// [`SeqCst`]: enum.Ordering.html#variant.SeqCst

0 commit comments

Comments
 (0)