kernel/scheduler/algorithm/interface.rs
1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15use foreign_box::ForeignBox;
16
17use crate::Kernel;
18use crate::scheduler::{Priority, Thread};
19
20/// The reason a thread is being rescheduled. This can be used as a hint to the
21/// scheduling algorithm to determine where the thread should end up in the
22/// queue of pending threads.
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum RescheduleReason {
25 /// The currently running thread has been preempted causing it to be re-added to the scheduler.
26 Preempted,
27 /// A scheduler tick occurred causing the currently running thread to be re-added to the scheduler.
28 Ticked,
29 /// A currently waiting thread has been unblocked.
30 Woken,
31 /// A thread has been started and is being added to the scheduler for the first time.
32 Started,
33 /// A thread has been terminated
34 Terminated,
35}
36
37/// Per-thread state used by the scheduling algorithm.
38pub struct SchedulerAlgorithmThreadState {
39 /// The current priority of the thread.
40 ///
41 /// Kept private so the priority cannot be casually mutated in place while
42 /// the thread is indexed in a `PriorityQueueSet`; priority changes must go
43 /// through the scheduling algorithm.
44 current_priority: Priority,
45}
46
47impl SchedulerAlgorithmThreadState {
48 #[must_use]
49 pub const fn new(current_priority: Priority) -> Self {
50 Self { current_priority }
51 }
52
53 #[inline]
54 #[must_use]
55 pub(crate) const fn priority(&self) -> Priority {
56 self.current_priority
57 }
58}
59
60/// Common interface implemented by scheduling algorithm backends.
61///
62/// Backends are embedded in the kernel's static scheduler state and accessed
63/// from any core under the scheduler lock (hence `'static + Send + Sync`).
64/// All methods are called with that lock held; implementations must not take
65/// additional locks or block.
66pub trait SchedulerAlgorithmInterface<K: Kernel>: 'static + Send + Sync {
67 /// Takes ownership of `thread` and makes it eligible to run.
68 ///
69 /// `reason` is a placement hint describing why the thread is being
70 /// enqueued (e.g. preempted threads may be placed ahead of peers of equal
71 /// priority).
72 fn schedule_thread(&mut self, thread: ForeignBox<Thread<K>>, reason: RescheduleReason);
73
74 /// Removes and returns the highest-priority ready thread, or `None` if no
75 /// thread is ready.
76 fn pop_next_thread(&mut self) -> Option<ForeignBox<Thread<K>>>;
77}