[Mammoth] Move thread syscalls to wrappers.

This commit is contained in:
Drew Galbraith 2024-07-28 19:48:05 -07:00
parent 612a5ac572
commit ca67da16e6
3 changed files with 51 additions and 46 deletions

View file

@ -7,17 +7,13 @@ use core::ffi::c_void;
pub type ThreadEntry = fn(*const c_void) -> ();
#[no_mangle]
extern "C" fn entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! {
debug!("Entry {:#p} arg1 {:#x}", entry_ptr, arg1 as u64);
extern "C" fn internal_entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! {
let entry = unsafe { *entry_ptr };
entry(arg1);
let _ = syscall::syscall(zion::kZionThreadExit, &zion::ZThreadExitReq {});
unreachable!();
syscall::thread_exit()
}
// TODO: Add a Drop implementation that kills this thread and drops its capability.
pub struct Thread<'a> {
cap: z_cap_t,
@ -27,35 +23,21 @@ pub struct Thread<'a> {
}
impl<'a> Thread<'a> {
pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Self {
let mut cap: z_cap_t = 0;
let req = zion::ZThreadCreateReq {
proc_cap: unsafe { crate::init::SELF_PROC_CAP },
thread_cap: &mut cap as *mut z_cap_t,
};
pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Result<Self, zion::ZError> {
let proc_cap = unsafe { crate::init::SELF_PROC_CAP };
let cap = syscall::thread_create(proc_cap)?;
syscall::syscall(zion::kZionThreadCreate, &req).expect("Failed to create thread.");
syscall::thread_start(
cap,
internal_entry_point as u64,
entry as *const ThreadEntry as u64,
arg1 as u64,
)?;
syscall::syscall(
zion::kZionThreadStart,
&zion::ZThreadStartReq {
thread_cap: cap,
entry: entry_point as u64,
arg1: entry as *const ThreadEntry as u64,
arg2: arg1 as u64,
},
)
.expect("Failed to start thread.");
Self { cap, _entry: entry }
Ok(Self { cap, _entry: entry })
}
pub fn join(&self) -> Result<(), zion::ZError> {
syscall::syscall(
zion::kZionThreadWait,
&zion::ZThreadWaitReq {
thread_cap: self.cap,
},
)
syscall::thread_wait(self.cap)
}
}