1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use crate::thr::Thread;
use core::cell::Cell;
static mut CURRENT: usize = 0;
pub struct PreemptedCell(Cell<usize>);
impl PreemptedCell {
pub const fn new() -> Self {
Self(Cell::new(0))
}
}
#[inline]
pub fn local<T: Thread>() -> &'static T::Local {
unsafe { (*T::first().add(CURRENT)).local() }
}
pub(crate) unsafe fn preempt(preempted: &PreemptedCell, thr_num: usize, f: impl FnOnce()) {
preempted.0.set(CURRENT);
CURRENT = thr_num;
f();
CURRENT = preempted.0.get();
}