thread yield refacto
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2023-01-01 15:50:57 +01:00
parent 563d97f372
commit 49f1821f9c
3 changed files with 15 additions and 7 deletions

@ -1,8 +1,6 @@
mod fd; mod fd;
pub mod iso9660; pub mod iso9660;
use crate::serial_println;
use crate::drivers::atapi::read_block; use crate::drivers::atapi::read_block;
use crate::fd::FDt; use crate::fd::FDt;
use crate::utils::unserialize; use crate::utils::unserialize;

@ -19,6 +19,8 @@ lazy_static! {
pub type Threadt = Arc<RefCell<Thread>>; pub type Threadt = Arc<RefCell<Thread>>;
pub const K_THREAD_ID: ThreadId = ThreadId(0); // Kernel main thread identifier
struct ThreadStream { struct ThreadStream {
ids: ArrayQueue<ThreadId>, ids: ArrayQueue<ThreadId>,
waker: AtomicWaker waker: AtomicWaker
@ -72,7 +74,7 @@ impl Scheduler {
thread_queue: ThreadStream::new(), thread_queue: ThreadStream::new(),
}; };
let k_thread: Thread = Thread { let k_thread: Thread = Thread {
id: ThreadId(0), id: K_THREAD_ID,
entry_point: 0, entry_point: 0,
started: true, started: true,
rsp: 0, rsp: 0,
@ -99,7 +101,7 @@ impl Scheduler {
if self.threads.insert(thread_id, thread).is_some() { if self.threads.insert(thread_id, thread).is_some() {
panic!("Duplicate thread ID") panic!("Duplicate thread ID")
} }
if thread_id != ThreadId(0) { if thread_id != K_THREAD_ID {
self.thread_queue.register(thread_id); self.thread_queue.register(thread_id);
} }
} }

@ -1,7 +1,7 @@
use crate::println; use crate::println;
use crate::utils::mutex::AsyncMutex; use crate::utils::mutex::AsyncMutex;
use super::scheduler::SCHEDULER; use super::scheduler::{SCHEDULER, K_THREAD_ID};
use core::arch::asm; use core::arch::asm;
use core::sync::atomic::{AtomicU64, Ordering}; use core::sync::atomic::{AtomicU64, Ordering};
@ -27,14 +27,22 @@ impl ThreadId {
pub fn exit() { pub fn exit() {
println!("Exiting thread"); 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 k_thread: *mut Thread;
{ {
let mut scheduler = SCHEDULER.try_lock().unwrap(); let mut scheduler = SCHEDULER.try_lock().unwrap();
k_thread = scheduler k_thread = scheduler
.get_thread(ThreadId(0)) .get_thread(K_THREAD_ID)
.unwrap() .unwrap()
.as_ptr(); .as_ptr();
scheduler.exit(*RUNNING_THREAD.try_lock().unwrap());
} // Drop scheduler mutex guard } // Drop scheduler mutex guard
unsafe { unsafe {