externally_driven_async_rt/tasks/
join_handle.rs1use core::{future::Future, marker::PhantomData, pin::Pin, task::Poll};
2
3use crate::tasks::{JoinStatus, TaskId, Tasks};
4
5#[must_use]
7pub struct JoinHandle<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> {
8 tasks: &'static Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
9 pub(crate) id: TaskId<TASK_SIZE>,
10 _not_send: PhantomData<*const ()>,
11}
12
13impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize>
14 JoinHandle<TASKS, TASK_SIZE, ROOT_TASK_SIZE>
15{
16 pub(crate) const fn new(
17 tasks: &'static Tasks<TASKS, TASK_SIZE, ROOT_TASK_SIZE>,
18 id: TaskId<TASK_SIZE>,
19 ) -> Self {
20 Self {
21 tasks,
22 id,
23 _not_send: PhantomData,
24 }
25 }
26}
27
28impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> Future
29 for JoinHandle<TASKS, TASK_SIZE, ROOT_TASK_SIZE>
30{
31 type Output = ();
32
33 fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<()> {
34 match self.tasks.set_join_waiter(self.id, cx.waker()) {
35 JoinStatus::Running => Poll::Pending,
36 JoinStatus::Finished => Poll::Ready(()),
37 }
38 }
39}
40
41impl<const TASKS: usize, const TASK_SIZE: usize, const ROOT_TASK_SIZE: usize> core::fmt::Debug
42 for JoinHandle<TASKS, TASK_SIZE, ROOT_TASK_SIZE>
43{
44 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45 f.write_str("JoinHandle")
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use core::{
52 cell::RefCell,
53 future::{Future, pending},
54 pin::Pin,
55 sync::atomic::{AtomicBool, Ordering::Relaxed},
56 task::{Context, Poll, Waker},
57 };
58
59 use crate::{
60 Selector,
61 tasks::{JoinHandle, Tasks},
62 testing::{Log, TestLoop, TestStorage, spawn_static},
63 };
64
65 type TestTasks = Tasks<2, 64, 0>;
66
67 fn poll(handle: &mut JoinHandle<2, 64, 0>) -> Poll<()> {
68 Pin::new(handle).poll(&mut Context::from_waker(Waker::noop()))
69 }
70
71 #[test]
72 fn pending_until_the_task_finishes() {
73 static TASKS: TestTasks = TestTasks::new();
74 let mut handle = JoinHandle::new(&TASKS, spawn_static(&TASKS, async {}));
75 assert_eq!(poll(&mut handle), Poll::Pending);
76 let _ = TASKS.run_ready();
77 assert_eq!(poll(&mut handle), Poll::Ready(()));
78 }
79
80 #[test]
81 fn joining_a_finished_task_resolves_immediately() {
82 static TASKS: TestTasks = TestTasks::new();
83 let id = spawn_static(&TASKS, async {});
84 let _ = TASKS.run_ready();
85 assert_eq!(poll(&mut JoinHandle::new(&TASKS, id)), Poll::Ready(()));
86 }
87
88 #[test]
89 fn dropped_handle_detaches_the_task() {
90 static TASKS: TestTasks = TestTasks::new();
91 static RAN: AtomicBool = AtomicBool::new(false);
92 let id = spawn_static(&TASKS, async { RAN.store(true, Relaxed) });
93 drop(JoinHandle::new(&TASKS, id));
94 let _ = TASKS.run_ready();
95 assert!(RAN.load(Relaxed));
96 }
97
98 #[test]
99 fn spawned_task_can_spawn_and_join() {
100 let mut selector = Selector::new().unwrap();
101 let mut state = RefCell::new(Log::default());
102 let event_loop = TestLoop::new(
103 {
104 static STORAGE: TestStorage = TestStorage::new();
105 &STORAGE
106 },
107 &mut selector,
108 &mut state,
109 async |ctx| {
110 ctx.spawn(async |ctx| {
111 ctx.spawn(async |ctx| ctx.dispatch(1)).await;
112 ctx.dispatch(2);
113 })
114 .await;
115 ctx.dispatch(3);
116 pending::<()>().await;
117 },
118 );
119 assert_eq!(event_loop.unwrap().state_mut().events(), [1, 2, 3]);
120 }
121}