use crate::runtime::blocking::task::BlockingTask;
use crate::runtime::task::{self, JoinHandle};
use crate::runtime::{blocking, context, driver, Spawner};
use crate::util::error::CONTEXT_MISSING_ERROR;
use std::future::Future;
use std::{error, fmt};
#[derive(Debug, Clone)]
pub struct Handle {
pub(super) spawner: Spawner,
pub(super) io_handle: driver::IoHandle,
pub(super) signal_handle: driver::SignalHandle,
pub(super) time_handle: driver::TimeHandle,
pub(super) clock: driver::Clock,
pub(super) blocking_spawner: blocking::Spawner,
}
#[derive(Debug)]
pub struct EnterGuard<'a> {
handle: &'a Handle,
guard: context::EnterGuard,
}
impl Handle {
pub fn enter(&self) -> EnterGuard<'_> {
EnterGuard {
handle: self,
guard: context::enter(self.clone()),
}
}
pub fn current() -> Self {
context::current().expect(CONTEXT_MISSING_ERROR)
}
pub fn try_current() -> Result<Self, TryCurrentError> {
context::current().ok_or(TryCurrentError(()))
}
#[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
#[cfg(all(tokio_unstable, feature = "tracing"))]
let future = crate::util::trace::task(future, "task");
self.spawner.spawn(future)
}
#[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
#[cfg(all(tokio_unstable, feature = "tracing"))]
let func = {
#[cfg(tokio_track_caller)]
let location = std::panic::Location::caller();
#[cfg(tokio_track_caller)]
let span = tracing::trace_span!(
target: "tokio::task",
"task",
kind = %"blocking",
function = %std::any::type_name::<F>(),
spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
);
#[cfg(not(tokio_track_caller))]
let span = tracing::trace_span!(
target: "tokio::task",
"task",
kind = %"blocking",
function = %std::any::type_name::<F>(),
);
move || {
let _g = span.enter();
func()
}
};
let (task, handle) = task::joinable(BlockingTask::new(func));
let _ = self.blocking_spawner.spawn(task, &self);
handle
}
}
pub struct TryCurrentError(());
impl fmt::Debug for TryCurrentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TryCurrentError").finish()
}
}
impl fmt::Display for TryCurrentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(CONTEXT_MISSING_ERROR)
}
}
impl error::Error for TryCurrentError {}