hazard3_executor_timer_queue/lib.rs
1//! Timer queue item for hazard3-executor integrated timer queues
2//!
3//! `hazard3-executor` provides the memory needed to implement integrated timer queues. This crate
4//! exists to separate that memory from `hazard3-executor` itself, to decouple the timer queue's
5//! release cycle from `hazard3-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 [`TimerQueueItemProvider`] trait, which must be implemented (by `hazard3-executor`,
11//! most likely) to return the `TimerQueueItem` associated with a given waker. It is a
12//! [unitrait]: the implementation is registered globally with
13//! [`timer_queue_item_provider_impl`] and resolved at link time.
14//!
15//! As a queue implementor, you will need to choose one of the `timer-item-size-N-words` features to
16//! select a queue item size. You can then define your own item type, which must be
17//! `#[repr(align(8))]` (or less) and must fit into the size you selected.
18//!
19//! You can access the `TimerQueueItem` from a `Waker` using the [`from_hazard3_waker`](TimerQueueItem::from_hazard3_waker)
20//! method. You can then use the [`as_ref`](TimerQueueItem::as_ref) and [`as_mut`](TimerQueueItem::as_mut)
21//! methods to reinterpret the data stored in the item as your custom item type.
22#![no_std]
23
24use core::task::Waker;
25
26hazard3_unitrait::unitrait! {
27 /// Provider of the [`TimerQueueItem`] associated with a waker.
28 ///
29 /// This trait is implemented by the executor (`hazard3-executor`, most likely), which owns
30 /// the storage for timer queue items.
31 #[symbol_prefix = "__embassy_time_queue"]
32 pub trait TimerQueueItemProvider {
33 /// Retrieves the `TimerQueueItem` reference that belongs to the task of the waker.
34 ///
35 /// Panics if called with a non-Hazard3 waker.
36 ///
37 /// # Safety
38 ///
39 /// The caller must ensure they are not violating Rust's aliasing rules - it is not allowed
40 /// to use this method to create multiple mutable references to the same `TimerQueueItem` at
41 /// the same time.
42 ///
43 /// This function must only be called in the context of a timer queue implementation.
44 unsafe fn item_from_waker(waker: &core::task::Waker) -> &'static mut TimerQueueItem;
45
46 /// Like [`item_from_waker`](TimerQueueItemProvider::item_from_waker), but returns `None`
47 /// instead of panicking if the waker is not a Hazard3 waker.
48 ///
49 /// # Safety
50 ///
51 /// Same as [`item_from_waker`](TimerQueueItemProvider::item_from_waker).
52 #[symbol = "__try_embassy_time_queue_item_from_waker"]
53 unsafe fn try_item_from_waker(waker: &core::task::Waker) -> Option<&'static mut TimerQueueItem>;
54 }
55
56 /// The global [`TimerQueueItemProvider`] implementation.
57 pub struct TimerQueueItemProviderImpl;
58
59 /// Register a type as the global [`TimerQueueItemProvider`] implementation.
60 ///
61 /// This must be done exactly once in the crate tree, by the executor.
62 macro timer_queue_item_provider_impl(path = $crate);
63}
64
65const ITEM_WORDS: usize = if cfg!(feature = "timer-item-size-8-words") {
66 8
67} else if cfg!(feature = "timer-item-size-6-words") {
68 6
69} else if cfg!(feature = "timer-item-size-4-words") {
70 4
71} else {
72 0
73};
74
75/// The timer queue item provided by the executor.
76///
77/// This type is opaque, it only provides the raw storage for a queue item. The queue implementation
78/// is responsible for reinterpreting the contents of the item using [`TimerQueueItem::as_ref`] and
79/// [`TimerQueueItem::as_mut`].
80#[repr(align(8))]
81pub struct TimerQueueItem {
82 data: [usize; ITEM_WORDS],
83}
84
85impl TimerQueueItem {
86 /// Creates a new, zero-initialized `TimerQueueItem`.
87 pub const fn new() -> Self {
88 Self { data: [0; ITEM_WORDS] }
89 }
90
91 /// Retrieves the `TimerQueueItem` reference that belongs to the task of the waker.
92 ///
93 /// Panics if called with a non-Hazard3 waker.
94 ///
95 /// # Safety
96 ///
97 /// The caller must ensure they are not violating Rust's aliasing rules - it is not allowed
98 /// to use this method to create multiple mutable references to the same `TimerQueueItem` at
99 /// the same time.
100 ///
101 /// This function must only be called in the context of a timer queue implementation.
102 pub unsafe fn from_hazard3_waker(waker: &Waker) -> &'static mut Self {
103 unsafe { TimerQueueItemProviderImpl::item_from_waker(waker) }
104 }
105
106 /// Access the data as a reference to a type `T`.
107 ///
108 /// Safety:
109 ///
110 /// - The type must be valid when zero-initialized.
111 /// - The timer queue should only be interpreted as a single type `T` during its lifetime.
112 pub unsafe fn as_ref<T>(&self) -> &T {
113 const { validate::<T>() }
114 unsafe { &*(self.data.as_ptr() as *const T) }
115 }
116
117 /// Access the data as a reference to a type `T`.
118 ///
119 /// Safety:
120 ///
121 /// - The type must be valid when zero-initialized.
122 /// - The timer queue should only be interpreted as a single type `T` during its lifetime.
123 pub unsafe fn as_mut<T>(&self) -> &mut T {
124 const { validate::<T>() }
125 unsafe { &mut *(self.data.as_ptr() as *mut T) }
126 }
127}
128
129const fn validate<T>() {
130 const {
131 assert!(
132 core::mem::size_of::<TimerQueueItem>() >= core::mem::size_of::<T>(),
133 "hazard3-executor-timer-queue item size is smaller than the requested type. Select a larger timer-item-size-N-words feature."
134 );
135 assert!(
136 core::mem::align_of::<TimerQueueItem>() >= core::mem::align_of::<T>(),
137 "the alignment of the requested type is greater than 8"
138 );
139 }
140}