Skip to main content

moirai_executor/schedule/class/
mod.rs

1//! Work class markers for compile-time scheduler routing.
2
3mod sealed {
4    pub trait Sealed {}
5}
6
7/// Compile-time routing contract for a scheduler work class.
8///
9/// Implementors are zero-sized marker types. The scheduler monomorphizes
10/// dispatch per marker and uses the associated constants for deterministic
11/// worker selection without storing a runtime policy object.
12pub trait WorkClass: sealed::Sealed + Send + Sync + 'static {
13    /// Offset mixed into round-robin worker selection for this work class.
14    const AFFINITY_OFFSET: usize;
15
16    /// Worker offset used for serial handoff when no queued backlog exists.
17    ///
18    /// Serial result-bearing submit/join loops benefit from a single stable
19    /// worker cache footprint. The value is an associated constant so the
20    /// scheduler monomorphizes the routing decision per work class.
21    const SERIAL_AFFINITY_OFFSET: usize;
22
23    /// Stable diagnostic name for this work class.
24    const NAME: &'static str;
25
26    /// Whether this work class is eligible for asynchronous lane routing.
27    const USES_ASYNC_LANE: bool;
28
29    /// Whether this work class uses the dedicated bounded blocking lane.
30    const USES_BLOCKING_LANE: bool;
31}
32
33/// CPU-bound synchronous task marker.
34#[derive(Debug, Clone, Copy, Default)]
35pub struct SyncTask;
36
37/// Future-driving task marker.
38#[derive(Debug, Clone, Copy, Default)]
39pub struct AsyncTask;
40
41/// Potentially blocking task marker.
42#[derive(Debug, Clone, Copy, Default)]
43pub struct BlockingTask;
44
45impl sealed::Sealed for SyncTask {}
46impl sealed::Sealed for AsyncTask {}
47impl sealed::Sealed for BlockingTask {}
48
49impl WorkClass for SyncTask {
50    const AFFINITY_OFFSET: usize = 0;
51    const SERIAL_AFFINITY_OFFSET: usize = 0;
52    const NAME: &'static str = "sync";
53    const USES_ASYNC_LANE: bool = false;
54    const USES_BLOCKING_LANE: bool = false;
55}
56
57impl WorkClass for AsyncTask {
58    const AFFINITY_OFFSET: usize = 1;
59    const SERIAL_AFFINITY_OFFSET: usize = Self::AFFINITY_OFFSET;
60    const NAME: &'static str = "async";
61    const USES_ASYNC_LANE: bool = true;
62    const USES_BLOCKING_LANE: bool = false;
63}
64
65impl WorkClass for BlockingTask {
66    const AFFINITY_OFFSET: usize = 2;
67    const SERIAL_AFFINITY_OFFSET: usize = Self::AFFINITY_OFFSET;
68    const NAME: &'static str = "blocking";
69    const USES_ASYNC_LANE: bool = false;
70    const USES_BLOCKING_LANE: bool = true;
71}
72
73#[cfg(test)]
74mod tests {
75    use super::{AsyncTask, BlockingTask, SyncTask};
76
77    #[test]
78    fn work_class_markers_are_zero_sized() {
79        assert_eq!(core::mem::size_of::<SyncTask>(), 0);
80        assert_eq!(core::mem::size_of::<AsyncTask>(), 0);
81        assert_eq!(core::mem::size_of::<BlockingTask>(), 0);
82    }
83}