From 49923acb3f9b33f21861f7826edc2604adf1507b Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 23 Jan 2020 11:03:15 +0100 Subject: [PATCH] Print thread id instead of hardcoding numbers --- src/interrupts.rs | 2 +- src/main.rs | 28 ++++++++-------------------- src/threads.rs | 22 ++++++++++++++++++---- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/interrupts.rs b/src/interrupts.rs index 089d0884..e8508d34 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -77,7 +77,7 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptSt PICS.lock() .notify_end_of_interrupt(InterruptIndex::Timer.as_u8()); } - crate::threads::scheduler(); + crate::threads::schedule(); } extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) { diff --git a/src/main.rs b/src/main.rs index f25f06fd..5fe62f59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,12 +56,12 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { use blog_os::threads::{create_thread, create_thread_from_closure}; - create_thread(thread_1, 2, &mut mapper, &mut frame_allocator); - create_thread(thread_2, 2, &mut mapper, &mut frame_allocator); - create_thread(thread_3, 2, &mut mapper, &mut frame_allocator); + for i in 0..10 { + create_thread(thread, 2, &mut mapper, &mut frame_allocator); + } create_thread_from_closure( || loop { - print!("4"); + print!("{}", blog_os::threads::current_thread_id().as_u64()); x86_64::instructions::hlt(); }, 2, @@ -70,26 +70,14 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { ); println!("It did not crash!"); + thread(); + blog_os::hlt_loop(); } -fn thread_1() -> ! { +fn thread() -> ! { loop { - print!("1"); - x86_64::instructions::hlt(); - } -} - -fn thread_2() -> ! { - loop { - print!("2"); - x86_64::instructions::hlt(); - } -} - -fn thread_3() -> ! { - loop { - print!("3"); + print!("{}", blog_os::threads::current_thread_id().as_u64()); x86_64::instructions::hlt(); } } diff --git a/src/threads.rs b/src/threads.rs index fb2632d7..4a938149 100644 --- a/src/threads.rs +++ b/src/threads.rs @@ -8,6 +8,12 @@ static SCHEDULER: spin::Mutex> = spin::Mutex::new(None); #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ThreadId(pub u64); +impl ThreadId { + pub fn as_u64(&self) -> u64 { + self.0 + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct StackPointer(VirtAddr); @@ -165,7 +171,7 @@ global_asm!( " ); -pub fn scheduler() { +pub fn schedule() { let next = SCHEDULER .try_lock() .and_then(|mut scheduler| scheduler.as_mut().and_then(|s| s.next_thread())); @@ -174,6 +180,10 @@ pub fn scheduler() { } } +pub fn with_scheduler(f: F) -> T where F: FnOnce(&mut Scheduler) -> T { + f(SCHEDULER.lock().get_or_insert_with(Scheduler::new)) +} + static PAUSED_THREADS: spin::Mutex>> = spin::Mutex::new(None); #[no_mangle] @@ -181,7 +191,7 @@ pub extern "C" fn add_paused_thread(stack_pointer: u64, new_thread_id: u64) { let stack_pointer = StackPointer(VirtAddr::new(stack_pointer)); let new_thread_id = ThreadId(new_thread_id); - SCHEDULER.lock().get_or_insert_with(Scheduler::new).add_paused_thread(stack_pointer, new_thread_id); + with_scheduler(|s| s.add_paused_thread(stack_pointer, new_thread_id)); } pub fn create_thread( @@ -191,7 +201,7 @@ pub fn create_thread( frame_allocator: &mut impl FrameAllocator, ) -> Result<(), ()> { let thread = Thread::create(entry_point, stack_size, mapper, frame_allocator)?; - SCHEDULER.lock().get_or_insert_with(Scheduler::new).add_new_thread(thread); + with_scheduler(|s| s.add_new_thread(thread)); Ok(()) } @@ -204,10 +214,14 @@ pub fn create_thread_from_closure( F: FnOnce() -> ! + 'static + Send + Sync, { let thread = Thread::create_from_closure(closure, stack_size, mapper, frame_allocator)?; - SCHEDULER.lock().get_or_insert_with(Scheduler::new).add_new_thread(thread); + with_scheduler(|s| s.add_new_thread(thread)); Ok(()) } +pub fn current_thread_id() -> ThreadId { + with_scheduler(|s| s.current_thread_id()) +} + type ThreadClosure = alloc::boxed::Box !>; #[no_mangle]