Print thread id instead of hardcoding numbers

This commit is contained in:
Philipp Oppermann
2020-01-23 11:03:15 +01:00
parent f2b1f3a593
commit 49923acb3f
3 changed files with 27 additions and 25 deletions

View File

@@ -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) {

View File

@@ -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();
}
}

View File

@@ -8,6 +8,12 @@ static SCHEDULER: spin::Mutex<Option<Scheduler>> = 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, T>(f: F) -> T where F: FnOnce(&mut Scheduler) -> T {
f(SCHEDULER.lock().get_or_insert_with(Scheduler::new))
}
static PAUSED_THREADS: spin::Mutex<Option<VecDeque<VirtAddr>>> = 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<Size4KiB>,
) -> 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>(
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<dyn FnOnce() -> !>;
#[no_mangle]