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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pub use std::thread::Result;
use std::{
future::Future,
mem::ManuallyDrop,
sync::Arc,
task::{Poll, Waker},
};
pub use crate::runtime::spawn;
mod raw;
use raw::{Head, Suit};
mod join;
pub use join::JoinHandle;
mod yield_now;
pub use yield_now::yield_now;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct TaskId(u64);
pub struct Task(ManuallyDrop<Arc<Head>>);
impl Task {
pub(crate) fn new<F, S>(id: u64, future: F, schedule: S) -> (Self, JoinHandle<F::Output>)
where
F: Future + Send + 'static,
F::Output: Send + 'static,
S: Schedule + Send + Sync,
{
let suit = Arc::new(Suit::new(id, future, schedule));
let task = Self::from_suit(suit.clone());
let handle = JoinHandle::new(Self::from_suit(suit));
(task, handle)
}
fn from_suit<F, S>(suit: Arc<Suit<F, S>>) -> Self
where
F: Future + Send + 'static,
F::Output: Send + 'static,
S: Schedule + Send + Sync,
{
let head = unsafe { Arc::from_raw(Arc::into_raw(suit) as *const Head) };
Self(ManuallyDrop::new(head))
}
pub fn id(&self) -> TaskId {
TaskId(self.0.id())
}
pub(crate) fn poll(&self) {
unsafe { self.0.poll(&self.0) }
}
pub(super) fn join<T>(&self, waker: &Waker) -> Poll<Result<T>> {
unsafe { self.0.join(&self.0, waker) }
}
pub(super) fn detach(&self) {
unsafe { self.0.detach(&self.0) }
}
}
unsafe impl Send for Task {}
impl Drop for Task {
fn drop(&mut self) {
unsafe { self.0.as_ref().drop(&self.0) }
}
}
pub(crate) trait Schedule {
fn schedule(&self, task: Task);
}