macro_rules! cpu_local_cell {
($( $(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; )*) => { ... };
}
Expand description
Defines an inner-mutable CPU-local variable.
The accessors of the CPU-local variables are defined with CpuLocalCell
.
It should be noted that if the interrupts or preemption is enabled, two operations on the same CPU-local cell variable may access different objects since the task may live on different CPUs.
ยงExample
use ostd::cpu_local_cell;
cpu_local_cell! {
static FOO: u32 = 1;
pub static BAR: *const usize = core::ptr::null();
}
fn not_an_atomic_function() {
let bar_var: usize = 1;
BAR.store(&bar_var as *const _);
// Note that the value of `BAR` here doesn't nessarily equal to the address
// of `bar_var`, since the task may be preempted and moved to another CPU.
// You can avoid this by disabling interrupts (and preemption, if needed).
println!("BAR VAL: {:?}", BAR.load());
let _irq_guard = ostd::trap::irq::disable_local_irq();
println!("1st FOO VAL: {:?}", FOO.load());
// No surprises here, the two accesses must result in the same value.
println!("2nd FOO VAL: {:?}", FOO.load());
}