rdrive/
osal.rs

1use rdif_base::custom_type;
2use spin::RwLock;
3
4custom_type!(#[doc="Process ID"],Pid, usize, "{:?}");
5
6impl Pid {
7    pub const NOT_SET: usize = -1isize as usize;
8    pub const INVALID: usize = -2isize as usize;
9
10    pub fn is_not_set(&self) -> bool {
11        self.0 == Pid::NOT_SET
12    }
13
14    pub fn is_invalid(&self) -> bool {
15        self.0 == Pid::INVALID
16    }
17}
18
19pub trait Osal: Sync + Send + 'static {
20    /// Get the current process ID.
21    fn get_pid(&self) -> Pid;
22}
23
24struct OsalImplEmplty;
25
26impl Osal for OsalImplEmplty {
27    fn get_pid(&self) -> Pid {
28        Pid::INVALID.into()
29    }
30}
31
32static OSAL: RwLock<&dyn Osal> = RwLock::new(&OsalImplEmplty);
33
34pub fn set_osal(osal: &'static dyn Osal) {
35    let mut guard = OSAL.write();
36    *guard = osal;
37}
38
39pub(crate) fn get_pid() -> Pid {
40    OSAL.read().get_pid()
41}