diff options
| author | Frostie314159 <[email protected]> | 2024-03-31 20:48:05 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-31 20:48:05 +0200 |
| commit | 67c9cc2c4b886e6962ecdd6eff8794b14c1accdc (patch) | |
| tree | f176ab269949d26f48e04c950cebc5489bae8c56 /embassy-time-queue-driver/src/lib.rs | |
| parent | a2f9aa592ec61beb247065003016515f0d423c13 (diff) | |
| parent | 6634cc90bcd3eb25b64712688920f383584b2964 (diff) | |
Merge branch 'embassy-rs:main' into ticker_send_sync
Diffstat (limited to 'embassy-time-queue-driver/src/lib.rs')
| -rw-r--r-- | embassy-time-queue-driver/src/lib.rs | 60 |
1 files changed, 60 insertions, 0 deletions
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 @@ | |||
| 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 | //! ``` | ||
| 29 | use core::task::Waker; | ||
| 30 | |||
| 31 | /// Timer queue | ||
| 32 | pub 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 | |||
| 38 | extern "Rust" { | ||
| 39 | fn _embassy_time_schedule_wake(at: u64, waker: &Waker); | ||
| 40 | } | ||
| 41 | |||
| 42 | /// Schedule the given waker to be woken at `at`. | ||
| 43 | pub 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] | ||
| 51 | macro_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 | } | ||
