diff options
Diffstat (limited to 'embassy-time-queue-driver')
| -rw-r--r-- | embassy-time-queue-driver/Cargo.toml | 26 | ||||
| -rw-r--r-- | embassy-time-queue-driver/README.md | 8 | ||||
| -rw-r--r-- | embassy-time-queue-driver/build.rs | 1 | ||||
| -rw-r--r-- | embassy-time-queue-driver/src/lib.rs | 60 |
4 files changed, 95 insertions, 0 deletions
diff --git a/embassy-time-queue-driver/Cargo.toml b/embassy-time-queue-driver/Cargo.toml new file mode 100644 index 000000000..9ce9d79bb --- /dev/null +++ b/embassy-time-queue-driver/Cargo.toml | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | [package] | ||
| 2 | name = "embassy-time-queue-driver" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | description = "Timer queue driver trait for embassy-time" | ||
| 6 | repository = "https://github.com/embassy-rs/embassy" | ||
| 7 | documentation = "https://docs.embassy.dev/embassy-time-queue-driver" | ||
| 8 | readme = "README.md" | ||
| 9 | license = "MIT OR Apache-2.0" | ||
| 10 | categories = [ | ||
| 11 | "embedded", | ||
| 12 | "no-std", | ||
| 13 | "concurrency", | ||
| 14 | "asynchronous", | ||
| 15 | ] | ||
| 16 | |||
| 17 | # Prevent multiple copies of this crate in the same binary. | ||
| 18 | # Needed because different copies might get different tick rates, causing | ||
| 19 | # wrong delays if the time driver is using one copy and user code is using another. | ||
| 20 | # This is especially common when mixing crates from crates.io and git. | ||
| 21 | links = "embassy-time-queue" | ||
| 22 | |||
| 23 | [package.metadata.embassy_docs] | ||
| 24 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-time-queue-driver-v$VERSION/embassy-time-queue-driver/src/" | ||
| 25 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-time-queue-driver/src/" | ||
| 26 | target = "x86_64-unknown-linux-gnu" | ||
diff --git a/embassy-time-queue-driver/README.md b/embassy-time-queue-driver/README.md new file mode 100644 index 000000000..8852b0358 --- /dev/null +++ b/embassy-time-queue-driver/README.md | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # embassy-time-queue-driver | ||
| 2 | |||
| 3 | This crate contains the driver trait used by the [`embassy-time`](https://crates.io/crates/embassy-time) timer queue. | ||
| 4 | |||
| 5 | You should rarely need to use this crate directly. Only use it when implementing your own timer queue. | ||
| 6 | |||
| 7 | There is two timer queue implementations, one in `embassy-time` enabled by the `generic-queue` feature, and | ||
| 8 | another in `embassy-executor` enabled by the `integrated-timers` feature. | ||
diff --git a/embassy-time-queue-driver/build.rs b/embassy-time-queue-driver/build.rs new file mode 100644 index 000000000..f328e4d9d --- /dev/null +++ b/embassy-time-queue-driver/build.rs | |||
| @@ -0,0 +1 @@ | |||
| fn main() {} | |||
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 | } | ||
