Use new-thread proc API

This commit is contained in:
4lDO2
2024-07-09 16:13:57 +02:00
parent bf26de70e2
commit c286dd6d33
9 changed files with 121 additions and 79 deletions
+22 -8
View File
@@ -2,7 +2,8 @@
use core::{
cell::{Cell, UnsafeCell},
ptr::NonNull,
mem::MaybeUninit,
ptr::{addr_of, NonNull},
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};
@@ -67,13 +68,13 @@ pub struct Pthread {
pub(crate) stack_base: *mut c_void,
pub(crate) stack_size: usize,
pub(crate) os_tid: UnsafeCell<OsTid>,
pub os_tid: UnsafeCell<OsTid>,
}
#[derive(Clone, Copy, Debug, Default, Ord, Eq, PartialOrd, PartialEq)]
pub struct OsTid {
#[cfg(target_os = "redox")]
pub context_id: usize,
pub thread_fd: usize,
#[cfg(target_os = "linux")]
pub thread_id: usize,
}
@@ -147,6 +148,9 @@ pub(crate) unsafe fn create(
let synchronization_mutex = Mutex::locked(current_sigmask);
let synchronization_mutex = &synchronization_mutex;
let tid_mutex = Mutex::<MaybeUninit<OsTid>>::new(MaybeUninit::uninit());
let mut tid_guard = tid_mutex.lock();
let stack_size = attrs.stacksize.next_multiple_of(Sys::getpagesize());
let stack_base = if attrs.stack != 0 {
@@ -208,7 +212,7 @@ pub(crate) unsafe fn create(
}
push(0);
push(synchronization_mutex as *const _ as usize);
push(0);
push(addr_of!(tid_mutex) as usize);
push(new_tcb as *mut _ as usize);
push(arg as usize);
@@ -222,6 +226,8 @@ pub(crate) unsafe fn create(
};
core::mem::forget(stack_raii);
tid_guard.write(os_tid);
drop(tid_guard);
let _ = synchronization_mutex.lock();
OS_TID_TO_PTHREAD
@@ -235,10 +241,18 @@ unsafe extern "C" fn new_thread_shim(
entry_point: unsafe extern "C" fn(*mut c_void) -> *mut c_void,
arg: *mut c_void,
tcb: *mut Tcb,
_pthread: *mut Pthread,
mutex: *const Mutex<u64>,
mutex1: *const Mutex<MaybeUninit<OsTid>>,
mutex2: *const Mutex<u64>,
) -> ! {
let procmask = (*mutex).as_ptr().read();
let procmask = (&*mutex2).as_ptr().read();
let tid = (*(&*mutex1).lock()).assume_init();
#[cfg(target_os = "redox")]
(*tcb)
.os_specific
.thr_fd
.get()
.write(Some(redox_rt::proc::FdGuard::new(tid.thread_fd)));
if let Some(tcb) = tcb.as_mut() {
tcb.copy_masters().unwrap();
@@ -247,7 +261,7 @@ unsafe extern "C" fn new_thread_shim(
(*tcb).pthread.os_tid.get().write(Sys::current_os_tid());
(&*mutex).manual_unlock();
(&*mutex2).manual_unlock();
#[cfg(target_os = "redox")]
{