diff options
| author | Dániel Buga <[email protected]> | 2024-12-16 18:51:09 +0100 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2024-12-16 20:36:59 +0100 |
| commit | c9f32b7e3667f29c4ab15d4dbab37acdb471d0ed (patch) | |
| tree | f58e3d23f9639b29278d68f1ad8885bfd1073e58 | |
| parent | 3c121e5425e0a1901c459d27e3e5929f86d0a206 (diff) | |
Attach payload to TimerQueueItem
| -rw-r--r-- | embassy-executor/Cargo.toml | 16 | ||||
| -rw-r--r-- | embassy-executor/src/raw/timer_queue.rs | 45 |
2 files changed, 61 insertions, 0 deletions
diff --git a/embassy-executor/Cargo.toml b/embassy-executor/Cargo.toml index 60fe7087a..2a64b9c83 100644 --- a/embassy-executor/Cargo.toml +++ b/embassy-executor/Cargo.toml | |||
| @@ -92,6 +92,22 @@ trace = [] | |||
| 92 | ## Enable support for rtos-trace framework | 92 | ## Enable support for rtos-trace framework |
| 93 | rtos-trace = ["dep:rtos-trace", "trace", "dep:embassy-time-driver"] | 93 | rtos-trace = ["dep:rtos-trace", "trace", "dep:embassy-time-driver"] |
| 94 | 94 | ||
| 95 | #! ### Timer Item Payload Size | ||
| 96 | #! Sets the size of the payload for timer items, allowing integrated timer implementors to store | ||
| 97 | #! additional data in the timer item. The payload field will be aligned to this value as well. | ||
| 98 | #! If these features are not defined, the timer item will contain no payload field. | ||
| 99 | |||
| 100 | _timer-item-payload = [] # A size was picked | ||
| 101 | |||
| 102 | ## 1 bytes | ||
| 103 | timer-item-payload-size-1 = ["_timer-item-payload"] | ||
| 104 | ## 2 bytes | ||
| 105 | timer-item-payload-size-2 = ["_timer-item-payload"] | ||
| 106 | ## 4 bytes | ||
| 107 | timer-item-payload-size-4 = ["_timer-item-payload"] | ||
| 108 | ## 8 bytes | ||
| 109 | timer-item-payload-size-8 = ["_timer-item-payload"] | ||
| 110 | |||
| 95 | #! ### Task Arena Size | 111 | #! ### Task Arena Size |
| 96 | #! Sets the [task arena](#task-arena) size. Necessary if you’re not using `nightly`. | 112 | #! Sets the [task arena](#task-arena) size. Necessary if you’re not using `nightly`. |
| 97 | #! | 113 | #! |
diff --git a/embassy-executor/src/raw/timer_queue.rs b/embassy-executor/src/raw/timer_queue.rs index 2ba0e00a9..c4dba18ff 100644 --- a/embassy-executor/src/raw/timer_queue.rs +++ b/embassy-executor/src/raw/timer_queue.rs | |||
| @@ -4,6 +4,45 @@ use core::cell::Cell; | |||
| 4 | 4 | ||
| 5 | use super::TaskRef; | 5 | use super::TaskRef; |
| 6 | 6 | ||
| 7 | #[cfg(feature = "_timer-item-payload")] | ||
| 8 | macro_rules! define_opaque { | ||
| 9 | ($size:tt) => { | ||
| 10 | /// An opaque data type. | ||
| 11 | #[repr(align($size))] | ||
| 12 | pub struct OpaqueData { | ||
| 13 | data: [u8; $size], | ||
| 14 | } | ||
| 15 | |||
| 16 | impl OpaqueData { | ||
| 17 | const fn new() -> Self { | ||
| 18 | Self { data: [0; $size] } | ||
| 19 | } | ||
| 20 | |||
| 21 | /// Access the data as a reference to a type `T`. | ||
| 22 | /// | ||
| 23 | /// Safety: | ||
| 24 | /// | ||
| 25 | /// The caller must ensure that the size of the type `T` is less than, or equal to | ||
| 26 | /// the size of the payload, and must ensure that the alignment of the type `T` is | ||
| 27 | /// less than, or equal to the alignment of the payload. | ||
| 28 | /// | ||
| 29 | /// The type must be valid when zero-initialized. | ||
| 30 | pub unsafe fn as_ref<T>(&self) -> &T { | ||
| 31 | &*(self.data.as_ptr() as *const T) | ||
| 32 | } | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | } | ||
| 36 | |||
| 37 | #[cfg(feature = "timer-item-payload-size-1")] | ||
| 38 | define_opaque!(1); | ||
| 39 | #[cfg(feature = "timer-item-payload-size-2")] | ||
| 40 | define_opaque!(2); | ||
| 41 | #[cfg(feature = "timer-item-payload-size-4")] | ||
| 42 | define_opaque!(4); | ||
| 43 | #[cfg(feature = "timer-item-payload-size-8")] | ||
| 44 | define_opaque!(8); | ||
| 45 | |||
| 7 | /// An item in the timer queue. | 46 | /// An item in the timer queue. |
| 8 | pub struct TimerQueueItem { | 47 | pub struct TimerQueueItem { |
| 9 | /// The next item in the queue. | 48 | /// The next item in the queue. |
| @@ -14,6 +53,10 @@ pub struct TimerQueueItem { | |||
| 14 | 53 | ||
| 15 | /// The time at which this item expires. | 54 | /// The time at which this item expires. |
| 16 | pub expires_at: Cell<u64>, | 55 | pub expires_at: Cell<u64>, |
| 56 | |||
| 57 | /// Some implementation-defined, zero-initialized piece of data. | ||
| 58 | #[cfg(feature = "_timer-item-payload")] | ||
| 59 | pub payload: OpaqueData, | ||
| 17 | } | 60 | } |
| 18 | 61 | ||
| 19 | unsafe impl Sync for TimerQueueItem {} | 62 | unsafe impl Sync for TimerQueueItem {} |
| @@ -23,6 +66,8 @@ impl TimerQueueItem { | |||
| 23 | Self { | 66 | Self { |
| 24 | next: Cell::new(None), | 67 | next: Cell::new(None), |
| 25 | expires_at: Cell::new(0), | 68 | expires_at: Cell::new(0), |
| 69 | #[cfg(feature = "_timer-item-payload")] | ||
| 70 | payload: OpaqueData::new(), | ||
| 26 | } | 71 | } |
| 27 | } | 72 | } |
| 28 | } | 73 | } |
