From 43b0f235ca43a95c91b42f2781f482809f1babe4 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 30 Sep 2025 12:49:22 -0600 Subject: [PATCH] Implement pthread_getattr_np --- redox-rt/src/arch/aarch64.rs | 4 ++-- redox-rt/src/arch/i686.rs | 4 ++-- redox-rt/src/arch/riscv64.rs | 4 ++-- redox-rt/src/arch/x86_64.rs | 4 ++-- src/header/pthread/attr.rs | 25 ++++++++++++++++++++++++- src/pthread/mod.rs | 24 +++++++++++++++++------- 6 files changed, 49 insertions(+), 16 deletions(-) diff --git a/redox-rt/src/arch/aarch64.rs b/redox-rt/src/arch/aarch64.rs index de30ce4732..ad723b11bb 100644 --- a/redox-rt/src/arch/aarch64.rs +++ b/redox-rt/src/arch/aarch64.rs @@ -10,8 +10,8 @@ use crate::{ }; // Setup a stack starting from the very end of the address space, and then growing downwards. -pub(crate) const STACK_TOP: usize = 1 << 47; -pub(crate) const STACK_SIZE: usize = 1024 * 1024; +pub const STACK_TOP: usize = 1 << 47; +pub const STACK_SIZE: usize = 1024 * 1024; #[derive(Debug, Default)] #[repr(C)] diff --git a/redox-rt/src/arch/i686.rs b/redox-rt/src/arch/i686.rs index b089a607bb..c026f7b867 100644 --- a/redox-rt/src/arch/i686.rs +++ b/redox-rt/src/arch/i686.rs @@ -10,8 +10,8 @@ use crate::{ }; // Setup a stack starting from the very end of the address space, and then growing downwards. -pub(crate) const STACK_TOP: usize = 1 << 31; -pub(crate) const STACK_SIZE: usize = 1024 * 1024; +pub const STACK_TOP: usize = 1 << 31; +pub const STACK_SIZE: usize = 1024 * 1024; #[derive(Debug, Default)] #[repr(C)] diff --git a/redox-rt/src/arch/riscv64.rs b/redox-rt/src/arch/riscv64.rs index 22b8c20008..520272e59f 100644 --- a/redox-rt/src/arch/riscv64.rs +++ b/redox-rt/src/arch/riscv64.rs @@ -10,8 +10,8 @@ use core::{mem::offset_of, ptr::NonNull, sync::atomic::Ordering}; use syscall::{data::*, error::*}; // Setup a stack starting from the very end of the address space, and then growing downwards. -pub(crate) const STACK_TOP: usize = 1 << 47; -pub(crate) const STACK_SIZE: usize = 1024 * 1024; +pub const STACK_TOP: usize = 1 << 47; +pub const STACK_SIZE: usize = 1024 * 1024; #[derive(Debug, Default)] #[repr(C)] diff --git a/redox-rt/src/arch/x86_64.rs b/redox-rt/src/arch/x86_64.rs index 7de6ae0756..db1c28f8ed 100644 --- a/redox-rt/src/arch/x86_64.rs +++ b/redox-rt/src/arch/x86_64.rs @@ -18,8 +18,8 @@ use crate::{ }; // Setup a stack starting from the very end of the address space, and then growing downwards. -pub(crate) const STACK_TOP: usize = 1 << 47; -pub(crate) const STACK_SIZE: usize = 1024 * 1024; +pub const STACK_TOP: usize = 1 << 47; +pub const STACK_SIZE: usize = 1024 * 1024; #[derive(Debug, Default)] #[repr(C)] diff --git a/src/header/pthread/attr.rs b/src/header/pthread/attr.rs index 7d8b6a1694..5abe05e341 100644 --- a/src/header/pthread/attr.rs +++ b/src/header/pthread/attr.rs @@ -1,6 +1,11 @@ +use core::{ptr, sync::atomic::Ordering}; + use super::*; -use crate::header::bits_pthread::pthread_attr_t; +use crate::{ + header::bits_pthread::{pthread_attr_t, pthread_t}, + pthread::{Pthread, PthreadFlags}, +}; impl Default for RlctAttr { fn default() -> Self { @@ -185,3 +190,21 @@ pub unsafe extern "C" fn pthread_attr_setstacksize( (*attr.cast::()).stacksize = stacksize; 0 } + +#[no_mangle] +pub unsafe extern "C" fn pthread_getattr_np( + thread_ptr: pthread_t, + attr_ptr: *mut pthread_attr_t, +) -> c_int { + let thread = &*thread_ptr.cast::(); + let attr_ptr = attr_ptr.cast::(); + ptr::write(attr_ptr, RlctAttr::default()); + let attr = &mut *attr_ptr; + if thread.flags.load(Ordering::Acquire) & PthreadFlags::DETACHED.bits() != 0 { + attr.detachstate = PTHREAD_CREATE_DETACHED as _; + } + attr.stack = thread.stack_base as usize; + attr.stacksize = thread.stack_size; + //TODO: more values? + 0 +} diff --git a/src/pthread/mod.rs b/src/pthread/mod.rs index bdf70af6ba..de847037b8 100644 --- a/src/pthread/mod.rs +++ b/src/pthread/mod.rs @@ -3,7 +3,7 @@ use core::{ cell::{Cell, UnsafeCell}, mem::{offset_of, MaybeUninit}, - ptr::{addr_of, NonNull}, + ptr::{self, addr_of, NonNull}, sync::atomic::{AtomicBool, AtomicUsize, Ordering}, }; @@ -24,9 +24,7 @@ use crate::sync::{waitval::Waitval, Mutex}; /// Called only by the main thread, as part of relibc_start. pub unsafe fn init() { - Tcb::current() - .expect_notls("no TCB present for main thread") - .pthread = Pthread { + let mut thread = Pthread { waitval: Waitval::new(), has_enabled_cancelation: AtomicBool::new(false), has_queued_cancelation: AtomicBool::new(false), @@ -34,12 +32,24 @@ pub unsafe fn init() { //index: FIRST_THREAD_IDX, - // TODO - stack_base: core::ptr::null_mut(), + // TODO: set these values on Linux as well + stack_base: ptr::null_mut(), stack_size: 0, os_tid: UnsafeCell::new(Sys::current_os_tid()), }; + + #[cfg(target_os = "redox")] + { + //TODO: what is the best way to get these values? + use redox_rt::arch::{STACK_SIZE, STACK_TOP}; + thread.stack_base = (STACK_TOP - STACK_SIZE) as *mut c_void; + thread.stack_size = STACK_SIZE; + } + + Tcb::current() + .expect_notls("no TCB present for main thread") + .pthread = thread; } //static NEXT_INDEX: AtomicU32 = AtomicU32::new(FIRST_THREAD_IDX + 1); @@ -52,7 +62,7 @@ pub unsafe fn terminate_from_main_thread() { } bitflags::bitflags! { - struct PthreadFlags: usize { + pub struct PthreadFlags: usize { const DETACHED = 1; } }