Move yunq to new thread spawn and remove old one.

This commit is contained in:
Drew Galbraith 2025-01-26 00:05:55 -08:00
parent 79e1ea2791
commit d777b8f4ab
7 changed files with 33 additions and 76 deletions

View file

@ -5,44 +5,6 @@ use crate::zion;
use alloc::boxed::Box;
use core::ffi::c_void;
pub type ThreadEntry = fn(*const c_void) -> ();
#[no_mangle]
extern "C" fn internal_entry_point(thread_ptr: *const Thread, arg1: *const c_void) -> ! {
let thread: &Thread = unsafe { thread_ptr.as_ref().expect("Failed to unwrap thread ref") };
(thread.entry)(arg1);
syscall::thread_exit()
}
// TODO: Add a Drop implementation that kills this thread and drops its capability.
pub struct Thread {
cap: Capability,
// This field only exists to ensure that the entry reference will outlive the thread object
// itself.
entry: ThreadEntry,
}
impl Thread {
pub fn spawn(entry: ThreadEntry, arg1: *const c_void) -> Result<Box<Self>, zion::ZError> {
let proc_cap = Capability::take_copy(unsafe { crate::init::SELF_PROC_CAP })?;
let cap = syscall::thread_create(&proc_cap)?;
let thread = Box::new(Self { cap, entry });
syscall::thread_start(
&thread.cap,
internal_entry_point as u64,
thread.as_ref() as *const Thread as u64,
arg1 as u64,
)?;
Ok(thread)
}
pub fn join(&self) -> Result<(), zion::ZError> {
syscall::thread_wait(&self.cap)
}
}
pub struct JoinHandle {
cap: Capability,
}

View file

@ -2,6 +2,8 @@ use crate::buffer::ByteBuffer;
use alloc::vec::Vec;
use mammoth::cap::Capability;
use mammoth::syscall;
use mammoth::thread;
use mammoth::thread::JoinHandle;
use mammoth::zion::z_cap_t;
use mammoth::zion::ZError;
@ -50,3 +52,10 @@ pub trait YunqServer {
cap_buffer: &mut Vec<z_cap_t>,
) -> Result<usize, ZError>;
}
pub fn spawn_server_thread<T>(mut server: T) -> JoinHandle
where
T: YunqServer + Send + 'static,
{
thread::spawn(move || server.server_loop())
}