|
| 1 | +#![cfg_attr(loom, allow(unused_imports))] |
| 2 | + |
1 | 3 | use crate::runtime::handle::Handle;
|
2 |
| -use crate::runtime::{blocking, driver, Callback, HistogramBuilder, Runtime}; |
| 4 | +#[cfg(tokio_unstable)] |
| 5 | +use crate::runtime::TaskMeta; |
| 6 | +use crate::runtime::{blocking, driver, Callback, HistogramBuilder, Runtime, TaskCallback}; |
3 | 7 | use crate::util::rand::{RngSeed, RngSeedGenerator};
|
4 | 8 |
|
5 | 9 | use std::fmt;
|
@@ -78,6 +82,12 @@ pub struct Builder {
|
78 | 82 | /// To run after each thread is unparked.
|
79 | 83 | pub(super) after_unpark: Option<Callback>,
|
80 | 84 |
|
| 85 | + /// To run before each task is spawned. |
| 86 | + pub(super) before_spawn: Option<TaskCallback>, |
| 87 | + |
| 88 | + /// To run after each task is terminated. |
| 89 | + pub(super) after_termination: Option<TaskCallback>, |
| 90 | + |
81 | 91 | /// Customizable keep alive timeout for `BlockingPool`
|
82 | 92 | pub(super) keep_alive: Option<Duration>,
|
83 | 93 |
|
@@ -290,6 +300,9 @@ impl Builder {
|
290 | 300 | before_park: None,
|
291 | 301 | after_unpark: None,
|
292 | 302 |
|
| 303 | + before_spawn: None, |
| 304 | + after_termination: None, |
| 305 | + |
293 | 306 | keep_alive: None,
|
294 | 307 |
|
295 | 308 | // Defaults for these values depend on the scheduler kind, so we get them
|
@@ -677,6 +690,91 @@ impl Builder {
|
677 | 690 | self
|
678 | 691 | }
|
679 | 692 |
|
| 693 | + /// Executes function `f` just before a task is spawned. |
| 694 | + /// |
| 695 | + /// `f` is called within the Tokio context, so functions like |
| 696 | + /// [`tokio::spawn`](crate::spawn) can be called, and may result in this callback being |
| 697 | + /// invoked immediately. |
| 698 | + /// |
| 699 | + /// This can be used for bookkeeping or monitoring purposes. |
| 700 | + /// |
| 701 | + /// Note: There can only be one spawn callback for a runtime; calling this function more |
| 702 | + /// than once replaces the last callback defined, rather than adding to it. |
| 703 | + /// |
| 704 | + /// This *does not* support [`LocalSet`](crate::task::LocalSet) at this time. |
| 705 | + /// |
| 706 | + /// # Examples |
| 707 | + /// |
| 708 | + /// ``` |
| 709 | + /// # use tokio::runtime; |
| 710 | + /// # pub fn main() { |
| 711 | + /// let runtime = runtime::Builder::new_current_thread() |
| 712 | + /// .on_task_spawn(|_| { |
| 713 | + /// println!("spawning task"); |
| 714 | + /// }) |
| 715 | + /// .build() |
| 716 | + /// .unwrap(); |
| 717 | + /// |
| 718 | + /// runtime.block_on(async { |
| 719 | + /// tokio::task::spawn(std::future::ready(())); |
| 720 | + /// |
| 721 | + /// for _ in 0..64 { |
| 722 | + /// tokio::task::yield_now().await; |
| 723 | + /// } |
| 724 | + /// }) |
| 725 | + /// # } |
| 726 | + /// ``` |
| 727 | + #[cfg(all(not(loom), tokio_unstable))] |
| 728 | + pub fn on_task_spawn<F>(&mut self, f: F) -> &mut Self |
| 729 | + where |
| 730 | + F: Fn(&TaskMeta<'_>) + Send + Sync + 'static, |
| 731 | + { |
| 732 | + self.before_spawn = Some(std::sync::Arc::new(f)); |
| 733 | + self |
| 734 | + } |
| 735 | + |
| 736 | + /// Executes function `f` just after a task is terminated. |
| 737 | + /// |
| 738 | + /// `f` is called within the Tokio context, so functions like |
| 739 | + /// [`tokio::spawn`](crate::spawn) can be called. |
| 740 | + /// |
| 741 | + /// This can be used for bookkeeping or monitoring purposes. |
| 742 | + /// |
| 743 | + /// Note: There can only be one task termination callback for a runtime; calling this |
| 744 | + /// function more than once replaces the last callback defined, rather than adding to it. |
| 745 | + /// |
| 746 | + /// This *does not* support [`LocalSet`](crate::task::LocalSet) at this time. |
| 747 | + /// |
| 748 | + /// # Examples |
| 749 | + /// |
| 750 | + /// ``` |
| 751 | + /// # use tokio::runtime; |
| 752 | + /// # pub fn main() { |
| 753 | + /// let runtime = runtime::Builder::new_current_thread() |
| 754 | + /// .on_task_terminate(|_| { |
| 755 | + /// println!("killing task"); |
| 756 | + /// }) |
| 757 | + /// .build() |
| 758 | + /// .unwrap(); |
| 759 | + /// |
| 760 | + /// runtime.block_on(async { |
| 761 | + /// tokio::task::spawn(std::future::ready(())); |
| 762 | + /// |
| 763 | + /// for _ in 0..64 { |
| 764 | + /// tokio::task::yield_now().await; |
| 765 | + /// } |
| 766 | + /// }) |
| 767 | + /// # } |
| 768 | + /// ``` |
| 769 | + #[cfg(all(not(loom), tokio_unstable))] |
| 770 | + pub fn on_task_terminate<F>(&mut self, f: F) -> &mut Self |
| 771 | + where |
| 772 | + F: Fn(&TaskMeta<'_>) + Send + Sync + 'static, |
| 773 | + { |
| 774 | + self.after_termination = Some(std::sync::Arc::new(f)); |
| 775 | + self |
| 776 | + } |
| 777 | + |
680 | 778 | /// Creates the configured `Runtime`.
|
681 | 779 | ///
|
682 | 780 | /// The returned `Runtime` instance is ready to spawn tasks.
|
@@ -1118,6 +1216,8 @@ impl Builder {
|
1118 | 1216 | Config {
|
1119 | 1217 | before_park: self.before_park.clone(),
|
1120 | 1218 | after_unpark: self.after_unpark.clone(),
|
| 1219 | + before_spawn: self.before_spawn.clone(), |
| 1220 | + after_termination: self.after_termination.clone(), |
1121 | 1221 | global_queue_interval: self.global_queue_interval,
|
1122 | 1222 | event_interval: self.event_interval,
|
1123 | 1223 | local_queue_capacity: self.local_queue_capacity,
|
@@ -1269,6 +1369,8 @@ cfg_rt_multi_thread! {
|
1269 | 1369 | Config {
|
1270 | 1370 | before_park: self.before_park.clone(),
|
1271 | 1371 | after_unpark: self.after_unpark.clone(),
|
| 1372 | + before_spawn: self.before_spawn.clone(), |
| 1373 | + after_termination: self.after_termination.clone(), |
1272 | 1374 | global_queue_interval: self.global_queue_interval,
|
1273 | 1375 | event_interval: self.event_interval,
|
1274 | 1376 | local_queue_capacity: self.local_queue_capacity,
|
@@ -1316,6 +1418,8 @@ cfg_rt_multi_thread! {
|
1316 | 1418 | Config {
|
1317 | 1419 | before_park: self.before_park.clone(),
|
1318 | 1420 | after_unpark: self.after_unpark.clone(),
|
| 1421 | + before_spawn: self.before_spawn.clone(), |
| 1422 | + after_termination: self.after_termination.clone(), |
1319 | 1423 | global_queue_interval: self.global_queue_interval,
|
1320 | 1424 | event_interval: self.event_interval,
|
1321 | 1425 | local_queue_capacity: self.local_queue_capacity,
|
|
0 commit comments