From 11a0eb679c165f64a9bb82d2fc89ed0e29eda137 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 23 Jan 2020 08:50:35 +0100 Subject: [PATCH] Fix race condition The first timer interrupt might occur before the heap is initialized. With lazy_static, this causes an allocation failure since the VecDeque is allocated when it's accessed the first time. This commit fixes this by only initializing VecDeque in `add_thread`. --- src/multitasking.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/multitasking.rs b/src/multitasking.rs index ef2a4454..9be2a0bd 100644 --- a/src/multitasking.rs +++ b/src/multitasking.rs @@ -1,5 +1,4 @@ use alloc::collections::VecDeque; -use lazy_static::lazy_static; use x86_64::structures::paging::{FrameAllocator, Mapper, Size4KiB}; use x86_64::VirtAddr; @@ -17,15 +16,15 @@ pub unsafe fn context_switch(stack_pointer: VirtAddr) { } pub fn scheduler() { - let next = PAUSED_THREADS.try_lock().and_then(|mut t| t.pop_front()); + let next = PAUSED_THREADS.try_lock().and_then(|mut paused_threads| { + paused_threads.as_mut().and_then(|threads| threads.pop_front()) + }); if let Some(next) = next { unsafe { context_switch(next) }; } } -lazy_static! { - static ref PAUSED_THREADS: spin::Mutex> = spin::Mutex::new(VecDeque::new()); -} +static PAUSED_THREADS: spin::Mutex>> = spin::Mutex::new(None); #[no_mangle] fn add_paused_thread(stack_pointer: VirtAddr) { @@ -33,7 +32,7 @@ fn add_paused_thread(stack_pointer: VirtAddr) { } fn add_thread(stack_pointer: VirtAddr) { - PAUSED_THREADS.lock().push_back(stack_pointer); + PAUSED_THREADS.lock().get_or_insert_with(VecDeque::new).push_back(stack_pointer); } pub fn create_thread(