Switch current_thread_id before context switch

This commit is contained in:
Philipp Oppermann
2020-01-28 11:29:21 +01:00
parent 0caf5c351e
commit 87719f2260
3 changed files with 10 additions and 10 deletions

View File

@@ -41,11 +41,11 @@ impl Stack {
} }
} }
pub unsafe fn context_switch_to(thread_id: ThreadId, stack_pointer: VirtAddr) { pub unsafe fn context_switch_to(new_stack_pointer: VirtAddr, prev_thread_id: ThreadId) {
asm!( asm!(
"call asm_context_switch" "call asm_context_switch"
: :
: "{rdi}"(stack_pointer), "{rsi}"(thread_id) : "{rdi}"(new_stack_pointer), "{rsi}"(prev_thread_id)
: "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rpb", "r8", "r9", "r10", : "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rpb", "r8", "r9", "r10",
"r11", "r12", "r13", "r14", "r15", "rflags", "memory" "r11", "r12", "r13", "r14", "r15", "rflags", "memory"
: "intel", "volatile" : "intel", "volatile"
@@ -72,8 +72,8 @@ global_asm!(
); );
#[no_mangle] #[no_mangle]
pub extern "C" fn add_paused_thread(paused_stack_pointer: VirtAddr, new_thread_id: ThreadId) { pub extern "C" fn add_paused_thread(paused_stack_pointer: VirtAddr, paused_thread_id: ThreadId) {
with_scheduler(|s| s.add_paused_thread(paused_stack_pointer, new_thread_id)); with_scheduler(|s| s.add_paused_thread(paused_stack_pointer, paused_thread_id));
} }
#[naked] #[naked]

View File

@@ -10,8 +10,8 @@ pub fn invoke_scheduler() {
let next = SCHEDULER let next = SCHEDULER
.try_lock() .try_lock()
.and_then(|mut scheduler| scheduler.as_mut().and_then(|s| s.schedule())); .and_then(|mut scheduler| scheduler.as_mut().and_then(|s| s.schedule()));
if let Some((next_id, next_stack_pointer)) = next { if let Some((next_stack_pointer, prev_thread_id)) = next {
unsafe { context_switch::context_switch_to(next_id, next_stack_pointer) }; unsafe { context_switch::context_switch_to(next_stack_pointer, prev_thread_id) };
} }
} }

View File

@@ -28,7 +28,7 @@ impl Scheduler {
self.paused_threads.pop_front() self.paused_threads.pop_front()
} }
pub fn schedule(&mut self) -> Option<(ThreadId, VirtAddr)> { pub fn schedule(&mut self) -> Option<(VirtAddr, ThreadId)> {
if let Some(next_id) = self.next_thread() { if let Some(next_id) = self.next_thread() {
let next_thread = self let next_thread = self
.threads .threads
@@ -38,7 +38,8 @@ impl Scheduler {
.stack_pointer() .stack_pointer()
.take() .take()
.expect("paused thread has no stack pointer"); .expect("paused thread has no stack pointer");
Some((next_id, next_stack_pointer)) let prev_thread_id = mem::replace(&mut self.current_thread_id, next_thread.id());
Some((next_stack_pointer, prev_thread_id))
} else { } else {
None None
} }
@@ -47,9 +48,8 @@ impl Scheduler {
pub(super) fn add_paused_thread( pub(super) fn add_paused_thread(
&mut self, &mut self,
paused_stack_pointer: VirtAddr, paused_stack_pointer: VirtAddr,
next_thread_id: ThreadId, paused_thread_id: ThreadId,
) { ) {
let paused_thread_id = mem::replace(&mut self.current_thread_id, next_thread_id);
let paused_thread = self let paused_thread = self
.threads .threads
.get_mut(&paused_thread_id) .get_mut(&paused_thread_id)