Struct std::sync::atomics::AtomicBool[src]
pub struct AtomicBool {
// some fields omitted
}An atomic boolean type.
Methods
impl AtomicBool
fn new(v: bool) -> AtomicBool
Create a new AtomicBool
fn load(&self, order: Ordering) -> bool
Load the value
fn store(&self, val: bool, order: Ordering)
Store the value
fn swap(&self, val: bool, order: Ordering) -> bool
Store a value, returning the old value
fn compare_and_swap(&self, old: bool, new: bool, order: Ordering) -> bool
If the current value is the same as expected, store a new value
Compare the current value with old; if they are the same then
replace the current value with new. Return the previous value.
If the return value is equal to old then the value was updated.
Examples
use std::sync::Arc; use std::sync::atomics::{AtomicBool, SeqCst}; use std::task::deschedule; fn main() { let spinlock = Arc::new(AtomicBool::new(false)); let spinlock_clone = spinlock.clone(); spawn(proc() { with_lock(&spinlock, || println!("task 1 in lock")); }); spawn(proc() { with_lock(&spinlock_clone, || println!("task 2 in lock")); }); } fn with_lock(spinlock: &Arc<AtomicBool>, f: || -> ()) { // CAS loop until we are able to replace `false` with `true` while spinlock.compare_and_swap(false, true, SeqCst) == false { // Since tasks may not be preemptive (if they are green threads) // yield to the scheduler to let the other task run. Low level // concurrent code needs to take into account Rust's two threading // models. deschedule(); } // Now we have the spinlock f(); // Release the lock spinlock.store(false, SeqCst); }use std::sync::Arc; use std::sync::atomics::{AtomicBool, SeqCst}; use std::task::deschedule; fn main() { let spinlock = Arc::new(AtomicBool::new(false)); let spinlock_clone = spinlock.clone(); spawn(proc() { with_lock(&spinlock, || println!("task 1 in lock")); }); spawn(proc() { with_lock(&spinlock_clone, || println!("task 2 in lock")); }); } fn with_lock(spinlock: &Arc<AtomicBool>, f: || -> ()) { // CAS loop until we are able to replace `false` with `true` while spinlock.compare_and_swap(false, true, SeqCst) == false { // Since tasks may not be preemptive (if they are green threads) // yield to the scheduler to let the other task run. Low level // concurrent code needs to take into account Rust's two threading // models. deschedule(); } // Now we have the spinlock f(); // Release the lock spinlock.store(false, SeqCst); }
fn fetch_and(&self, val: bool, order: Ordering) -> bool
A logical "and" operation
Performs a logical "and" operation on the current value and the
argument val, and sets the new value to the result.
Returns the previous value.
Examples
fn main() { use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_and(false, SeqCst)); assert_eq!(false, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_and(true, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_and(false, SeqCst)); assert_eq!(false, foo.load(SeqCst)); }use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_and(false, SeqCst)); assert_eq!(false, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_and(true, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_and(false, SeqCst)); assert_eq!(false, foo.load(SeqCst));
fn fetch_nand(&self, val: bool, order: Ordering) -> bool
A logical "nand" operation
Performs a logical "nand" operation on the current value and the
argument val, and sets the new value to the result.
Returns the previous value.
Examples
fn main() { use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_nand(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_nand(true, SeqCst)); assert_eq!(0, foo.load(SeqCst) as int); assert_eq!(false, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_nand(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); }use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_nand(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_nand(true, SeqCst)); assert_eq!(0, foo.load(SeqCst) as int); assert_eq!(false, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_nand(false, SeqCst)); assert_eq!(true, foo.load(SeqCst));
fn fetch_or(&self, val: bool, order: Ordering) -> bool
A logical "or" operation
Performs a logical "or" operation on the current value and the
argument val, and sets the new value to the result.
Returns the previous value.
Examples
fn main() { use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_or(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_or(true, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_or(false, SeqCst)); assert_eq!(false, foo.load(SeqCst)); }use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_or(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_or(true, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_or(false, SeqCst)); assert_eq!(false, foo.load(SeqCst));
fn fetch_xor(&self, val: bool, order: Ordering) -> bool
A logical "xor" operation
Performs a logical "xor" operation on the current value and the
argument val, and sets the new value to the result.
Returns the previous value.
Examples
fn main() { use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_xor(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_xor(true, SeqCst)); assert_eq!(false, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_xor(false, SeqCst)); assert_eq!(false, foo.load(SeqCst)); }use std::sync::atomics::{AtomicBool, SeqCst}; let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_xor(false, SeqCst)); assert_eq!(true, foo.load(SeqCst)); let foo = AtomicBool::new(true); assert_eq!(true, foo.fetch_xor(true, SeqCst)); assert_eq!(false, foo.load(SeqCst)); let foo = AtomicBool::new(false); assert_eq!(false, foo.fetch_xor(false, SeqCst)); assert_eq!(false, foo.load(SeqCst));