From 49f1821f9c7d6e0d1814abd4c712cd2568ea9738 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Sun, 1 Jan 2023 15:50:57 +0100 Subject: [PATCH] thread yield refacto Signed-off-by: Julien CLEMENT --- src/fs/iso/mod.rs | 2 -- src/proc/scheduler/mod.rs | 6 ++++-- src/proc/thread/mod.rs | 14 +++++++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/fs/iso/mod.rs b/src/fs/iso/mod.rs index 8cd48dd..448e9cc 100644 --- a/src/fs/iso/mod.rs +++ b/src/fs/iso/mod.rs @@ -1,8 +1,6 @@ mod fd; pub mod iso9660; -use crate::serial_println; - use crate::drivers::atapi::read_block; use crate::fd::FDt; use crate::utils::unserialize; diff --git a/src/proc/scheduler/mod.rs b/src/proc/scheduler/mod.rs index 1c7ec52..d3f8b1a 100644 --- a/src/proc/scheduler/mod.rs +++ b/src/proc/scheduler/mod.rs @@ -19,6 +19,8 @@ lazy_static! { pub type Threadt = Arc>; +pub const K_THREAD_ID: ThreadId = ThreadId(0); // Kernel main thread identifier + struct ThreadStream { ids: ArrayQueue, waker: AtomicWaker @@ -72,7 +74,7 @@ impl Scheduler { thread_queue: ThreadStream::new(), }; let k_thread: Thread = Thread { - id: ThreadId(0), + id: K_THREAD_ID, entry_point: 0, started: true, rsp: 0, @@ -99,7 +101,7 @@ impl Scheduler { if self.threads.insert(thread_id, thread).is_some() { panic!("Duplicate thread ID") } - if thread_id != ThreadId(0) { + if thread_id != K_THREAD_ID { self.thread_queue.register(thread_id); } } diff --git a/src/proc/thread/mod.rs b/src/proc/thread/mod.rs index 29de36a..e801f5d 100644 --- a/src/proc/thread/mod.rs +++ b/src/proc/thread/mod.rs @@ -1,7 +1,7 @@ use crate::println; use crate::utils::mutex::AsyncMutex; -use super::scheduler::SCHEDULER; +use super::scheduler::{SCHEDULER, K_THREAD_ID}; use core::arch::asm; use core::sync::atomic::{AtomicU64, Ordering}; @@ -27,14 +27,22 @@ impl ThreadId { pub fn exit() { println!("Exiting thread"); + { + let mut scheduler = SCHEDULER.try_lock().unwrap(); + scheduler.exit(*RUNNING_THREAD.try_lock().unwrap()); + } // Drop scheduler mutex guard + + resume_k_thread(); +} + +pub fn resume_k_thread() { let k_thread: *mut Thread; { let mut scheduler = SCHEDULER.try_lock().unwrap(); k_thread = scheduler - .get_thread(ThreadId(0)) + .get_thread(K_THREAD_ID) .unwrap() .as_ptr(); - scheduler.exit(*RUNNING_THREAD.try_lock().unwrap()); } // Drop scheduler mutex guard unsafe {