redox-rt: Use Option rather than MaybeUninit + bool

This commit is contained in:
bjorn3
2026-02-07 18:37:37 +01:00
parent 02d2597292
commit 24aaf8fac9
4 changed files with 16 additions and 34 deletions
+6 -15
View File
@@ -4,10 +4,7 @@
#![feature(core_intrinsics, int_roundings, slice_ptr_get, sync_unsafe_cell)]
#![forbid(unreachable_patterns)]
use core::{
cell::UnsafeCell,
mem::{MaybeUninit, size_of},
};
use core::cell::UnsafeCell;
use generic_rt::{ExpectTlsFree, GenericTcb};
use syscall::Sigcontrol;
@@ -208,12 +205,10 @@ pub unsafe fn initialize(
pid: metadata.pid,
#[cfg(feature = "proc")]
proc_fd: MaybeUninit::new(proc_fd),
proc_fd: Some(proc_fd),
#[cfg(not(feature = "proc"))]
proc_fd: MaybeUninit::uninit(),
has_proc_fd: cfg!(feature = "proc"),
proc_fd: None,
})
};
@@ -232,11 +227,9 @@ pub unsafe fn initialize(
}
}
#[repr(C)] // TODO: is repr(C) required?
pub(crate) struct StaticProcInfo {
pid: u32,
proc_fd: MaybeUninit<FdGuardUpper>,
has_proc_fd: bool,
proc_fd: Option<FdGuardUpper>,
}
pub struct DynamicProcInfo {
pub pgid: u32,
@@ -267,8 +260,7 @@ pub(crate) fn static_proc_info() -> &'static StaticProcInfo {
#[inline]
pub fn current_proc_fd() -> &'static FdGuardUpper {
let info = static_proc_info();
assert!(info.has_proc_fd);
unsafe { info.proc_fd.assume_init_ref() }
info.proc_fd.as_ref().unwrap()
}
#[inline]
pub fn current_namespace_fd() -> usize {
@@ -310,8 +302,7 @@ unsafe fn child_hook_common(args: ChildHookCommonArgs) {
.get()
.replace(StaticProcInfo {
pid: metadata.pid,
has_proc_fd: new_proc_fd.is_some(),
proc_fd: new_proc_fd.map_or_else(MaybeUninit::uninit, MaybeUninit::new),
proc_fd: new_proc_fd,
})
.proc_fd
};
+8 -16
View File
@@ -1,9 +1,4 @@
use core::{
cell::SyncUnsafeCell,
cmp,
fmt::Debug,
mem::{MaybeUninit, size_of},
};
use core::{cell::SyncUnsafeCell, cmp, fmt::Debug};
use crate::{
DYNAMIC_PROC_INFO, RtTcb, StaticProcInfo,
@@ -1048,11 +1043,10 @@ pub fn new_child_process(args: &ForkArgs<'_>) -> Result<NewChildProc> {
match *args {
ForkArgs::Managed => {
let proc_info = crate::static_proc_info();
assert!(
proc_info.has_proc_fd,
"cannot use ForkArgs::Managed without an existing proc info"
);
let this_proc_fd = unsafe { proc_info.proc_fd.assume_init_ref() };
let this_proc_fd = proc_info
.proc_fd
.as_ref()
.expect("cannot use ForkArgs::Managed without an existing proc info");
let child_proc_fd = this_proc_fd.dup(b"fork")?.to_upper()?;
let only_thread_fd = child_proc_fd.dup(b"thread-0")?.to_upper()?;
let meta = read_proc_meta(&child_proc_fd)?;
@@ -1118,8 +1112,7 @@ pub unsafe fn make_init(proc_cap: usize) -> (&'static FdGuardUpper, &'static FdG
unsafe {
STATIC_PROC_INFO.get().write(crate::StaticProcInfo {
pid: 1,
proc_fd: MaybeUninit::new(proc_fd),
has_proc_fd: true,
proc_fd: Some(proc_fd),
})
};
*DYNAMIC_PROC_INFO.lock() = crate::DynamicProcInfo {
@@ -1133,13 +1126,12 @@ pub unsafe fn make_init(proc_cap: usize) -> (&'static FdGuardUpper, &'static FdG
ns_fd: None,
};
(
unsafe { (*STATIC_PROC_INFO.get()).proc_fd.assume_init_ref() },
unsafe { (*STATIC_PROC_INFO.get()).proc_fd.as_ref().unwrap() },
managed_thr_fd,
)
}
pub(crate) static STATIC_PROC_INFO: SyncUnsafeCell<StaticProcInfo> =
SyncUnsafeCell::new(StaticProcInfo {
pid: 0,
proc_fd: MaybeUninit::zeroed(),
has_proc_fd: false,
proc_fd: None,
});
+1 -1
View File
@@ -907,7 +907,7 @@ fn try_claim_single(sig_idx: u32, thread_control: Option<&Sigcontrol>) -> Option
let mut buf = [0_u8; size_of::<RtSigInfo>()];
buf[..4].copy_from_slice(&(sig_idx - 32).to_ne_bytes());
proc_call(
static_proc_info().proc_fd.assume_init_ref().as_raw_fd(),
static_proc_info().proc_fd.as_ref().unwrap().as_raw_fd(),
&mut buf,
CallFlags::empty(),
&[ProcCall::Sigdeq as u64],
+1 -2
View File
@@ -7,8 +7,7 @@ use crate::{RtTcb, arch::*, proc::*, signal::tmp_disable_signals, static_proc_in
/// Spawns a new context sharing the same address space as the current one (i.e. a new thread).
pub unsafe fn rlct_clone_impl(stack: *mut usize, tcb: &RtTcb) -> Result<usize> {
let proc_info = static_proc_info();
assert!(proc_info.has_proc_fd);
let cur_proc_fd = unsafe { proc_info.proc_fd.assume_init_ref() };
let cur_proc_fd = proc_info.proc_fd.as_ref().unwrap();
let cur_thr_fd = RtTcb::current().thread_fd();
let new_thr_fd = cur_proc_fd.dup(b"new-thread")?.to_upper().unwrap();