summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio/src/runtime/spawner.rs
blob: d81a806cb59a6d7565294f366778e63b3c0dada6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::future::Future;
use crate::runtime::basic_scheduler;
use crate::task::JoinHandle;

cfg_rt_multi_thread! {
    use crate::runtime::thread_pool;
}

#[derive(Debug, Clone)]
pub(crate) enum Spawner {
    Basic(basic_scheduler::Spawner),
    #[cfg(feature = "rt-multi-thread")]
    ThreadPool(thread_pool::Spawner),
}

impl Spawner {
    pub(crate) fn shutdown(&mut self) {
        #[cfg(feature = "rt-multi-thread")]
        {
            if let Spawner::ThreadPool(spawner) = self {
                spawner.shutdown();
            }
        }
    }

    pub(crate) fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        match self {
            Spawner::Basic(spawner) => spawner.spawn(future),
            #[cfg(feature = "rt-multi-thread")]
            Spawner::ThreadPool(spawner) => spawner.spawn(future),
        }
    }
}

cfg_metrics! {
    use crate::runtime::{SchedulerMetrics, WorkerMetrics};

    impl Spawner {
        pub(crate) fn num_workers(&self) -> usize {
            match self {
                Spawner::Basic(_) => 1,
                #[cfg(feature = "rt-multi-thread")]
                Spawner::ThreadPool(spawner) => spawner.num_workers(),
            }
        }

        pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
            match self {
                Spawner::Basic(spawner) => spawner.scheduler_metrics(),
                #[cfg(feature = "rt-multi-thread")]
                Spawner::ThreadPool(spawner) => spawner.scheduler_metrics(),
            }
        }

        pub(crate) fn worker_metrics(&self, worker: usize) -> &WorkerMetrics {
            match self {
                Spawner::Basic(spawner) => spawner.worker_metrics(worker),
                #[cfg(feature = "rt-multi-thread")]
                Spawner::ThreadPool(spawner) => spawner.worker_metrics(worker),
            }
        }

        pub(crate) fn injection_queue_depth(&self) -> usize {
            match self {
                Spawner::Basic(spawner) => spawner.injection_queue_depth(),
                #[cfg(feature = "rt-multi-thread")]
                Spawner::ThreadPool(spawner) => spawner.injection_queue_depth(),
            }
        }

        pub(crate) fn worker_local_queue_depth(&self, worker: usize) -> usize {
            match self {
                Spawner::Basic(spawner) => spawner.worker_metrics(worker).queue_depth(),
                #[cfg(feature = "rt-multi-thread")]
                Spawner::ThreadPool(spawner) => spawner.worker_local_queue_depth(worker),
            }
        }
    }
}