diff options
Diffstat (limited to 'embassy-executor-timer-queue/src')
| -rw-r--r-- | embassy-executor-timer-queue/src/lib.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/embassy-executor-timer-queue/src/lib.rs b/embassy-executor-timer-queue/src/lib.rs new file mode 100644 index 000000000..456ccaec3 --- /dev/null +++ b/embassy-executor-timer-queue/src/lib.rs | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | //! Timer queue item for embassy-executor integrated timer queues | ||
| 2 | //! | ||
| 3 | //! `embassy-executor` provides the memory needed to implement integrated timer queues. This crate | ||
| 4 | //! exists to separate that memory from `embassy-executor` itself, to decouple the timer queue's | ||
| 5 | //! release cycle from `embassy-executor`. | ||
| 6 | //! | ||
| 7 | //! This crate contains two things: | ||
| 8 | //! - [`TimerQueueItem`]: The item type that can be requested from the executor. The size of this | ||
| 9 | //! type can be configured using the `timer-item-size-N-words` Cargo features. | ||
| 10 | //! - The expectation that `extern "Rust" fn __embassy_time_queue_item_from_waker(waker: &Waker) -> &mut TimerQueueItem` | ||
| 11 | //! is implemented (by `embassy-executor`, most likely). This function must return a mutable | ||
| 12 | //! reference to the `TimerQueueItem` associated with the given waker. | ||
| 13 | //! | ||
| 14 | //! As a queue implementor, you will need to choose one of the `timer-item-size-N-words` features to | ||
| 15 | //! select a queue item size. You can then define your own item type, which must be | ||
| 16 | //! `#[repr(align(8))]` (or less) and must fit into the size you selected. | ||
| 17 | //! | ||
| 18 | //! You can access the `TimerQueueItem` from a `Waker` using the [`from_embassy_waker`](TimerQueueItem::from_embassy_waker) | ||
| 19 | //! method. You can then use the [`as_ref`](TimerQueueItem::as_ref) and [`as_mut`](TimerQueueItem::as_mut) | ||
| 20 | //! methods to reinterpret the data stored in the item as your custom item type. | ||
| 21 | #![no_std] | ||
| 22 | |||
| 23 | use core::task::Waker; | ||
| 24 | |||
| 25 | const ITEM_SIZE: usize = if cfg!(feature = "timer-item-size-8-words") { | ||
| 26 | 8 | ||
| 27 | } else if cfg!(feature = "timer-item-size-6-words") { | ||
| 28 | 6 | ||
| 29 | } else if cfg!(feature = "timer-item-size-4-words") { | ||
| 30 | 4 | ||
| 31 | } else { | ||
| 32 | 0 | ||
| 33 | }; | ||
| 34 | |||
| 35 | /// The timer queue item provided by the executor. | ||
| 36 | /// | ||
| 37 | /// This type is opaque, it only provides the raw storage for a queue item. The queue implementation | ||
| 38 | /// is responsible for reinterpreting the contents of the item using [`TimerQueueItem::as_ref`] and | ||
| 39 | /// [`TimerQueueItem::as_mut`]. | ||
| 40 | #[repr(align(8))] | ||
| 41 | pub struct TimerQueueItem { | ||
| 42 | data: [usize; ITEM_SIZE], | ||
| 43 | } | ||
| 44 | |||
| 45 | impl TimerQueueItem { | ||
| 46 | /// Creates a new, zero-initialized `TimerQueueItem`. | ||
| 47 | pub const fn new() -> Self { | ||
| 48 | Self { data: [0; ITEM_SIZE] } | ||
| 49 | } | ||
| 50 | |||
| 51 | /// Retrieves the `TimerQueueItem` reference that belongs to the task of the waker. | ||
| 52 | /// | ||
| 53 | /// Panics if called with a non-embassy waker. | ||
| 54 | /// | ||
| 55 | /// # Safety | ||
| 56 | /// | ||
| 57 | /// The caller must ensure they are not violating Rust's aliasing rules - it is not allowed | ||
| 58 | /// to use this method to create multiple mutable references to the same `TimerQueueItem` at | ||
| 59 | /// the same time. | ||
| 60 | /// | ||
| 61 | /// This function must only be called in the context of a timer queue implementation. | ||
| 62 | pub unsafe fn from_embassy_waker(waker: &Waker) -> &'static mut Self { | ||
| 63 | unsafe extern "Rust" { | ||
| 64 | // Waker -> TimerQueueItem, validates that Waker is an embassy Waker. | ||
| 65 | fn __embassy_time_queue_item_from_waker(waker: &Waker) -> &'static mut TimerQueueItem; | ||
| 66 | } | ||
| 67 | unsafe { __embassy_time_queue_item_from_waker(waker) } | ||
| 68 | } | ||
| 69 | |||
| 70 | /// Access the data as a reference to a type `T`. | ||
| 71 | /// | ||
| 72 | /// Safety: | ||
| 73 | /// | ||
| 74 | /// - The type must be valid when zero-initialized. | ||
| 75 | /// - The timer queue should only be interpreted as a single type `T` during its lifetime. | ||
| 76 | pub unsafe fn as_ref<T>(&self) -> &T { | ||
| 77 | const { | ||
| 78 | assert!(core::mem::size_of::<Self>() >= core::mem::size_of::<T>()); | ||
| 79 | assert!(core::mem::align_of::<Self>() >= core::mem::align_of::<T>()); | ||
| 80 | } | ||
| 81 | unsafe { &*(self.data.as_ptr() as *const T) } | ||
| 82 | } | ||
| 83 | |||
| 84 | /// Access the data as a reference to a type `T`. | ||
| 85 | /// | ||
| 86 | /// Safety: | ||
| 87 | /// | ||
| 88 | /// - The type must be valid when zero-initialized. | ||
| 89 | /// - The timer queue should only be interpreted as a single type `T` during its lifetime. | ||
| 90 | pub unsafe fn as_mut<T>(&self) -> &mut T { | ||
| 91 | const { | ||
| 92 | assert!(core::mem::size_of::<Self>() >= core::mem::size_of::<T>()); | ||
| 93 | assert!(core::mem::align_of::<Self>() >= core::mem::align_of::<T>()); | ||
| 94 | } | ||
| 95 | unsafe { &mut *(self.data.as_ptr() as *mut T) } | ||
| 96 | } | ||
| 97 | } | ||
