Skip to content

Commit 3dca73e

Browse files
committed
simplify scheduler by using const initialization of static elements
1 parent 6792069 commit 3dca73e

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

src/scheduler/mod.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ pub const TASK_TIME_SLICE: u64 = 10_000;
2828

2929
static NO_TASKS: AtomicU32 = AtomicU32::new(0);
3030
/// Map between Core ID and per-core scheduler
31-
static mut SCHEDULERS: Option<BTreeMap<CoreId, &PerCoreScheduler>> = None;
31+
static mut SCHEDULERS: BTreeMap<CoreId, &PerCoreScheduler> = BTreeMap::new();
3232
/// Map between Task ID and Task Control Block
33-
static TASKS: SpinlockIrqSave<Option<BTreeMap<TaskId, VecDeque<TaskHandle>>>> =
34-
SpinlockIrqSave::new(None);
33+
static TASKS: SpinlockIrqSave<BTreeMap<TaskId, VecDeque<TaskHandle>>> =
34+
SpinlockIrqSave::new(BTreeMap::new());
3535

3636
/// Unique identifier for a core.
3737
pub type CoreId = u32;
@@ -95,11 +95,7 @@ impl PerCoreScheduler {
9595
// Add it to the task lists.
9696
let wakeup = {
9797
let mut input_locked = get_scheduler(core_id).input.lock();
98-
TASKS
99-
.lock()
100-
.as_mut()
101-
.unwrap()
102-
.insert(tid, VecDeque::with_capacity(1));
98+
TASKS.lock().insert(tid, VecDeque::with_capacity(1));
10399
NO_TASKS.fetch_add(1, Ordering::SeqCst);
104100

105101
if core_id != core_scheduler().core_id {
@@ -183,11 +179,7 @@ impl PerCoreScheduler {
183179
// Add it to the task lists.
184180
let wakeup = {
185181
let mut input_locked = get_scheduler(core_id).input.lock();
186-
TASKS
187-
.lock()
188-
.as_mut()
189-
.unwrap()
190-
.insert(tid, VecDeque::with_capacity(1));
182+
TASKS.lock().insert(tid, VecDeque::with_capacity(1));
191183
NO_TASKS.fetch_add(1, Ordering::SeqCst);
192184
if core_id != core_scheduler().core_id {
193185
input_locked.new_tasks.push_back(clone_task.clone());
@@ -345,7 +337,7 @@ impl PerCoreScheduler {
345337
debug!("Cleaning up task {}", id);
346338

347339
// wakeup tasks, which are waiting for task with the identifier id
348-
match TASKS.lock().as_mut().unwrap().remove(&id) {
340+
match TASKS.lock().remove(&id) {
349341
Some(mut queue) => {
350342
while let Some(task) = queue.pop_front() {
351343
result = true;
@@ -502,22 +494,17 @@ impl PerCoreScheduler {
502494

503495
fn get_tid() -> TaskId {
504496
static TID_COUNTER: AtomicU32 = AtomicU32::new(0);
505-
let mut guard = TASKS.lock();
497+
let guard = TASKS.lock();
506498

507499
loop {
508500
let id = TaskId::from(TID_COUNTER.fetch_add(1, Ordering::SeqCst));
509-
if !guard.as_mut().unwrap().contains_key(&id) {
501+
if !guard.contains_key(&id) {
510502
return id;
511503
}
512504
}
513505
}
514506

515-
pub fn init() {
516-
unsafe {
517-
SCHEDULERS = Some(BTreeMap::new());
518-
}
519-
*TASKS.lock() = Some(BTreeMap::new());
520-
}
507+
pub fn init() {}
521508

522509
#[inline]
523510
pub fn abort() {
@@ -532,11 +519,7 @@ pub fn add_current_core() {
532519
let idle_task = Rc::new(RefCell::new(Task::new_idle(tid, core_id)));
533520

534521
// Add the ID -> Task mapping.
535-
TASKS
536-
.lock()
537-
.as_mut()
538-
.unwrap()
539-
.insert(tid, VecDeque::with_capacity(1));
522+
TASKS.lock().insert(tid, VecDeque::with_capacity(1));
540523
// Initialize a scheduler for this core.
541524
debug!(
542525
"Initializing scheduler for core {} with idle task {}",
@@ -557,14 +540,14 @@ pub fn add_current_core() {
557540
let scheduler = Box::into_raw(boxed_scheduler);
558541
set_core_scheduler(scheduler);
559542
unsafe {
560-
SCHEDULERS.as_mut().unwrap().insert(core_id, &(*scheduler));
543+
SCHEDULERS.insert(core_id, &(*scheduler));
561544
}
562545
}
563546

564547
#[inline]
565548
fn get_scheduler(core_id: CoreId) -> &'static PerCoreScheduler {
566549
// Get the scheduler for the desired core.
567-
if let Some(result) = unsafe { SCHEDULERS.as_ref().unwrap().get(&core_id) } {
550+
if let Some(result) = unsafe { SCHEDULERS.get(&core_id) } {
568551
result
569552
} else {
570553
panic!(
@@ -585,7 +568,7 @@ pub fn join(id: TaskId) -> Result<(), ()> {
585568

586569
{
587570
let mut guard = TASKS.lock();
588-
match guard.as_mut().unwrap().get_mut(&id) {
571+
match guard.get_mut(&id) {
589572
Some(queue) => {
590573
queue.push_back(core_scheduler.get_current_task_handle());
591574
core_scheduler.block_current_task(None);

0 commit comments

Comments
 (0)