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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use super::{Timer, TimerError};
use core::{
    ptr,
    sync::atomic::{AtomicPtr, Ordering},
};

static GLOBAL_TIMER: AtomicPtr<Timer> = AtomicPtr::new(ptr::null_mut());

/// Errors returned by [`set_global_timer`].
#[derive(Debug)]
pub struct AlreadyInitialized(());

/// Sets a [`Timer`] as the [global default timer].
///
/// This function must be called in order for the [`sleep`] and [`timeout`] free
/// functions to be used.
///
/// The global timer can only be set a single time. Once the global timer is
/// initialized, subsequent calls to this function will return an
/// [`AlreadyInitialized`] error.
///
/// [`sleep`]: crate::time::sleep()
/// [`timeout`]: crate::time::timeout()
/// [global default timer]: crate::time#global-timers
pub fn set_global_timer(timer: &'static Timer) -> Result<(), AlreadyInitialized> {
    GLOBAL_TIMER
        .compare_exchange(
            ptr::null_mut(),
            timer as *const _ as *mut _,
            Ordering::AcqRel,
            Ordering::Acquire,
        )
        .map_err(|_| AlreadyInitialized(()))
        .map(|_| ())
}

#[inline(always)]
pub(in crate::time) fn default() -> Result<&'static Timer, TimerError> {
    let ptr = GLOBAL_TIMER.load(Ordering::Acquire);
    ptr::NonNull::new(ptr)
        .ok_or(TimerError::NoGlobalTimer)
        .map(|ptr| unsafe {
            // safety: we have just null-checked this pointer, so we know it's not
            // null. and it's safe to convert it to an `&'static Timer`, because we
            // know that the pointer stored in the atomic *came* from an `&'static
            // Timer` (as it's only set in `set_global_timer`).
            ptr.as_ref()
        })
}