Skip to main content

ax_task/sched/system/cpu/local/
mod.rs

1//! CPU-owner scheduler state and switch continuation.
2
3use super::*;
4use crate::sched::system::task_system::TaskSystem;
5mod dispatch_state;
6mod drain_state;
7mod owner_deadline;
8mod owner_dispatch;
9mod owner_idle;
10
11pub(crate) use owner_deadline::{
12    HardTimerServiceClaim, HardTimerServiceStep, KtimerServiceClaim,
13    SchedulerDeadlineRqObservation, SoftTimerExpireBatch,
14};
15
16use crate::sched::system::cpu::remote::SchedulerDeadlinePublicationState;
17
18/// Outer owner-CPU transition that requested a fresh scheduler deadline derivation.
19#[derive(Clone, Copy)]
20#[repr(usize)]
21pub(crate) enum SchedulerDeadlineDerivationSource {
22    ClockEvent,
23    KtimerService,
24    Enqueue,
25    Placement,
26    ScheduleSelection,
27    ScheduleNoSwitch,
28}
29
30/// Scheduler state that is created explicitly and mutated only by its owner CPU.
31///
32/// The object is `!Unpin`; runtimes store it in per-CPU pinned allocations and
33/// publish it only after registration has completed.
34#[derive(Debug)]
35pub struct CpuLocal {
36    owner: CpuId,
37    remote: Arc<CpuRemote>,
38    rt_bandwidth: Arc<RootRtBandwidth>,
39    dispatch: dispatch_state::OwnerDispatchState,
40    drain: drain_state::OwnerDrainScratch,
41    _pinned: PhantomPinned,
42}
43
44impl CpuLocal {
45    pub(crate) fn create(
46        owner: CpuId,
47        config: TaskSystemConfig,
48        remote: Arc<CpuRemote>,
49        rt_bandwidth: Arc<RootRtBandwidth>,
50    ) -> Pin<Box<Self>> {
51        debug_assert_eq!(owner, remote.owner());
52        Box::pin(Self {
53            owner,
54            remote,
55            rt_bandwidth,
56            dispatch: dispatch_state::OwnerDispatchState::new(config),
57            drain: drain_state::OwnerDrainScratch::new(config),
58            _pinned: PhantomPinned,
59        })
60    }
61
62    /// Returns the logical processor that exclusively owns the run queue.
63    pub const fn owner(&self) -> CpuId {
64        self.owner
65    }
66
67    /// Returns whether registration and online publication have completed.
68    pub fn is_online(&self) -> bool {
69        self.remote.is_online()
70    }
71
72    pub(crate) fn remote(&self) -> &Arc<CpuRemote> {
73        &self.remote
74    }
75
76    /// Borrows the owner CPU's immutable runqueue endpoint independently of
77    /// the pinned `CpuLocal` dispatch fields.
78    ///
79    /// # Safety
80    ///
81    /// The caller must retain the owner capability for the complete borrow and
82    /// must not drop or replace this `CpuLocal`. The endpoint is owned by the
83    /// same pinned allocation and remains immutable after CPU publication.
84    pub(crate) unsafe fn remote_for_owner(&self) -> &'static CpuRemote {
85        // SAFETY: the owner capability pins this allocation and its Arc-backed
86        // endpoint until the caller releases every derived transaction.
87        unsafe { &*Arc::as_ptr(&self.remote) }
88    }
89}