Macro cpu_local

Source
macro_rules! cpu_local {
    ($( $(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; )*) => { ... };
}
Expand description

Defines a statically-allocated CPU-local variable.

The accessors of the CPU-local variables are defined with CpuLocal.

You can get the reference to the inner object on one CPU by calling CpuLocal::get_on_cpu. Also if you intend to access the inner object on the current CPU, you can use CpuLocal::get_with. The latter accessors can be used even if the inner object is not Sync.

ยงExample

use ostd::{cpu_local, cpu::PinCurrentCpu, task::disable_preempt, trap};
use core::{sync::atomic::{AtomicU32, Ordering}, cell::Cell};

cpu_local! {
    static FOO: AtomicU32 = AtomicU32::new(1);
    pub static BAR: Cell<usize> = Cell::new(2);
}

fn not_an_atomic_function() {
    let preempt_guard = disable_preempt();
    let ref_of_foo = FOO.get_on_cpu(preempt_guard.current_cpu());
    let val_of_foo = ref_of_foo.load(Ordering::Relaxed);
    println!("FOO VAL: {}", val_of_foo);

    let irq_guard = trap::irq::disable_local();
    let bar_guard = BAR.get_with(&irq_guard);
    let val_of_bar = bar_guard.get();
    println!("BAR VAL: {}", val_of_bar);
}