maitake/time/timer/
global.rs

1use super::{Timer, TimerError};
2use core::{
3    ptr,
4    sync::atomic::{AtomicPtr, Ordering},
5};
6
7static GLOBAL_TIMER: AtomicPtr<Timer> = AtomicPtr::new(ptr::null_mut());
8
9/// Errors returned by [`set_global_timer`].
10#[derive(Debug)]
11pub struct AlreadyInitialized(());
12
13/// Sets a [`Timer`] as the [global default timer].
14///
15/// This function must be called in order for the [`sleep`] and [`timeout`] free
16/// functions to be used.
17///
18/// The global timer can only be set a single time. Once the global timer is
19/// initialized, subsequent calls to this function will return an
20/// [`AlreadyInitialized`] error.
21///
22/// [`sleep`]: crate::time::sleep()
23/// [`timeout`]: crate::time::timeout()
24/// [global default timer]: crate::time#global-timers
25pub fn set_global_timer(timer: &'static Timer) -> Result<(), AlreadyInitialized> {
26    GLOBAL_TIMER
27        .compare_exchange(
28            ptr::null_mut(),
29            timer as *const _ as *mut _,
30            Ordering::AcqRel,
31            Ordering::Acquire,
32        )
33        .map_err(|_| AlreadyInitialized(()))
34        .map(|_| ())
35}
36
37#[inline(always)]
38pub(in crate::time) fn default() -> Result<&'static Timer, TimerError> {
39    let ptr = GLOBAL_TIMER.load(Ordering::Acquire);
40    ptr::NonNull::new(ptr)
41        .ok_or(TimerError::NoGlobalTimer)
42        .map(|ptr| unsafe {
43            // safety: we have just null-checked this pointer, so we know it's not
44            // null. and it's safe to convert it to an `&'static Timer`, because we
45            // know that the pointer stored in the atomic *came* from an `&'static
46            // Timer` (as it's only set in `set_global_timer`).
47            ptr.as_ref()
48        })
49}