@@ -88,6 +88,14 @@ pub struct Builder {
88
88
/// To run before each task is spawned.
89
89
pub ( super ) before_spawn : Option < TaskCallback > ,
90
90
91
+ /// To run before each poll
92
+ #[ cfg( tokio_unstable) ]
93
+ pub ( super ) before_poll : Option < TaskCallback > ,
94
+
95
+ /// To run after each poll
96
+ #[ cfg( tokio_unstable) ]
97
+ pub ( super ) after_poll : Option < TaskCallback > ,
98
+
91
99
/// To run after each task is terminated.
92
100
pub ( super ) after_termination : Option < TaskCallback > ,
93
101
@@ -306,6 +314,11 @@ impl Builder {
306
314
before_spawn : None ,
307
315
after_termination : None ,
308
316
317
+ #[ cfg( tokio_unstable) ]
318
+ before_poll : None ,
319
+ #[ cfg( tokio_unstable) ]
320
+ after_poll : None ,
321
+
309
322
keep_alive : None ,
310
323
311
324
// Defaults for these values depend on the scheduler kind, so we get them
@@ -743,6 +756,92 @@ impl Builder {
743
756
self
744
757
}
745
758
759
+ /// Executes function `f` just before a task is polled
760
+ ///
761
+ /// `f` is called within the Tokio context, so functions like
762
+ /// [`tokio::spawn`](crate::spawn) can be called, and may result in this callback being
763
+ /// invoked immediately.
764
+ ///
765
+ /// **Note**: This is an [unstable API][unstable]. The public API of this type
766
+ /// may break in 1.x releases. See [the documentation on unstable
767
+ /// features][unstable] for details.
768
+ ///
769
+ /// [unstable]: crate#unstable-features
770
+ ///
771
+ /// # Examples
772
+ ///
773
+ /// ```
774
+ /// # use std::sync::{atomic::AtomicUsize, Arc};
775
+ /// # use tokio::task::yield_now;
776
+ /// # pub fn main() {
777
+ /// let poll_start_counter = Arc::new(AtomicUsize::new(0));
778
+ /// let poll_start = poll_start_counter.clone();
779
+ /// let rt = tokio::runtime::Builder::new_multi_thread()
780
+ /// .enable_all()
781
+ /// .on_before_task_poll(move |meta| {
782
+ /// println!("task {} is about to be polled", meta.id())
783
+ /// })
784
+ /// .build()
785
+ /// .unwrap();
786
+ /// let task = rt.spawn(async {
787
+ /// yield_now().await;
788
+ /// });
789
+ /// let _ = rt.block_on(task);
790
+ ///
791
+ /// # }
792
+ /// ```
793
+ #[ cfg( tokio_unstable) ]
794
+ pub fn on_before_task_poll < F > ( & mut self , f : F ) -> & mut Self
795
+ where
796
+ F : Fn ( & TaskMeta < ' _ > ) + Send + Sync + ' static ,
797
+ {
798
+ self . before_poll = Some ( std:: sync:: Arc :: new ( f) ) ;
799
+ self
800
+ }
801
+
802
+ /// Executes function `f` just after a task is polled
803
+ ///
804
+ /// `f` is called within the Tokio context, so functions like
805
+ /// [`tokio::spawn`](crate::spawn) can be called, and may result in this callback being
806
+ /// invoked immediately.
807
+ ///
808
+ /// **Note**: This is an [unstable API][unstable]. The public API of this type
809
+ /// may break in 1.x releases. See [the documentation on unstable
810
+ /// features][unstable] for details.
811
+ ///
812
+ /// [unstable]: crate#unstable-features
813
+ ///
814
+ /// # Examples
815
+ ///
816
+ /// ```
817
+ /// # use std::sync::{atomic::AtomicUsize, Arc};
818
+ /// # use tokio::task::yield_now;
819
+ /// # pub fn main() {
820
+ /// let poll_stop_counter = Arc::new(AtomicUsize::new(0));
821
+ /// let poll_stop = poll_stop_counter.clone();
822
+ /// let rt = tokio::runtime::Builder::new_multi_thread()
823
+ /// .enable_all()
824
+ /// .on_after_task_poll(move |meta| {
825
+ /// println!("task {} completed polling", meta.id());
826
+ /// })
827
+ /// .build()
828
+ /// .unwrap();
829
+ /// let task = rt.spawn(async {
830
+ /// yield_now().await;
831
+ /// });
832
+ /// let _ = rt.block_on(task);
833
+ ///
834
+ /// # }
835
+ /// ```
836
+ #[ cfg( tokio_unstable) ]
837
+ pub fn on_after_task_poll < F > ( & mut self , f : F ) -> & mut Self
838
+ where
839
+ F : Fn ( & TaskMeta < ' _ > ) + Send + Sync + ' static ,
840
+ {
841
+ self . after_poll = Some ( std:: sync:: Arc :: new ( f) ) ;
842
+ self
843
+ }
844
+
746
845
/// Executes function `f` just after a task is terminated.
747
846
///
748
847
/// `f` is called within the Tokio context, so functions like
@@ -1410,6 +1509,10 @@ impl Builder {
1410
1509
before_park : self . before_park . clone ( ) ,
1411
1510
after_unpark : self . after_unpark . clone ( ) ,
1412
1511
before_spawn : self . before_spawn . clone ( ) ,
1512
+ #[ cfg( tokio_unstable) ]
1513
+ before_poll : self . before_poll . clone ( ) ,
1514
+ #[ cfg( tokio_unstable) ]
1515
+ after_poll : self . after_poll . clone ( ) ,
1413
1516
after_termination : self . after_termination . clone ( ) ,
1414
1517
global_queue_interval : self . global_queue_interval ,
1415
1518
event_interval : self . event_interval ,
@@ -1560,6 +1663,10 @@ cfg_rt_multi_thread! {
1560
1663
before_park: self . before_park. clone( ) ,
1561
1664
after_unpark: self . after_unpark. clone( ) ,
1562
1665
before_spawn: self . before_spawn. clone( ) ,
1666
+ #[ cfg( tokio_unstable) ]
1667
+ before_poll: self . before_poll. clone( ) ,
1668
+ #[ cfg( tokio_unstable) ]
1669
+ after_poll: self . after_poll. clone( ) ,
1563
1670
after_termination: self . after_termination. clone( ) ,
1564
1671
global_queue_interval: self . global_queue_interval,
1565
1672
event_interval: self . event_interval,
@@ -1610,6 +1717,10 @@ cfg_rt_multi_thread! {
1610
1717
after_unpark: self . after_unpark. clone( ) ,
1611
1718
before_spawn: self . before_spawn. clone( ) ,
1612
1719
after_termination: self . after_termination. clone( ) ,
1720
+ #[ cfg( tokio_unstable) ]
1721
+ before_poll: self . before_poll. clone( ) ,
1722
+ #[ cfg( tokio_unstable) ]
1723
+ after_poll: self . after_poll. clone( ) ,
1613
1724
global_queue_interval: self . global_queue_interval,
1614
1725
event_interval: self . event_interval,
1615
1726
local_queue_capacity: self . local_queue_capacity,
0 commit comments