Expand description
Atomic types
Atomic types provide primitive shared-memory communication between threads, and are the building blocks of other concurrent types.
This module defines atomic versions of a select number of primitive
types, including AtomicBool, AtomicIsize, and AtomicUsize.
Atomic types present operations that, when used correctly, synchronize
updates between threads.
MSP430 note: All atomic operations in this crate have SeqCst
memory ordering.
Atomic variables are safe to share between threads (they implement Sync)
but they do not themselves provide the mechanism for sharing and follow the
threading model
of rust.
Most atomic types may be stored in static variables, initialized using
the provided static initializers like ATOMIC_BOOL_INIT. Atomic statics
are often used for lazy global initialization.
§Examples
A simple spinlock:
use msp430_atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
use std::thread;
// Initialize SPINLOCK to 0
static SPINLOCK: AtomicUsize = ATOMIC_USIZE_INIT;
fn main() {
let thread = thread::spawn(move|| {
SPINLOCK.store(1);
});
// Wait for the other thread to release the lock
while SPINLOCK.load() == 0 {}
if let Err(panic) = thread.join() {
println!("Thread had an error: {:?}", panic);
}
}Structs§
- Atomic
Bool - A boolean type which can be safely shared between threads.
- Atomic
I8 - An integer type which can be safely shared between threads.
- Atomic
I16 - An integer type which can be safely shared between threads.
- Atomic
Isize - An integer type which can be safely shared between threads.
- Atomic
Ptr - A raw pointer type which can be safely shared between threads.
- Atomic
U8 - An integer type which can be safely shared between threads.
- Atomic
U16 - An integer type which can be safely shared between threads.
- Atomic
Usize - An integer type which can be safely shared between threads.
Constants§
- ATOMIC_
BOOL_ INIT - An
AtomicBoolinitialized tofalse. - ATOMIC_
I8_ INIT - An atomic integer initialized to
0. - ATOMIC_
I16_ INIT - An atomic integer initialized to
0. - ATOMIC_
ISIZE_ INIT - An atomic integer initialized to
0. - ATOMIC_
U8_ INIT - An atomic integer initialized to
0. - ATOMIC_
U16_ INIT - An atomic integer initialized to
0. - ATOMIC_
USIZE_ INIT - An atomic integer initialized to
0.
Traits§
- Atomic
Operations - Atomic arithmetic and bitwise operations implemented for numerical types. Each operation is implemented with a single assembly instruction.