From f0606da9adc8032cc92c06c0661b385742459fc8 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 11 Jan 2024 22:47:05 +0100 Subject: time: split queue driver too, don't reexport drivers. --- embassy-time-queue-driver/src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 embassy-time-queue-driver/src/lib.rs (limited to 'embassy-time-queue-driver/src/lib.rs') diff --git a/embassy-time-queue-driver/src/lib.rs b/embassy-time-queue-driver/src/lib.rs new file mode 100644 index 000000000..50736e8c7 --- /dev/null +++ b/embassy-time-queue-driver/src/lib.rs @@ -0,0 +1,60 @@ +#![no_std] +#![doc = include_str!("../README.md")] +#![warn(missing_docs)] + +//! ## Implementing a timer queue +//! +//! - Define a struct `MyTimerQueue` +//! - Implement [`TimerQueue`] for it +//! - Register it as the global timer queue with [`timer_queue_impl`](crate::timer_queue_impl). +//! +//! ## Example +//! +//! ``` +//! use core::task::Waker; +//! +//! use embassy_time::Instant; +//! use embassy_time::queue::{TimerQueue}; +//! +//! struct MyTimerQueue{}; // not public! +//! +//! impl TimerQueue for MyTimerQueue { +//! fn schedule_wake(&'static self, at: u64, waker: &Waker) { +//! todo!() +//! } +//! } +//! +//! embassy_time_queue_driver::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{}); +//! ``` +use core::task::Waker; + +/// 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: u64, waker: &Waker); +} + +extern "Rust" { + fn _embassy_time_schedule_wake(at: u64, waker: &Waker); +} + +/// Schedule the given waker to be woken at `at`. +pub fn schedule_wake(at: u64, waker: &Waker) { + unsafe { _embassy_time_schedule_wake(at, waker) } +} + +/// Set the TimerQueue implementation. +/// +/// See the module documentation for an example. +#[macro_export] +macro_rules! timer_queue_impl { + (static $name:ident: $t: ty = $val:expr) => { + static $name: $t = $val; + + #[no_mangle] + fn _embassy_time_schedule_wake(at: u64, waker: &core::task::Waker) { + <$t as $crate::TimerQueue>::schedule_wake(&$name, at, waker); + } + }; +} -- cgit