embassy/embassy-time/src/queue.rs

59 lines
1.7 KiB
Rust
Raw Normal View History

2022-09-26 12:46:15 +02:00
//! Timer queue implementation
//!
2022-09-26 12:46:15 +02:00
//! This module defines the interface a timer queue needs to implement to power the `embassy_time` module.
//!
2022-09-26 12:46:15 +02:00
//! # Implementing a timer queue
//!
2022-09-26 12:46:15 +02:00
//! - Define a struct `MyTimerQueue`
//! - Implement [`TimerQueue`] for it
//! - Register it as the global timer queue with [`timer_queue_impl`](crate::timer_queue_impl).
//!
2022-09-26 12:46:15 +02:00
//! # Linkage details
//!
2022-09-26 12:46:15 +02:00
//! Check the documentation of the [`driver`](crate::driver) module for more information.
//!
2022-09-26 12:46:15 +02:00
//! Similarly to driver, if there is none or multiple timer queues in the crate tree, linking will fail.
//!
//! # Example
//!
2022-09-26 12:46:15 +02:00
//! ```
//! use core::task::Waker;
//!
//! use embassy_time::Instant;
//! use embassy_time::queue::{TimerQueue};
//!
2022-09-26 12:46:15 +02:00
//! struct MyTimerQueue{}; // not public!
//! embassy_time::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{});
//!
2022-09-26 12:46:15 +02:00
//! impl TimerQueue for MyTimerQueue {
//! fn schedule_wake(&'static self, at: Instant, waker: &Waker) {
//! todo!()
//! }
//! }
//! ```
2022-09-06 20:39:23 +02:00
use core::task::Waker;
use crate::Instant;
2022-09-26 12:46:15 +02:00
/// Timer queue
pub trait TimerQueue {
/// Schedules a waker in the queue to be awoken at moment `at`.
/// If this moment is in the past, the waker might be awoken immediately.
fn schedule_wake(&'static self, at: Instant, waker: &Waker);
2022-09-06 20:39:23 +02:00
}
2022-09-26 12:46:15 +02:00
/// Set the TimerQueue implementation.
///
/// See the module documentation for an example.
2022-09-06 20:39:23 +02:00
#[macro_export]
2022-09-26 12:46:15 +02:00
macro_rules! timer_queue_impl {
2022-09-06 20:39:23 +02:00
(static $name:ident: $t: ty = $val:expr) => {
static $name: $t = $val;
#[no_mangle]
fn _embassy_time_schedule_wake(at: $crate::Instant, waker: &core::task::Waker) {
2022-09-26 12:46:15 +02:00
<$t as $crate::queue::TimerQueue>::schedule_wake(&$name, at, waker);
2022-09-06 20:39:23 +02:00
}
};
}