kernel/scheduler.rs
1// Copyright 2025 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
15//! Kernel Scheduler / Context Switching engine
16//!
17//! The module provides several interfaces for managing context switches. Each is
18//! different in its contract:
19//! - `block()` is used when a thread is taken out of the scheduler algorithm
20//! and is managed by another source such as a wait queue. These threads
21//! should be in the [`State::WaitingInterruptible`] or
22//! [`State::WaitingNonInterruptible`] state.
23//! - `try_reschedule()` is used when there is a change to the scheduling state
24//! (such as waking a thread). Depending on the local thread's
25//! [`PreemptDisableGuard`] state and the scheduler state, this may trigger an
26//! immediate context switch or it may defer it. This is useful for code which
27//! may or may not be called in an interrupt context.
28//! - `try_deferred_reschedule()` is used to trigger a context switch if one was
29//! deferred by `try_reschedule()`. Notably, this does not guarantee a context
30//! switch if the [`PreemptDisableGuard`] state is not active but does
31//! guarantee forward progress. This comes into play for architectures like
32//! M-profile ARM which handles the context switch in an exception handler
33//! after an interrupt has returned.
34
35pub(crate) mod affinity;
36pub(crate) mod algorithm;
37pub(crate) mod core;
38pub(crate) mod locks;
39pub(crate) mod priority;
40pub(crate) mod process;
41pub(crate) mod thread;
42pub(crate) mod timer;
43pub(crate) mod wait_queue;
44
45pub(crate) use core::block;
46pub use core::{
47 JoinResult, PreemptDisableGuard, ProcessJoinResult, ProcessTryJoinResult, SchedulerState,
48 ThreadLocalState, TryJoinResult, add_process, bootstrap_scheduler, exit_thread,
49 handle_terminal_exception, initialize, sleep_until, start_thread, tick,
50 try_deferred_reschedule, yield_timeslice,
51};
52
53pub use affinity::{AffinityMask, CoreId};
54pub use algorithm::RescheduleReason;
55pub use locks::{SchedLockGuard, WaitQueueLock, WaitQueueLockGuard};
56pub use priority::Priority;
57pub use process::{Process, ProcessHandle};
58pub use thread::{
59 Stack, StackStorage, StackStorageExt, State, Thread, ThreadArg, ThreadHandle, ThreadState,
60 init_thread_in,
61};
62pub use timer::TimerQueue;
63pub use wait_queue::{WaitQueue, WaitType, WakeList, WakeResult};