Skip to content

Remove AtomicFlag #12951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 6 additions & 41 deletions src/libstd/sync/atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ use std::kinds::marker;
use option::{Option,Some,None};
use ops::Drop;

/**
* A simple atomic flag, that can be set and cleared. The most basic atomic type.
*/
pub struct AtomicFlag {
priv v: int,
priv nopod: marker::NoPod
}

/**
* An atomic boolean type.
*/
Expand Down Expand Up @@ -92,36 +84,11 @@ pub enum Ordering {
SeqCst
}

pub static INIT_ATOMIC_FLAG : AtomicFlag = AtomicFlag { v: 0, nopod: marker::NoPod };
pub static INIT_ATOMIC_BOOL : AtomicBool = AtomicBool { v: 0, nopod: marker::NoPod };
pub static INIT_ATOMIC_INT : AtomicInt = AtomicInt { v: 0, nopod: marker::NoPod };
pub static INIT_ATOMIC_UINT : AtomicUint = AtomicUint { v: 0, nopod: marker::NoPod };
pub static INIT_ATOMIC_U64 : AtomicU64 = AtomicU64 { v: 0, nopod: marker::NoPod };

impl AtomicFlag {

pub fn new() -> AtomicFlag {
AtomicFlag { v: 0, nopod: marker::NoPod}
}

/**
* Clears the atomic flag
*/
#[inline]
pub fn clear(&mut self, order: Ordering) {
unsafe {atomic_store(&mut self.v, 0, order)}
}

/**
* Sets the flag if it was previously unset, returns the previous value of the
* flag.
*/
#[inline]
pub fn test_and_set(&mut self, order: Ordering) -> bool {
unsafe { atomic_compare_and_swap(&mut self.v, 0, 1, order) > 0 }
}
}

impl AtomicBool {
pub fn new(v: bool) -> AtomicBool {
AtomicBool { v: if v { 1 } else { 0 }, nopod: marker::NoPod }
Expand Down Expand Up @@ -539,13 +506,13 @@ mod test {
use super::*;

#[test]
fn flag() {
let mut flg = AtomicFlag::new();
assert!(!flg.test_and_set(SeqCst));
assert!(flg.test_and_set(SeqCst));
fn bool_() {
let mut a = AtomicBool::new(false);
assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
assert_eq!(a.compare_and_swap(false, true, SeqCst), true);

flg.clear(SeqCst);
assert!(!flg.test_and_set(SeqCst));
a.store(false, SeqCst);
assert_eq!(a.compare_and_swap(false, true, SeqCst), false);
}

#[test]
Expand Down Expand Up @@ -595,15 +562,13 @@ mod test {
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);
Expand Down
2 changes: 0 additions & 2 deletions src/test/compile-fail/std-uncopyable-atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use std::sync::atomics::*;
use std::ptr;

fn main() {
let x = INIT_ATOMIC_FLAG; //~ ERROR cannot move out of static item
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_BOOL; //~ ERROR cannot move out of static item
let x = *&x; //~ ERROR: cannot move out of dereference
let x = INIT_ATOMIC_INT; //~ ERROR cannot move out of static item
Expand Down