tokio/runtime/
task_hooks.rs1use super::Config;
2use std::marker::PhantomData;
3#[cfg(all(tokio_unstable, feature = "schedule-latency"))]
4use std::time::Duration;
5
6impl TaskHooks {
7 pub(crate) fn spawn(&self, meta: &TaskMeta<'_>) {
8 if let Some(f) = self.task_spawn_callback.as_ref() {
9 f(meta)
10 }
11 }
12
13 #[allow(dead_code)]
14 pub(crate) fn from_config(config: &Config) -> Self {
15 Self {
16 task_spawn_callback: config.before_spawn.clone(),
17 task_terminate_callback: config.after_termination.clone(),
18 #[cfg(tokio_unstable)]
19 before_poll_callback: config.before_poll.clone(),
20 #[cfg(tokio_unstable)]
21 after_poll_callback: config.after_poll.clone(),
22 }
23 }
24
25 #[cfg(tokio_unstable)]
26 #[inline]
27 pub(crate) fn poll_start_callback(&self, meta: &TaskMeta<'_>) {
28 if let Some(poll_start) = &self.before_poll_callback {
29 (poll_start)(meta);
30 }
31 }
32
33 #[cfg(tokio_unstable)]
34 #[inline]
35 pub(crate) fn poll_stop_callback(&self, meta: &TaskMeta<'_>) {
36 if let Some(poll_stop) = &self.after_poll_callback {
37 (poll_stop)(meta);
38 }
39 }
40}
41
42#[derive(Clone)]
43pub(crate) struct TaskHooks {
44 pub(crate) task_spawn_callback: Option<TaskCallback>,
45 pub(crate) task_terminate_callback: Option<TaskCallback>,
46 #[cfg(tokio_unstable)]
47 pub(crate) before_poll_callback: Option<TaskCallback>,
48 #[cfg(tokio_unstable)]
49 pub(crate) after_poll_callback: Option<TaskCallback>,
50}
51
52#[allow(missing_debug_implementations)]
60#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))]
61pub struct TaskMeta<'a> {
62 pub(crate) id: super::task::Id,
64 #[cfg_attr(not(tokio_unstable), allow(unreachable_pub, dead_code))]
66 pub(crate) spawned_at: crate::runtime::task::SpawnLocation,
67 #[cfg(all(tokio_unstable, feature = "schedule-latency"))]
69 pub(crate) schedule_latency: Option<Duration>,
70 pub(crate) _phantom: PhantomData<&'a ()>,
71}
72
73impl<'a> TaskMeta<'a> {
74 #[cfg_attr(not(tokio_unstable), allow(unreachable_pub, dead_code))]
76 pub fn id(&self) -> super::task::Id {
77 self.id
78 }
79
80 #[cfg(tokio_unstable)]
82 pub fn spawned_at(&self) -> &'static std::panic::Location<'static> {
83 self.spawned_at.0
84 }
85
86 #[cfg(all(tokio_unstable, feature = "schedule-latency"))]
107 #[cfg_attr(docsrs, doc(cfg(all(tokio_unstable, feature = "schedule-latency"))))]
108 pub fn schedule_latency(&self) -> Option<Duration> {
109 self.schedule_latency
110 }
111}
112
113pub(crate) type TaskCallback = std::sync::Arc<dyn Fn(&TaskMeta<'_>) + Send + Sync>;