Move Tid from Thread to PosixThread

This commit is contained in:
Jianfeng Jiang
2024-09-12 04:48:33 +00:00
committed by Tate, Hongliang Tian
parent ceb6e2b242
commit 81b0f265b5
21 changed files with 90 additions and 86 deletions

View File

@ -18,7 +18,8 @@ use crate::{
cpu::LinuxAbi,
fs::{file_table::FileTable, fs_resolver::FsResolver, utils::FileCreationMask},
prelude::*,
thread::{allocate_tid, thread_table, Thread, Tid},
process::posix_thread::allocate_posix_tid,
thread::{thread_table, Thread, Tid},
};
bitflags! {
@ -180,7 +181,7 @@ fn clone_child_thread(
// Inherit sigmask from current thread
let sig_mask = posix_thread.sig_mask().load(Ordering::Relaxed).into();
let child_tid = allocate_tid();
let child_tid = allocate_posix_tid();
let child_thread = {
let credentials = {
let credentials = ctx.posix_thread.credentials();
@ -262,7 +263,7 @@ fn clone_child_process(
// inherit parent's nice value
let child_nice = process.nice().load(Ordering::Relaxed);
let child_tid = allocate_tid();
let child_tid = allocate_posix_tid();
let child = {
let child_elf_path = process.executable_path();
@ -295,7 +296,7 @@ fn clone_child_process(
};
// Deals with clone flags
let child_thread = thread_table::get_thread(child_tid).unwrap();
let child_thread = thread_table::get_posix_thread(child_tid).unwrap();
let child_posix_thread = child_thread.as_posix_thread().unwrap();
clone_parent_settid(child_tid, clone_args.parent_tidptr, clone_flags)?;
clone_child_cleartid(child_posix_thread, clone_args.child_tidptr, clone_flags)?;

View File

@ -4,7 +4,7 @@ use super::{process_table, Pid, Process, TermStatus};
use crate::{
prelude::*,
process::{
posix_thread::do_exit,
posix_thread::{do_exit, PosixThreadExt},
signal::{constants::SIGCHLD, signals::kernel::KernelSignal},
},
};
@ -20,7 +20,7 @@ pub fn do_exit_group(term_status: TermStatus) {
// Exit all threads
let threads = current.threads().lock().clone();
for thread in threads {
if let Err(e) = do_exit(thread, term_status) {
if let Err(e) = do_exit(&thread, thread.as_posix_thread().unwrap(), term_status) {
debug!("Ignore error when call exit: {:?}", e);
}
}

View File

@ -71,7 +71,7 @@ pub fn kill_group(pgid: Pgid, signal: Option<UserSignal>, ctx: &Context) -> Resu
/// If `signal` is `None`, this method will only check permission without sending
/// any signal.
pub fn tgkill(tid: Tid, tgid: Pid, signal: Option<UserSignal>, ctx: &Context) -> Result<()> {
let thread = thread_table::get_thread(tid)
let thread = thread_table::get_posix_thread(tid)
.ok_or_else(|| Error::with_message(Errno::ESRCH, "target thread does not exist"))?;
if thread.status().is_exited() {

View File

@ -95,6 +95,7 @@ impl PosixThreadBuilder {
let posix_thread = PosixThread {
process,
tid,
name: Mutex::new(thread_name),
set_child_tid: Mutex::new(set_child_tid),
clear_child_tid: Mutex::new(clear_child_tid),
@ -110,9 +111,9 @@ impl PosixThreadBuilder {
prof_timer_manager,
};
Thread::new(tid, task, posix_thread, status)
Thread::new(task, posix_thread, status)
});
thread_table::add_thread(thread.clone());
thread_table::add_posix_thread(tid, thread.clone());
thread
}
}

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use super::{futex::futex_wake, robust_list::wake_robust_futex, PosixThread, PosixThreadExt};
use super::{futex::futex_wake, robust_list::wake_robust_futex, PosixThread};
use crate::{
prelude::*,
process::{do_exit_group, TermStatus},
@ -12,15 +12,13 @@ use crate::{
/// # Panics
///
/// If the thread is not a POSIX thread, this method will panic.
pub fn do_exit(thread: Arc<Thread>, term_status: TermStatus) -> Result<()> {
pub fn do_exit(thread: &Thread, posix_thread: &PosixThread, term_status: TermStatus) -> Result<()> {
if thread.status().is_exited() {
return Ok(());
}
thread.exit();
let tid = thread.tid();
let posix_thread = thread.as_posix_thread().unwrap();
let tid = posix_thread.tid;
let mut clear_ctid = posix_thread.clear_child_tid().lock();
// If clear_ctid !=0 ,do a futex wake and write zero to the clear_ctid addr.
@ -38,7 +36,7 @@ pub fn do_exit(thread: Arc<Thread>, term_status: TermStatus) -> Result<()> {
if tid != posix_thread.process().pid() {
// We don't remove main thread.
// The main thread is removed when the process is reaped.
thread_table::remove_thread(tid);
thread_table::remove_posix_thread(tid);
}
if posix_thread.is_main_thread(tid) || posix_thread.is_last_thread() {

View File

@ -2,7 +2,7 @@
#![allow(dead_code)]
use core::sync::atomic::Ordering;
use core::sync::atomic::{AtomicU32, Ordering};
use aster_rights::{ReadOp, WriteOp};
use ostd::sync::Waker;
@ -42,6 +42,8 @@ pub use robust_list::RobustListHead;
pub struct PosixThread {
// Immutable part
process: Weak<Process>,
tid: Tid,
// Mutable part
name: Mutex<Option<ThreadName>>,
@ -87,6 +89,11 @@ impl PosixThread {
Weak::clone(&self.process)
}
/// Returns the thread id
pub fn tid(&self) -> Tid {
self.tid
}
pub fn thread_name(&self) -> &Mutex<Option<ThreadName>> {
&self.name
}
@ -292,3 +299,10 @@ impl PosixThread {
self.credentials.dup().restrict()
}
}
static POSIX_TID_ALLOCATOR: AtomicU32 = AtomicU32::new(0);
/// Allocates a new tid for the new posix thread
pub fn allocate_posix_tid() -> Tid {
POSIX_TID_ALLOCATOR.fetch_add(1, Ordering::SeqCst)
}

View File

@ -13,6 +13,14 @@ use crate::{
thread::{Thread, Tid},
};
pub trait PosixThreadExt {
/// Returns the thread id.
///
/// # Panics
///
/// If the thread is not posix thread, this method will panic.
fn tid(&self) -> Tid {
self.as_posix_thread().unwrap().tid()
}
fn as_posix_thread(&self) -> Option<&PosixThread>;
#[allow(clippy::too_many_arguments)]
fn new_posix_thread_from_executable(

View File

@ -4,7 +4,7 @@ use core::sync::atomic::Ordering;
use self::timer_manager::PosixTimerManager;
use super::{
posix_thread::PosixThreadExt,
posix_thread::{allocate_posix_tid, PosixThreadExt},
process_table,
process_vm::{Heap, InitStackReader, ProcessVm},
rlimit::ResourceLimits,
@ -21,7 +21,7 @@ use crate::{
fs::{file_table::FileTable, fs_resolver::FsResolver, utils::FileCreationMask},
prelude::*,
sched::nice::Nice,
thread::{allocate_tid, Thread},
thread::Thread,
time::clocks::ProfClock,
vm::vmar::Vmar,
};
@ -236,7 +236,7 @@ impl Process {
envp: Vec<CString>,
) -> Result<Arc<Self>> {
let process_builder = {
let pid = allocate_tid();
let pid = allocate_posix_tid();
let parent = Weak::new();
let credentials = Credentials::new_root();
@ -710,7 +710,7 @@ mod test {
fn new_process(parent: Option<Arc<Process>>) -> Arc<Process> {
crate::util::random::init();
crate::fs::rootfs::init_root_mount();
let pid = allocate_tid();
let pid = allocate_posix_tid();
let parent = if let Some(parent) = parent {
Arc::downgrade(&parent)
} else {

View File

@ -2,12 +2,12 @@
#![allow(dead_code)]
use super::{
process_filter::ProcessFilter,
signal::{constants::SIGCHLD, with_signal_blocked},
ExitCode, Pid, Process,
use super::{process_filter::ProcessFilter, signal::constants::SIGCHLD, ExitCode, Pid, Process};
use crate::{
prelude::*,
process::{posix_thread::PosixThreadExt, process_table, signal::with_signal_blocked},
thread::thread_table,
};
use crate::{prelude::*, process::process_table, thread::thread_table};
// The definition of WaitOptions is from Occlum
bitflags! {
@ -86,7 +86,7 @@ fn reap_zombie_child(process: &Process, pid: Pid) -> ExitCode {
let child_process = process.children().lock().remove(&pid).unwrap();
assert!(child_process.is_zombie());
for thread in &*child_process.threads().lock() {
thread_table::remove_thread(thread.tid());
thread_table::remove_posix_thread(thread.tid());
}
// Lock order: session table -> group table -> process table -> group of process