libasync/dispatcher/
task.rs1use zx::sys::ZX_OK;
8
9use core::task::Context;
10use fuchsia_sync::Mutex;
11use std::pin::Pin;
12use std::sync::atomic::{AtomicBool, Ordering};
13use std::sync::{Arc, mpsc};
14use std::task::Poll;
15
16use zx::Status;
17
18use futures::future::{BoxFuture, FutureExt};
19use futures::task::{ArcWake, AtomicWaker, waker_ref};
20
21use crate::{AsyncDispatcher, OnDispatcher};
22
23#[must_use]
26pub struct Task<T> {
27 state: Arc<TaskFutureState>,
28 result_receiver: mpsc::Receiver<Result<T, Status>>,
29 detached: bool,
30}
31
32impl<T: Send + 'static> Task<T> {
33 fn new<D: OnDispatcher + 'static>(
34 future: impl Future<Output = T> + Send + 'static,
35 dispatcher: D,
36 ) -> (Self, Arc<TaskWakerState<T, D>>) {
37 let future_state = Arc::new(TaskFutureState {
38 waker: AtomicWaker::new(),
39 aborted: AtomicBool::new(false),
40 });
41 let (result_sender, result_receiver) = mpsc::sync_channel(1);
42 let state = Arc::new(TaskWakerState {
43 result_sender,
44 future_state: future_state.clone(),
45 future: Mutex::new(Some(future.boxed())),
46 dispatcher,
47 });
48 let future = Task { state: future_state, result_receiver, detached: false };
49 (future, state)
50 }
51
52 pub(crate) fn try_start<D: OnDispatcher + 'static>(
53 future: impl Future<Output = T> + Send + 'static,
54 dispatcher: D,
55 ) -> Result<Self, Status> {
56 let (future, state) = Self::new(future, dispatcher);
57 state.queue().map(|_| future)
58 }
59
60 pub(crate) fn start<D: OnDispatcher + 'static>(
61 future: impl Future<Output = T> + Send + 'static,
62 dispatcher: D,
63 ) -> Self {
64 let (future, state) = Self::new(future, dispatcher);
65
66 if let Err(err) = state.queue() {
69 drop(state.future.lock().take());
71 state.result_sender.try_send(Err(err)).unwrap();
74 }
75
76 future
77 }
78}
79
80impl<T> Task<T> {
81 pub fn detach(self) {
85 drop(self.detach_on_drop());
86 }
87
88 pub fn detach_on_drop(mut self) -> JoinHandle<T> {
94 self.detached = true;
95 JoinHandle(self)
96 }
97
98 pub fn abort(&self) {
102 self.state.aborted.store(true, Ordering::Relaxed);
103 }
104}
105
106impl<T> Drop for Task<T> {
107 fn drop(&mut self) {
108 if !self.detached {
109 self.state.aborted.store(true, Ordering::Relaxed);
110 }
111 }
112}
113
114struct TaskFutureState {
115 waker: AtomicWaker,
116 aborted: AtomicBool,
117}
118
119impl<T> Future for Task<T> {
120 type Output = Result<T, Status>;
121
122 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
123 match self.result_receiver.try_recv() {
124 Ok(res) => Poll::Ready(res),
125 Err(_) => {
126 self.state.waker.register(cx.waker());
127 Poll::Pending
128 }
129 }
130 }
131}
132
133pub struct JoinHandle<T>(Task<T>);
135
136impl<T> JoinHandle<T> {
137 pub fn abort(&self) {
141 self.0.abort()
142 }
143}
144
145impl<T> Future for JoinHandle<T> {
146 type Output = Result<T, Status>;
147
148 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
149 self.0.poll_unpin(cx)
150 }
151}
152
153struct TaskWakerState<T, D> {
154 result_sender: mpsc::SyncSender<Result<T, Status>>,
155 future_state: Arc<TaskFutureState>,
156 future: Mutex<Option<BoxFuture<'static, T>>>,
157 dispatcher: D,
158}
159
160impl<T: Send + 'static, D: OnDispatcher + 'static> ArcWake for TaskWakerState<T, D> {
161 fn wake_by_ref(arc_self: &Arc<Self>) {
162 match arc_self.queue() {
163 Err(e) if e == Status::BAD_STATE => {
164 let future_slot = arc_self.future.lock().take();
167 drop(future_slot);
168 arc_self.send_result(Err(e));
169 }
170 res => res.expect("Unexpected error waking dispatcher task"),
171 }
172 }
173}
174
175impl<T: Send + 'static, D: OnDispatcher + 'static> TaskWakerState<T, D> {
176 fn send_result(&self, res: Result<T, Status>) {
178 self.result_sender.try_send(res).ok();
182 self.future_state.waker.wake();
183 }
184
185 pub(crate) fn queue(self: &Arc<Self>) -> Result<(), Status> {
189 let arc_self = self.clone();
190 self.dispatcher.on_maybe_dispatcher(move |dispatcher| {
191 dispatcher
192 .post_task_sync(move |status| {
193 let mut future_slot = arc_self.future.lock();
194 if status != Status::from_raw(ZX_OK) {
197 drop(future_slot.take());
198 arc_self.send_result(Err(status));
199 return;
200 }
201
202 if arc_self.future_state.aborted.load(Ordering::Relaxed) {
205 drop(future_slot.take());
206 arc_self.send_result(Err(Status::CANCELED));
207 return;
208 }
209
210 let Some(mut future) = future_slot.take() else {
211 return;
212 };
213 let waker = waker_ref(&arc_self);
214 let context = &mut Context::from_waker(&waker);
215 match future.as_mut().poll(context) {
216 Poll::Pending => *future_slot = Some(future),
217 Poll::Ready(res) => {
218 arc_self.send_result(Ok(res));
219 }
220 }
221 })
222 .map(|_| ())
223 })
224 }
225}