Skip to main content Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. Rust
  4. pw_kernel
  5. src
  6. kernel/scheduler/algorithm/global_queue.rs

kernel/scheduler/algorithm/
global_queue.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::Thread;
19use crate::scheduler::algorithm::priority_queue_set::PriorityQueueSet;
20use crate::scheduler::algorithm::{RescheduleReason, SchedulerAlgorithmInterface};
21use crate::scheduler::thread::ThreadOwner;
22
23/// Scheduler backend with a single global [`PriorityQueueSet`] shared by all
24/// cores.
25///
26/// Preempted and terminating threads are placed at the head of their
27/// priority's queue so they resume (or are reaped) before peers of equal
28/// priority; started, ticked, and woken threads go to the tail for round-robin
29/// fairness.
30pub struct GlobalQueueScheduler<K: Kernel> {
31    queue: PriorityQueueSet<K>,
32}
33
34impl<K: Kernel> GlobalQueueScheduler<K> {
35    #[must_use]
36    pub const fn new() -> Self {
37        Self {
38            queue: PriorityQueueSet::new(),
39        }
40    }
41}
42
43impl<K: Kernel> SchedulerAlgorithmInterface<K> for GlobalQueueScheduler<K> {
44    #[inline]
45    fn schedule_thread(&mut self, mut thread: ForeignBox<Thread<K>>, reason: RescheduleReason) {
46        thread.owner = ThreadOwner::Scheduler;
47
48        match reason {
49            RescheduleReason::Preempted | RescheduleReason::Terminated => {
50                self.queue.push_front(thread);
51            }
52            RescheduleReason::Started | RescheduleReason::Ticked | RescheduleReason::Woken => {
53                self.queue.push_back(thread);
54            }
55        }
56    }
57
58    #[inline]
59    fn pop_next_thread(&mut self) -> Option<ForeignBox<Thread<K>>> {
60        self.queue.pop_head()
61    }
62}