Crate msp430_atomic
source ·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
Constants
AtomicBool
initialized to false
.0
.0
.0
.0
.0
.0
.