externally_driven_async_rt/tasks/
task_reservation.rs1use crate::{
2 claimed::Claimed,
3 tasks::{JoinHandle, TaskEntry, TaskId, Tasks},
4};
5
6#[must_use = "a reservation holds a task slot until it is spawned into or dropped"]
8pub struct TaskReservation<
9 'a,
10 const TASKS: usize,
11 const TASK_SIZE: usize,
12 const ROOT_TASK_SIZE: usize,
13> {
14 tasks: &'static Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
15 claimed: Claimed<'a>,
16 id: TaskId<TASK_SIZE>,
17 entry: &'static TaskEntry<TASK_SIZE>,
18}
19
20impl<'a, const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize>
21 TaskReservation<'a, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
22{
23 pub(crate) const fn new(
24 tasks: &'static Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
25 claimed: Claimed<'a>,
26 id: TaskId<TASK_SIZE>,
27 entry: &'static TaskEntry<TASK_SIZE>,
28 ) -> Self {
29 Self {
30 tasks,
31 claimed,
32 id,
33 entry,
34 }
35 }
36
37 pub fn spawn<Fut>(self, future: Fut) -> JoinHandle<TASKS, TASK_SIZE, ROOT_TASK_SIZE>
46 where
47 Fut: Future<Output = ()> + 'a,
48 {
49 let Self {
50 tasks,
51 claimed,
52 id,
53 entry,
54 } = self;
55 tasks.spawn_reserved(claimed, id, entry, future);
56 core::mem::forget(self);
57 JoinHandle::new(tasks, id)
58 }
59}
60
61impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> Drop
62 for TaskReservation<'_, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
63{
64 fn drop(&mut self) {
65 self.tasks.release(self.id);
66 }
67}
68
69impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> core::fmt::Debug
70 for TaskReservation<'_, TASKS, TASK_SIZE, ROOT_TASK_SIZE>
71{
72 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73 f.write_str("TaskReservation")
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use core::{
80 future::{Future, pending},
81 pin::Pin,
82 sync::atomic::{AtomicBool, Ordering::Relaxed},
83 task::{Context, Poll, Waker},
84 };
85
86 use crate::{
87 tasks::{ReserveTask, TaskReservation, Tasks},
88 testing::{claimed_forever, spawn_static},
89 };
90
91 #[test]
92 fn spawning_runs_the_task() {
93 static TASKS: Tasks<1, 64, 0> = Tasks::new();
94 static RAN: AtomicBool = AtomicBool::new(false);
95 let (id, entry) = TASKS.reserve().unwrap();
96 let mut handle = TaskReservation::new(&TASKS, claimed_forever(), id, entry)
97 .spawn(async { RAN.store(true, Relaxed) });
98 let _ = TASKS.run_ready();
99 assert!(RAN.load(Relaxed));
100 let mut cx = Context::from_waker(Waker::noop());
101 assert_eq!(Pin::new(&mut handle).poll(&mut cx), Poll::Ready(()));
102 }
103
104 #[test]
105 fn dropping_frees_the_slot_and_wakes_the_waiter() {
106 static TASKS: Tasks<2, 64, 0> = Tasks::new();
107 TASKS.queue.claim();
108 let waiter = spawn_static(&TASKS, pending());
109 let _ = TASKS.run_ready();
110 let (id, entry) = TASKS.reserve().unwrap();
111 let reservation = TaskReservation::new(&TASKS, claimed_forever(), id, entry);
112 let mut reserve = ReserveTask::new(&TASKS, claimed_forever());
113 let waker = TASKS.queue.waker(waiter.slot());
114 let mut cx = Context::from_waker(&waker);
115 assert!(Pin::new(&mut reserve).poll(&mut cx).is_pending());
116 drop(reservation);
117 assert!(TASKS.has_ready());
118 assert!(Pin::new(&mut reserve).poll(&mut cx).is_ready());
119 }
120
121 #[test]
122 #[should_panic(expected = "more than 1 concurrent tasks")]
123 fn a_reserved_slot_is_not_handed_to_spawn() {
124 static TASKS: Tasks<1, 64, 0> = Tasks::new();
125 let (id, entry) = TASKS.reserve().unwrap();
126 let _reservation = TaskReservation::new(&TASKS, claimed_forever(), id, entry);
127 let _ = spawn_static(&TASKS, async {});
128 }
129}