externally_driven_async_rt/tasks/
reserve_task.rs1use core::{future::Future, pin::Pin, task::Poll};
2
3use crate::{
4 claimed::Claimed,
5 tasks::{TaskReservation, Tasks},
6};
7
8#[must_use = "futures do nothing unless you `.await` or poll them"]
10pub struct ReserveTask<'a, const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize>
11{
12 tasks: &'static Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
13 claimed: Claimed<'a>,
14 waiting: bool,
15}
16
17impl<'a, const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize>
18 ReserveTask<'a, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
19{
20 pub(crate) const fn new(
21 tasks: &'static Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
22 claimed: Claimed<'a>,
23 ) -> Self {
24 Self {
25 tasks,
26 claimed,
27 waiting: false,
28 }
29 }
30}
31
32impl<'a, const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> Future
33 for ReserveTask<'a, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
34{
35 type Output = TaskReservation<'a, TASKS, TASK_SIZE, ROOT_TASK_SIZE>;
36
37 fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
38 let this = self.get_mut();
39 let tasks = this.tasks;
40 if let Some((id, entry)) = tasks.reserve() {
41 if this.waiting {
42 tasks.clear_slot_waiter();
43 this.waiting = false;
44 }
45 Poll::Ready(TaskReservation::new(tasks, this.claimed, id, entry))
46 } else {
47 assert!(
48 this.waiting || !tasks.has_slot_waiter(),
49 "only one task can wait for a free task slot"
50 );
51 tasks.set_slot_waiter(cx.waker());
52 this.waiting = true;
53 Poll::Pending
54 }
55 }
56}
57
58impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> Drop
59 for ReserveTask<'_, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
60{
61 fn drop(&mut self) {
62 if self.waiting {
63 self.tasks.clear_slot_waiter();
64 }
65 }
66}
67
68impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> core::fmt::Debug
69 for ReserveTask<'_, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
70{
71 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
72 f.write_str("ReserveTask")
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use core::{
79 future::{Future, pending},
80 pin::Pin,
81 task::{Context, Waker},
82 };
83
84 use crate::{
85 tasks::{ReserveTask, Tasks},
86 testing::{claimed_forever, spawn_static},
87 };
88
89 type TestTasks = Tasks<1, 64, 0>;
90
91 fn poll(reserve: &mut ReserveTask<'static, 1, 64, 0>) -> bool {
92 Pin::new(reserve)
93 .poll(&mut Context::from_waker(Waker::noop()))
94 .is_ready()
95 }
96
97 #[test]
98 fn waits_until_a_task_finishes() {
99 static TASKS: TestTasks = TestTasks::new();
100 let _ = spawn_static(&TASKS, async {});
101 let mut reserve = ReserveTask::new(&TASKS, claimed_forever());
102 assert!(!poll(&mut reserve));
103 assert!(TASKS.has_slot_waiter());
104 let _ = TASKS.run_ready();
105 assert!(poll(&mut reserve));
106 assert!(!TASKS.has_slot_waiter());
107 }
108
109 #[test]
110 #[should_panic(expected = "only one task can wait for a free task slot")]
111 fn a_second_waiter_panics() {
112 static TASKS: TestTasks = TestTasks::new();
113 let _ = spawn_static(&TASKS, pending());
114 let mut first = ReserveTask::new(&TASKS, claimed_forever());
115 let mut second = ReserveTask::new(&TASKS, claimed_forever());
116 assert!(!poll(&mut first));
117 poll(&mut second);
118 }
119}