Skip to content

Commit

Permalink
Refactor scheduler suspension.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyma98 committed Aug 18, 2024
1 parent 5bfa856 commit fb3a655
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 338 deletions.
6 changes: 3 additions & 3 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::{

use super::{
interrupt::{svc, trap_frame::TrapFrame},
schedule::scheduler,
schedule::scheduler::Scheduler,
task,
unrecoverable::{self, Lethal},
};
Expand Down Expand Up @@ -165,7 +165,7 @@ pub(crate) fn initialize() {
fn die_if_not_in_svc() {
// Only perform sanity check after the scheduler has started, otherwise
// we may still be running with the bootstrap stack with MSP.
if !scheduler::has_started() {
if !Scheduler::has_started() {
return;
}

Expand All @@ -187,7 +187,7 @@ fn die_if_not_in_svc() {
fn die_if_not_in_svc_or_pendsv() {
// Only perform sanity check after the scheduler has started, otherwise
// we may still be running with the bootstrap stack with MSP.
if !scheduler::has_started() {
if !Scheduler::has_started() {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/boot/system_init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The module performs the initialization before running the user defined main
//! function.
use crate::{allocator, config, schedule::scheduler, task, unrecoverable::Lethal};
use crate::{allocator, config, schedule::scheduler::Scheduler, task, unrecoverable::Lethal};
use alloc::boxed::Box;
use core::sync::atomic::AtomicPtr;
use cortex_m::peripheral::scb::SystemHandler;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub(super) extern "C" fn system_start() -> ! {
// the idle task context and then perform a context switch to run the main
// task.
unsafe {
scheduler::start();
Scheduler::start();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/interrupt/context_switch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
config,
schedule::{current, scheduler},
schedule::{current, scheduler::Scheduler},
unrecoverable,
};
use core::arch::asm;
Expand Down Expand Up @@ -110,5 +110,5 @@ extern "C" fn pendsv_handler(ex_ret_lr: u32) {

// The `CUR_TASK_CTXT_PTR` pointer will be updated to reflect the next
// chosen task to run.
scheduler::pick_next();
Scheduler::pick_next();
}
11 changes: 6 additions & 5 deletions src/interrupt/svc_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use super::trap_frame::TrapFrame;
use crate::{
allocator, config,
schedule::scheduler,
schedule::scheduler::Scheduler,
task::{self, MoreStackReason, TaskLocalStorage},
unrecoverable::{self, Lethal},
};
Expand All @@ -29,6 +29,9 @@ pub(crate) enum SVCNum {
/// The calling task wants to get off from CPU. A task may voluntarily
/// yield the CPU or it may be forced to yield when becoming blocked on a
/// synchronization primitive.
///
/// Note that the SVC handler does not directly perform a context switch.
/// Instead, a PendSV will be tail chained to perform it.
TaskYield = 1,
/// The task wants to terminate and release its task struct.
TaskDestroy = 2,
Expand Down Expand Up @@ -144,10 +147,8 @@ fn get_svc_num(tf: &TrapFrame) -> SVCNum {
/// the assembly code.
extern "C" fn svc_handler(tf: &mut TrapFrame, ctxt: &mut TaskSVCCtxt) {
match get_svc_num(tf) {
// Task wants to yield. Mark its state as ready so that the
// scheduler can schedule it later.
SVCNum::TaskYield => scheduler::yield_cur_task_from_isr(),
SVCNum::TaskDestroy => scheduler::destroy_current_task_and_schedule(),
SVCNum::TaskYield => Scheduler::yield_current_task_from_svc(),
SVCNum::TaskDestroy => Scheduler::drop_current_task_from_svc(),
SVCNum::TaskLessStack => task::less_stack(tf, ctxt),
SVCNum::TaskMoreStack => task::more_stack(tf, ctxt, MoreStackReason::Normal),
SVCNum::TaskMoreStackFromDrop => task::more_stack(tf, ctxt, MoreStackReason::Drop),
Expand Down
15 changes: 11 additions & 4 deletions src/schedule/current.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::scheduler::Scheduler;
use crate::{
sync::{self, RwLock},
sync::RwLock,
task::{Task, TaskCtxt},
unrecoverable,
};
Expand Down Expand Up @@ -69,7 +70,7 @@ where
F: FnOnce(Arc<Task>) -> R,
{
// Suspend the scheduler and lock the current task `Arc` in reader mode.
let _sched_suspend_guard = sync::suspend_scheduler();
let _sched_suspend_guard = Scheduler::suspend();
let read_guard = CUR_TASK.read();

// Run the closure.
Expand All @@ -90,7 +91,7 @@ where
F: FnOnce(&Task) -> R,
{
// Suspend the scheduler and lock the current task `Arc` in reader mode.
let _sched_suspend_guard = sync::suspend_scheduler();
let _sched_suspend_guard = Scheduler::suspend();
let read_guard = CUR_TASK.read();

// Run the closure.
Expand All @@ -102,7 +103,7 @@ where
}

/// Return if the code is currently executing in an interrupt service routine
/// (ISR) context.
/// (ISR), in contrast to in a task.
pub(crate) fn is_in_isr_context() -> bool {
let ipsr: u32;

Expand All @@ -117,6 +118,12 @@ pub(crate) fn is_in_isr_context() -> bool {
ipsr != 0
}

/// Return if the code is currently executing in a task, in contrast to in an
/// interrupt service routine (ISR).
pub(crate) fn is_in_task_context() -> bool {
!is_in_isr_context()
}

/// Return if the code is currently executing in the PendSV exception context.
pub(crate) fn is_in_pendsv_context() -> bool {
let ipsr: u32;
Expand Down
Loading

0 comments on commit fb3a655

Please sign in to comment.