embassy_time_queue_driver/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![warn(missing_docs)]
4
5//! ## Implementing a timer queue
6//!
7//! - Define a struct `MyTimerQueue`
8//! - Implement [`TimerQueue`] for it
9//! - Register it as the global timer queue with [`timer_queue_impl`](crate::timer_queue_impl).
10//!
11//! ## Example
12//!
13//! ```
14//! use core::task::Waker;
15//!
16//! use embassy_time::Instant;
17//! use embassy_time::queue::{TimerQueue};
18//!
19//! struct MyTimerQueue{}; // not public!
20//!
21//! impl TimerQueue for MyTimerQueue {
22//!     fn schedule_wake(&'static self, at: u64, waker: &Waker) {
23//!         todo!()
24//!     }
25//! }
26//!
27//! embassy_time_queue_driver::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{});
28//! ```
29use core::task::Waker;
30
31/// Timer queue
32pub trait TimerQueue {
33    /// Schedules a waker in the queue to be awoken at moment `at`.
34    /// If this moment is in the past, the waker might be awoken immediately.
35    fn schedule_wake(&'static self, at: u64, waker: &Waker);
36}
37
38extern "Rust" {
39    fn _embassy_time_schedule_wake(at: u64, waker: &Waker);
40}
41
42/// Schedule the given waker to be woken at `at`.
43pub fn schedule_wake(at: u64, waker: &Waker) {
44    unsafe { _embassy_time_schedule_wake(at, waker) }
45}
46
47/// Set the TimerQueue implementation.
48///
49/// See the module documentation for an example.
50#[macro_export]
51macro_rules! timer_queue_impl {
52    (static $name:ident: $t: ty = $val:expr) => {
53        static $name: $t = $val;
54
55        #[no_mangle]
56        fn _embassy_time_schedule_wake(at: u64, waker: &core::task::Waker) {
57            <$t as $crate::TimerQueue>::schedule_wake(&$name, at, waker);
58        }
59    };
60}