pub struct AtomicBool(AtomicBool);Expand description
A boolean type which can be safely shared between threads.
This type has the same size, alignment, and bit validity as a [bool].
Tuple Fields§
§0: AtomicBoolImplementations§
Source§impl AtomicBool
impl AtomicBool
Sourcepub const fn new(v: bool) -> AtomicBool
pub const fn new(v: bool) -> AtomicBool
Creates a new AtomicBool.
§Examples
use KeOS::sync::atomic::AtomicBool;
let atomic_true = AtomicBool::new(true);
let atomic_false = AtomicBool::new(false);Sourcepub fn get_mut(&mut self) -> &mut bool
pub fn get_mut(&mut self) -> &mut bool
Returns a mutable reference to the underlying [bool].
This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.
§Examples
use keos::atomic::AtomicBool;
let mut some_bool = AtomicBool::new(true);
assert_eq!(*some_bool.get_mut(), true);
*some_bool.get_mut() = false;
assert_eq!(some_bool.load(), false);Sourcepub const fn into_inner(self) -> bool
pub const fn into_inner(self) -> bool
Consumes the atomic and returns the contained value.
This is safe because passing self by value guarantees that no other
threads are concurrently accessing the atomic data.
§Examples
use keos::atomic::AtomicBool;
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.into_inner(), true);Sourcepub fn load(&self) -> bool
pub fn load(&self) -> bool
Loads a value from the bool.
§Examples
use keos::atomic::AtomicBool;
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.load(), true);Sourcepub fn store(&self, val: bool)
pub fn store(&self, val: bool)
Stores a value into the bool.
§Examples
use keos::atomic::AtomicBool;
let some_bool = AtomicBool::new(true);
some_bool.store(false);
assert_eq!(some_bool.load(), false);Sourcepub fn swap(&self, val: bool) -> bool
pub fn swap(&self, val: bool) -> bool
Stores a value into the bool, returning the previous value.
§Examples
use keos::atomic::AtomicBool;
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.swap(false), true);
assert_eq!(some_bool.load(), false);Sourcepub fn compare_exchange(&self, current: bool, new: bool) -> Result<bool, bool>
pub fn compare_exchange(&self, current: bool, new: bool) -> Result<bool, bool>
Stores a value into the [bool] if the current value is the same as the
current value.
The return value is a result indicating whether the new value was
written and containing the previous value. On success this value is
guaranteed to be equal to current.
§Examples
use std::sync::atomic::AtomicBool;
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.compare_exchange(true, false), Ok(true));
assert_eq!(some_bool.load(), false);
assert_eq!(some_bool.compare_exchange(true, true), Err(false));
assert_eq!(some_bool.load(), false);Sourcepub fn fetch_and(&self, val: bool) -> bool
pub fn fetch_and(&self, val: bool) -> bool
Logical “and” with a boolean value.
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
use keos::atomic::AtomicBool;
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_and(false), true);
assert_eq!(foo.load(), false);
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_and(true), true);
assert_eq!(foo.load(), true);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_and(false), false);
assert_eq!(foo.load(), false);Sourcepub fn fetch_nand(&self, val: bool) -> bool
pub fn fetch_nand(&self, val: bool) -> bool
Logical “nand” with a boolean value.
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.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use keos::atomic::{AtomicBool, Ordering};
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_nand(false), true);
assert_eq!(foo.load(), true);
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_nand(true), true);
assert_eq!(foo.load() as usize, 0);
assert_eq!(foo.load(), false);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_nand(false), false);
assert_eq!(foo.load(), true);Sourcepub fn fetch_or(&self, val: bool) -> bool
pub fn fetch_or(&self, val: bool) -> bool
Logical “or” with a boolean value.
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.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use keos::atomic::{AtomicBool, Ordering};
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_or(false), true);
assert_eq!(foo.load(), true);
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_or(true), true);
assert_eq!(foo.load(), true);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_or(false), false);
assert_eq!(foo.load(), false);Sourcepub fn fetch_xor(&self, val: bool) -> bool
pub fn fetch_xor(&self, val: bool) -> bool
Logical “xor” with a boolean value.
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.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use keos::atomic::{AtomicBool, Ordering};
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_xor(false), true);
assert_eq!(foo.load(), true);
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_xor(true), true);
assert_eq!(foo.load(), false);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_xor(false), false);
assert_eq!(foo.load(), false);Sourcepub fn fetch_not(&self) -> bool
pub fn fetch_not(&self) -> bool
Logical “not” with a boolean value.
Performs a logical “not” operation on the current value, and sets the new value to the result.
Returns the previous value.
Note: This method is only available on platforms that support atomic
operations on u8.
§Examples
use keos::atomic::AtomicBool;
let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_not(), true);
assert_eq!(foo.load(), false);
let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_not(), false);
assert_eq!(foo.load(), true);Sourcepub fn fetch_update<F>(&self, f: F) -> Result<bool, bool>where
F: FnMut(bool) -> Option<bool>,
pub fn fetch_update<F>(&self, f: F) -> Result<bool, bool>where
F: FnMut(bool) -> Option<bool>,
Fetches the value, and applies a function to it that returns an optional
new value. Returns a Result of Ok(previous_value) if the function
returned Some(_), else Err(previous_value).
Note: This may call the function multiple times if the value has been
changed from other threads in the meantime, as long as the function
returns Some(_), but the function will have been applied only once to
the stored value.
§Examples
use keos::atomic::AtomicBool;
let x = AtomicBool::new(false);
assert_eq!(x.fetch_update(|_| None), Err(false));
assert_eq!(x.fetch_update(|x| Some(!x)), Ok(false));
assert_eq!(x.fetch_update(|x| Some(!x)), Ok(true));
assert_eq!(x.load(), false);