Struct atomic_polyfill::AtomicBool
1.0.0 · #[repr(C, align(1))]pub struct AtomicBool { /* private fields */ }
Expand description
A boolean type which can be safely shared between threads.
This type has the same in-memory representation as a [bool
].
Note: This type is only available on platforms that support atomic
loads and stores of u8
.
Implementations§
§impl AtomicBool
impl AtomicBool
const: 1.24.0pub const fn new(v: bool) -> AtomicBool
pub const fn new(v: bool) -> AtomicBool
Creates a new AtomicBool
.
Examples
use std::sync::atomic::AtomicBool;
let atomic_true = AtomicBool::new(true);
let atomic_false = AtomicBool::new(false);
const: unstablepub unsafe fn from_ptr<'a>(ptr: *mut bool) -> &'a AtomicBool
🔬This is a nightly-only experimental API. (atomic_from_ptr
)
pub unsafe fn from_ptr<'a>(ptr: *mut bool) -> &'a AtomicBool
atomic_from_ptr
)Creates a new AtomicBool
from a pointer.
Examples
#![feature(atomic_from_ptr, pointer_is_aligned)]
use std::sync::atomic::{self, AtomicBool};
use std::mem::align_of;
// Get a pointer to an allocated value
let ptr: *mut bool = Box::into_raw(Box::new(false));
assert!(ptr.is_aligned_to(align_of::<AtomicBool>()));
{
// Create an atomic view of the allocated value
let atomic = unsafe { AtomicBool::from_ptr(ptr) };
// Use `atomic` for atomic operations, possibly share it with other threads
atomic.store(true, atomic::Ordering::Relaxed);
}
// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, true);
// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
Safety
ptr
must be aligned toalign_of::<AtomicBool>()
(note that on some platforms this can be bigger thanalign_of::<bool>()
).ptr
must be valid for both reads and writes for the whole lifetime'a
.- The value behind
ptr
must not be accessed through non-atomic operations for the whole lifetime'a
.
1.15.0pub 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 std::sync::atomic::{AtomicBool, Ordering};
let mut some_bool = AtomicBool::new(true);
assert_eq!(*some_bool.get_mut(), true);
*some_bool.get_mut() = false;
assert_eq!(some_bool.load(Ordering::SeqCst), false);
pub fn from_mut(v: &mut bool) -> &mut AtomicBool
🔬This is a nightly-only experimental API. (atomic_from_mut
)
pub fn from_mut(v: &mut bool) -> &mut AtomicBool
atomic_from_mut
)Get atomic access to a &mut bool
.
Examples
#![feature(atomic_from_mut)]
use std::sync::atomic::{AtomicBool, Ordering};
let mut some_bool = true;
let a = AtomicBool::from_mut(&mut some_bool);
a.store(false, Ordering::Relaxed);
assert_eq!(some_bool, false);
pub fn get_mut_slice(this: &mut [AtomicBool]) -> &mut [bool]
🔬This is a nightly-only experimental API. (atomic_from_mut
)
pub fn get_mut_slice(this: &mut [AtomicBool]) -> &mut [bool]
atomic_from_mut
)Get non-atomic access to a &mut [AtomicBool]
slice.
This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.
Examples
#![feature(atomic_from_mut, inline_const)]
use std::sync::atomic::{AtomicBool, Ordering};
let mut some_bools = [const { AtomicBool::new(false) }; 10];
let view: &mut [bool] = AtomicBool::get_mut_slice(&mut some_bools);
assert_eq!(view, [false; 10]);
view[..5].copy_from_slice(&[true; 5]);
std::thread::scope(|s| {
for t in &some_bools[..5] {
s.spawn(move || assert_eq!(t.load(Ordering::Relaxed), true));
}
for f in &some_bools[5..] {
s.spawn(move || assert_eq!(f.load(Ordering::Relaxed), false));
}
});
pub fn from_mut_slice(v: &mut [bool]) -> &mut [AtomicBool]
🔬This is a nightly-only experimental API. (atomic_from_mut
)
pub fn from_mut_slice(v: &mut [bool]) -> &mut [AtomicBool]
atomic_from_mut
)Get atomic access to a &mut [bool]
slice.
Examples
#![feature(atomic_from_mut)]
use std::sync::atomic::{AtomicBool, Ordering};
let mut some_bools = [false; 10];
let a = &*AtomicBool::from_mut_slice(&mut some_bools);
std::thread::scope(|s| {
for i in 0..a.len() {
s.spawn(move || a[i].store(true, Ordering::Relaxed));
}
});
assert_eq!(some_bools, [true; 10]);
1.15.0 (const: unstable)pub fn into_inner(self) -> bool
pub 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 std::sync::atomic::AtomicBool;
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.into_inner(), true);
pub fn load(&self, order: Ordering) -> bool
pub fn load(&self, order: Ordering) -> bool
Loads a value from the bool.
load
takes an Ordering
argument which describes the memory ordering
of this operation. Possible values are SeqCst
, Acquire
and Relaxed
.
Panics
Panics if order
is Release
or AcqRel
.
Examples
use std::sync::atomic::{AtomicBool, Ordering};
let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.load(Ordering::Relaxed), true);
pub fn store(&self, val: bool, order: Ordering)
pub fn store(&self, val: bool, order: Ordering)
Stores a value into the bool.
store
takes an Ordering
argument which describes the memory ordering
of this operation. Possible values are SeqCst
, Release
and Relaxed
.
Panics
Panics if order
is Acquire
or AcqRel
.
Examples
use std::sync::atomic::{AtomicBool, Ordering};
let some_bool = AtomicBool::new(true);
some_bool.store(false, Ordering::Relaxed);
assert_eq!(some_bool.load(Ordering::Relaxed), false);
1.70.0 (const: 1.70.0)pub const fn as_ptr(&self) -> *mut bool
pub const fn as_ptr(&self) -> *mut bool
Returns a mutable pointer to the underlying [bool
].
Doing non-atomic reads and writes on the resulting integer can be a data race.
This method is mostly useful for FFI, where the function signature may use
*mut bool
instead of &AtomicBool
.
Returning an *mut
pointer from a shared reference to this atomic is safe because the
atomic types work with interior mutability. All modifications of an atomic change the value
through a shared reference, and can do so safely as long as they use atomic operations. Any
use of the returned raw pointer requires an unsafe
block and still has to uphold the same
restriction: operations on it must be atomic.
Examples
use std::sync::atomic::AtomicBool;
extern "C" {
fn my_atomic_op(arg: *mut bool);
}
let mut atomic = AtomicBool::new(true);
unsafe {
my_atomic_op(atomic.as_ptr());
}
Trait Implementations§
1.3.0§impl Debug for AtomicBool
impl Debug for AtomicBool
§impl Default for AtomicBool
impl Default for AtomicBool
§fn default() -> AtomicBool
fn default() -> AtomicBool
Creates an AtomicBool
initialized to false
.
1.24.0§impl From<bool> for AtomicBool
impl From<bool> for AtomicBool
§fn from(b: bool) -> AtomicBool
fn from(b: bool) -> AtomicBool
Converts a bool
into an AtomicBool
.
Examples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")