mod core;
use self::core::Cell;
use self::core::Header;
mod error;
#[allow(unreachable_pub)] pub use self::error::JoinError;
mod harness;
use self::harness::Harness;
cfg_rt_multi_thread! {
mod inject;
pub(super) use self::inject::Inject;
}
mod join;
#[allow(unreachable_pub)] pub use self::join::JoinHandle;
mod list;
pub(crate) use self::list::{LocalOwnedTasks, OwnedTasks};
mod raw;
use self::raw::RawTask;
mod state;
use self::state::State;
mod waker;
use crate::future::Future;
use crate::util::linked_list;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::{fmt, mem};
#[repr(transparent)]
pub(crate) struct Task<S: 'static> {
raw: RawTask,
_p: PhantomData<S>,
}
unsafe impl<S> Send for Task<S> {}
unsafe impl<S> Sync for Task<S> {}
#[repr(transparent)]
pub(crate) struct Notified<S: 'static>(Task<S>);
unsafe impl<S: Schedule> Send for Notified<S> {}
unsafe impl<S: Schedule> Sync for Notified<S> {}
#[repr(transparent)]
pub(crate) struct LocalNotified<S: 'static> {
task: Task<S>,
_not_send: PhantomData<*const ()>,
}
pub(crate) struct UnownedTask<S: 'static> {
raw: RawTask,
_p: PhantomData<S>,
}
unsafe impl<S> Send for UnownedTask<S> {}
unsafe impl<S> Sync for UnownedTask<S> {}
pub(crate) type Result<T> = std::result::Result<T, JoinError>;
pub(crate) trait Schedule: Sync + Sized + 'static {
fn release(&self, task: &Task<Self>) -> Option<Task<Self>>;
fn schedule(&self, task: Notified<Self>);
fn yield_now(&self, task: Notified<Self>) {
self.schedule(task);
}
}
cfg_rt! {
fn new_task<T, S>(
task: T,
scheduler: S
) -> (Task<S>, Notified<S>, JoinHandle<T::Output>)
where
S: Schedule,
T: Future + 'static,
T::Output: 'static,
{
let raw = RawTask::new::<T, S>(task, scheduler);
let task = Task {
raw,
_p: PhantomData,
};
let notified = Notified(Task {
raw,
_p: PhantomData,
});
let join = JoinHandle::new(raw);
(task, notified, join)
}
pub(crate) fn unowned<T, S>(task: T, scheduler: S) -> (UnownedTask<S>, JoinHandle<T::Output>)
where
S: Schedule,
T: Send + Future + 'static,
T::Output: Send + 'static,
{
let (task, notified, join) = new_task(task, scheduler);
let unowned = UnownedTask {
raw: task.raw,
_p: PhantomData,
};
std::mem::forget(task);
std::mem::forget(notified);
(unowned, join)
}
}
impl<S: 'static> Task<S> {
unsafe fn from_raw(ptr: NonNull<Header>) -> Task<S> {
Task {
raw: RawTask::from_raw(ptr),
_p: PhantomData,
}
}
fn header(&self) -> &Header {
self.raw.header()
}
}
impl<S: 'static> Notified<S> {
fn header(&self) -> &Header {
self.0.header()
}
}
cfg_rt_multi_thread! {
impl<S: 'static> Notified<S> {
unsafe fn from_raw(ptr: NonNull<Header>) -> Notified<S> {
Notified(Task::from_raw(ptr))
}
}
impl<S: 'static> Task<S> {
fn into_raw(self) -> NonNull<Header> {
let ret = self.header().into();
mem::forget(self);
ret
}
}
impl<S: 'static> Notified<S> {
fn into_raw(self) -> NonNull<Header> {
self.0.into_raw()
}
}
}
impl<S: Schedule> Task<S> {
pub(crate) fn shutdown(self) {
let raw = self.raw;
mem::forget(self);
raw.shutdown();
}
}
impl<S: Schedule> LocalNotified<S> {
pub(crate) fn run(self) {
let raw = self.task.raw;
mem::forget(self);
raw.poll();
}
}
impl<S: Schedule> UnownedTask<S> {
#[cfg(test)]
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub(super) fn into_notified(self) -> Notified<S> {
Notified(self.into_task())
}
fn into_task(self) -> Task<S> {
let task = Task {
raw: self.raw,
_p: PhantomData,
};
mem::forget(self);
task.header().state.ref_dec();
task
}
pub(crate) fn run(self) {
let raw = self.raw;
mem::forget(self);
let task = Task::<S> {
raw,
_p: PhantomData,
};
raw.poll();
drop(task);
}
pub(crate) fn shutdown(self) {
self.into_task().shutdown()
}
}
impl<S: 'static> Drop for Task<S> {
fn drop(&mut self) {
if self.header().state.ref_dec() {
self.raw.dealloc();
}
}
}
impl<S: 'static> Drop for UnownedTask<S> {
fn drop(&mut self) {
if self.raw.header().state.ref_dec_twice() {
self.raw.dealloc();
}
}
}
impl<S> fmt::Debug for Task<S> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "Task({:p})", self.header())
}
}
impl<S> fmt::Debug for Notified<S> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "task::Notified({:p})", self.0.header())
}
}
unsafe impl<S> linked_list::Link for Task<S> {
type Handle = Task<S>;
type Target = Header;
fn as_raw(handle: &Task<S>) -> NonNull<Header> {
handle.header().into()
}
unsafe fn from_raw(ptr: NonNull<Header>) -> Task<S> {
Task::from_raw(ptr)
}
unsafe fn pointers(target: NonNull<Header>) -> NonNull<linked_list::Pointers<Header>> {
NonNull::from(target.as_ref().owned.with_mut(|ptr| &mut *ptr))
}
}