Skip to main content

async_runtime/
priority.rs

1use std::num::NonZeroUsize;
2
3/// Scheduling priority for tasks in the general worker pool.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum Priority {
6    /// Latency-sensitive work.
7    High,
8    /// Ordinary work.
9    Normal,
10    /// Work that may make progress at a lower rate.
11    Background,
12}
13
14/// Relative scheduling opportunities assigned to each priority.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct PriorityWeights {
17    high: NonZeroUsize,
18    normal: NonZeroUsize,
19    background: NonZeroUsize,
20}
21
22impl PriorityWeights {
23    /// Creates a set of non-zero weights.
24    pub const fn new(high: NonZeroUsize, normal: NonZeroUsize, background: NonZeroUsize) -> Self {
25        Self {
26            high,
27            normal,
28            background,
29        }
30    }
31
32    /// Returns the weight for high-priority work.
33    pub const fn high(self) -> NonZeroUsize {
34        self.high
35    }
36
37    /// Returns the weight for normal-priority work.
38    pub const fn normal(self) -> NonZeroUsize {
39        self.normal
40    }
41
42    /// Returns the weight for background work.
43    pub const fn background(self) -> NonZeroUsize {
44        self.background
45    }
46}
47
48impl Default for PriorityWeights {
49    fn default() -> Self {
50        Self::new(
51            NonZeroUsize::new(8).expect("8 is non-zero"),
52            NonZeroUsize::new(4).expect("4 is non-zero"),
53            NonZeroUsize::new(1).expect("1 is non-zero"),
54        )
55    }
56}