Better user programs with a special std. Sleep and exit are calling scheduler instead of wfi.

This commit is contained in:
2026-02-21 18:29:27 +01:00
parent 235f17e7cf
commit 8a8034bd11
25 changed files with 263 additions and 210 deletions

View File

@@ -1,9 +1,12 @@
use core::{arch::riscv64::wfi, time::Duration};
use core::time::Duration;
use alloc::{format, string::String};
use alloc::{boxed::Box, format, string::String, vec::Vec};
use bffs::{io::Read, path::Path};
use shared::syscall::exit;
use crate::{
scheduler::{ACTIVE_PID, PROCESS_COUNT, PROCESS_TABLE},
fs::FILE_SYSTEM,
scheduler::{scheduler_without_ret, ACTIVE_PID, PROCESS_COUNT, PROCESS_TABLE},
time::elapsed_time_since_startup,
};
@@ -54,6 +57,21 @@ impl core::fmt::Debug for Process {
}
}
pub fn create_process_from_file<'a, T: Into<Path<'a>>>(path: T) -> i64 {
let path = path.into();
let name = path.as_str();
let mut bin = FILE_SYSTEM.open_file(path).unwrap();
let mut content: Vec<u8> = Vec::new();
bin.read_to_end(&mut content).unwrap();
let test =
unsafe { core::mem::transmute::<*const u8, extern "C" fn()>(Vec::leak(content).as_ptr()) };
let test = Box::leak(Box::new(move || {
test();
}));
create_process(test, name)
}
pub fn create_process<T: Into<String>, F: Fn()>(code: &'static F, name: T) -> i64 {
let mut next_pid = 0;
while next_pid < PROCESS_COUNT && unsafe { PROCESS_TABLE[next_pid].state != ProcessState::Dead }
@@ -82,27 +100,21 @@ pub fn create_process<T: Into<String>, F: Fn()>(code: &'static F, name: T) -> i6
extern "C" fn process_launcher(code: *const &dyn Fn()) {
unsafe { (*code)() };
terminate_process();
// User code didn't exit before the end of its execution, so we call the exit syscall ourselves
exit();
}
fn terminate_process() {
pub fn exit_process(interrupt_context: &mut *mut ExecutionContext) {
unsafe {
PROCESS_TABLE[ACTIVE_PID].state = ProcessState::Dead;
}
loop {}
// unsafe {
// wfi();
// }
// scheduler();
scheduler_without_ret(interrupt_context)
}
pub fn sleep(duration: Duration) {
pub fn sleep(duration: Duration, interrupt_context: &mut *mut ExecutionContext) {
unsafe {
PROCESS_TABLE[ACTIVE_PID].wake_time = elapsed_time_since_startup() + duration;
PROCESS_TABLE[ACTIVE_PID].state = ProcessState::Asleep;
}
unsafe {
wfi();
}
// scheduler();
scheduler_without_ret(interrupt_context)
}