Skip to content

Commit 5c7e016

Browse files
committed
Add static initializers for atomics
1 parent 4a1a0fb commit 5c7e016

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/libstd/unstable/atomics.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ pub enum Ordering {
7575
SeqCst
7676
}
7777

78+
pub static INIT_ATOMIC_FLAG : AtomicFlag = AtomicFlag { v: 0 };
79+
pub static INIT_ATOMIC_BOOL : AtomicBool = AtomicBool { v: 0 };
80+
pub static INIT_ATOMIC_INT : AtomicInt = AtomicInt { v: 0 };
81+
pub static INIT_ATOMIC_UINT : AtomicUint = AtomicUint { v: 0 };
7882

7983
impl AtomicFlag {
8084

@@ -589,11 +593,13 @@ pub unsafe fn atomic_umin<T>(dst: &mut T, val: T, order: Ordering) -> T {
589593
*/
590594
#[inline] #[cfg(not(stage0))]
591595
pub fn fence(order: Ordering) {
592-
match order {
593-
Acquire => intrinsics::atomic_fence_acq(),
594-
Release => intrinsics::atomic_fence_rel(),
595-
AcqRel => intrinsics::atomic_fence_rel(),
596-
_ => intrinsics::atomic_fence(),
596+
unsafe {
597+
match order {
598+
Acquire => intrinsics::atomic_fence_acq(),
599+
Release => intrinsics::atomic_fence_rel(),
600+
AcqRel => intrinsics::atomic_fence_rel(),
601+
_ => intrinsics::atomic_fence(),
602+
}
597603
}
598604
}
599605

@@ -657,4 +663,19 @@ mod test {
657663
assert_eq!(a.fetch_and(false, SeqCst),true);
658664
assert_eq!(a.load(SeqCst),false);
659665
}
666+
667+
static mut S_FLAG : AtomicFlag = INIT_ATOMIC_FLAG;
668+
static mut S_BOOL : AtomicBool = INIT_ATOMIC_BOOL;
669+
static mut S_INT : AtomicInt = INIT_ATOMIC_INT;
670+
static mut S_UINT : AtomicUint = INIT_ATOMIC_UINT;
671+
672+
#[test]
673+
fn static_init() {
674+
unsafe {
675+
assert!(!S_FLAG.test_and_set(SeqCst));
676+
assert!(!S_BOOL.load(SeqCst));
677+
assert!(S_INT.load(SeqCst) == 0);
678+
assert!(S_UINT.load(SeqCst) == 0);
679+
}
680+
}
660681
}

0 commit comments

Comments
 (0)