Skip to content

Commit

Permalink
Add static initializers for atomics
Browse files Browse the repository at this point in the history
  • Loading branch information
Aatch committed Jul 28, 2013
1 parent 4a1a0fb commit 5c7e016
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/libstd/unstable/atomics.rs
Expand Up @@ -75,6 +75,10 @@ pub enum Ordering {
SeqCst
}

pub static INIT_ATOMIC_FLAG : AtomicFlag = AtomicFlag { v: 0 };
pub static INIT_ATOMIC_BOOL : AtomicBool = AtomicBool { v: 0 };
pub static INIT_ATOMIC_INT : AtomicInt = AtomicInt { v: 0 };
pub static INIT_ATOMIC_UINT : AtomicUint = AtomicUint { v: 0 };

impl AtomicFlag {

Expand Down Expand Up @@ -589,11 +593,13 @@ pub unsafe fn atomic_umin<T>(dst: &mut T, val: T, order: Ordering) -> T {
*/
#[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(),
unsafe {
match order {
Acquire => intrinsics::atomic_fence_acq(),
Release => intrinsics::atomic_fence_rel(),
AcqRel => intrinsics::atomic_fence_rel(),
_ => intrinsics::atomic_fence(),
}
}
}

Expand Down Expand Up @@ -657,4 +663,19 @@ mod test {
assert_eq!(a.fetch_and(false, SeqCst),true);
assert_eq!(a.load(SeqCst),false);
}

static mut S_FLAG : AtomicFlag = INIT_ATOMIC_FLAG;
static mut S_BOOL : AtomicBool = INIT_ATOMIC_BOOL;
static mut S_INT : AtomicInt = INIT_ATOMIC_INT;
static mut S_UINT : AtomicUint = INIT_ATOMIC_UINT;

#[test]
fn static_init() {
unsafe {
assert!(!S_FLAG.test_and_set(SeqCst));
assert!(!S_BOOL.load(SeqCst));
assert!(S_INT.load(SeqCst) == 0);
assert!(S_UINT.load(SeqCst) == 0);
}
}
}

0 comments on commit 5c7e016

Please sign in to comment.