From 4a1a0fbed5a57958eb7b658bbe3e5257872ae99f Mon Sep 17 00:00:00 2001 From: James Miller Date: Sun, 28 Jul 2013 19:48:16 +1200 Subject: Add an atomic fence intrinsic --- src/libstd/unstable/atomics.rs | 27 +++++++++++++++++++++++++++ src/libstd/unstable/intrinsics.rs | 9 +++++++++ 2 files changed, 36 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/unstable/atomics.rs b/src/libstd/unstable/atomics.rs index 712c32d2436..8b57da32bbd 100644 --- a/src/libstd/unstable/atomics.rs +++ b/src/libstd/unstable/atomics.rs @@ -569,6 +569,33 @@ pub unsafe fn atomic_umin(dst: &mut T, val: T, order: Ordering) -> T { }) } +/** + * An atomic fence. + * + * A fence 'A' which has `Release` ordering semantics, synchronizes with a + * fence 'B' with (at least) `Aquire` semantics, if and only if there exists + * atomic operations X and Y, bother operating on some atomic object 'M' such + * that A is sequenced before X, Y is synchronized before B and Y obsevers + * the change to M. This provides a happens-before dependence between A and B. + * + * Atomic operations with `Release` or `Acquire` semantics can also synchronize + * with a fence. + * + * A fence with has `SeqCst` ordering, in addition to having both `Acquire` and + * `Release` semantics, participates in the global program order of the other + * `SeqCst` operations and/or fences. + * + * Accepts `Acquire`, `Release`, `AcqRel` and `SeqCst` orderings. + */ +#[inline] #[cfg(not(stage0))] +pub fn fence(order: Ordering) { + match order { + Acquire => intrinsics::atomic_fence_acq(), + Release => intrinsics::atomic_fence_rel(), + AcqRel => intrinsics::atomic_fence_rel(), + _ => intrinsics::atomic_fence(), + } +} #[cfg(test)] mod test { diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index 76958fa333f..655ede6b4eb 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -256,6 +256,15 @@ extern "rust-intrinsic" { pub fn atomic_umax_acqrel(dst: &mut int, src: int) -> int; pub fn atomic_umax_relaxed(dst: &mut int, src: int) -> int; + #[cfg(not(stage0))] + pub fn atomic_fence(); + #[cfg(not(stage0))] + pub fn atomic_fence_acq(); + #[cfg(not(stage0))] + pub fn atomic_fence_rel(); + #[cfg(not(stage0))] + pub fn atomic_fence_acqrel(); + /// The size of a type in bytes. /// /// This is the exact number of bytes in memory taken up by a -- cgit 1.4.1-3-g733a5