jay_config/
tasks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Tools for async task management.

use std::{
    cell::Cell,
    fmt::{Debug, Formatter},
    future::Future,
    pin::Pin,
    rc::Rc,
    task::{Context, Poll, Waker},
};

/// Spawns an asynchronous task that will run in the background.
pub fn spawn<T, F>(f: F) -> JoinHandle<T>
where
    T: 'static,
    F: Future<Output = T> + 'static,
{
    let slot = match try_get!() {
        None => Rc::new(JoinSlot {
            task_id: 0,
            slot: Cell::new(None),
            waker: Cell::new(None),
        }),
        Some(c) => c.spawn_task(f),
    };
    JoinHandle { slot }
}

pub(crate) struct JoinSlot<T> {
    pub task_id: u64,
    pub slot: Cell<Option<T>>,
    pub waker: Cell<Option<Waker>>,
}

/// A handle to join or abort a spawned task.
///
/// When the handle is dropped, the task continues to run in the background.
pub struct JoinHandle<T> {
    slot: Rc<JoinSlot<T>>,
}

impl<T> Debug for JoinHandle<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JoinHandle")
            .field("task_id", &self.slot.task_id)
            .finish_non_exhaustive()
    }
}

impl<T> Unpin for JoinHandle<T> {}

impl<T> JoinHandle<T> {
    /// Aborts the task immediately.
    pub fn abort(self) {
        get!().abort_task(self.slot.task_id);
    }
}

impl<T> Future for JoinHandle<T> {
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if let Some(t) = self.slot.slot.take() {
            return Poll::Ready(t);
        }
        self.slot.waker.set(Some(cx.waker().clone()));
        Poll::Pending
    }
}