asterinas/kernel/src/process/posix_thread/builder.rs

164 lines
4.5 KiB
Rust
Raw Normal View History

2024-01-03 03:22:36 +00:00
// SPDX-License-Identifier: MPL-2.0
#![expect(dead_code)]
2025-03-17 05:55:53 +00:00
use ostd::{
cpu::{context::UserContext, CpuSet},
2025-03-17 05:55:53 +00:00
sync::RwArc,
task::Task,
};
2022-12-20 06:12:22 +00:00
2024-12-01 01:19:03 +00:00
use super::{thread_table, PosixThread, ThreadLocal};
2022-12-20 06:12:22 +00:00
use crate::{
2024-12-01 03:41:23 +00:00
fs::{file_table::FileTable, thread_info::ThreadFsInfo},
2022-12-20 06:12:22 +00:00
prelude::*,
process::{
posix_thread::name::ThreadName,
signal::{sig_mask::AtomicSigMask, sig_queues::SigQueues},
2023-11-29 11:42:53 +00:00
Credentials, Process,
2022-12-20 06:12:22 +00:00
},
2025-01-22 06:08:35 +00:00
sched::{Nice, SchedPolicy},
2024-08-17 08:06:35 +00:00
thread::{task, Thread, Tid},
2024-05-31 10:11:52 +00:00
time::{clocks::ProfClock, TimerManager},
2022-12-20 06:12:22 +00:00
};
/// The builder to build a posix thread
pub struct PosixThreadBuilder {
// The essential part
tid: Tid,
2025-03-17 05:55:53 +00:00
user_ctx: Arc<UserContext>,
2022-12-20 06:12:22 +00:00
process: Weak<Process>,
2023-11-29 11:42:53 +00:00
credentials: Credentials,
2022-12-20 06:12:22 +00:00
// Optional part
thread_name: Option<ThreadName>,
set_child_tid: Vaddr,
clear_child_tid: Vaddr,
2024-12-26 13:35:56 +00:00
file_table: Option<RwArc<FileTable>>,
2024-12-01 03:41:23 +00:00
fs: Option<Arc<ThreadFsInfo>>,
sig_mask: AtomicSigMask,
2022-12-20 06:12:22 +00:00
sig_queues: SigQueues,
2025-01-22 06:08:35 +00:00
sched_policy: SchedPolicy,
2022-12-20 06:12:22 +00:00
}
impl PosixThreadBuilder {
2025-03-17 05:55:53 +00:00
pub fn new(tid: Tid, user_ctx: Arc<UserContext>, credentials: Credentials) -> Self {
2022-12-20 06:12:22 +00:00
Self {
tid,
2025-03-17 05:55:53 +00:00
user_ctx,
2022-12-20 06:12:22 +00:00
process: Weak::new(),
2023-11-29 11:42:53 +00:00
credentials,
2022-12-20 06:12:22 +00:00
thread_name: None,
set_child_tid: 0,
clear_child_tid: 0,
2024-12-01 03:41:23 +00:00
file_table: None,
fs: None,
sig_mask: AtomicSigMask::new_empty(),
2022-12-20 06:12:22 +00:00
sig_queues: SigQueues::new(),
2025-01-22 06:08:35 +00:00
sched_policy: SchedPolicy::Fair(Nice::default()),
2022-12-20 06:12:22 +00:00
}
}
pub fn process(mut self, process: Weak<Process>) -> Self {
self.process = process;
self
}
pub fn thread_name(mut self, thread_name: Option<ThreadName>) -> Self {
self.thread_name = thread_name;
self
}
pub fn set_child_tid(mut self, set_child_tid: Vaddr) -> Self {
self.set_child_tid = set_child_tid;
self
}
pub fn clear_child_tid(mut self, clear_child_tid: Vaddr) -> Self {
self.clear_child_tid = clear_child_tid;
self
}
2024-12-26 13:35:56 +00:00
pub fn file_table(mut self, file_table: RwArc<FileTable>) -> Self {
2024-12-01 03:41:23 +00:00
self.file_table = Some(file_table);
self
}
pub fn fs(mut self, fs: Arc<ThreadFsInfo>) -> Self {
self.fs = Some(fs);
self
}
pub fn sig_mask(mut self, sig_mask: AtomicSigMask) -> Self {
2022-12-20 06:12:22 +00:00
self.sig_mask = sig_mask;
self
}
2025-01-22 06:08:35 +00:00
pub fn sched_policy(mut self, sched_policy: SchedPolicy) -> Self {
self.sched_policy = sched_policy;
2024-08-17 08:06:35 +00:00
self
}
pub fn build(self) -> Arc<Task> {
2022-12-20 06:12:22 +00:00
let Self {
tid,
2025-03-17 05:55:53 +00:00
user_ctx,
2022-12-20 06:12:22 +00:00
process,
2023-11-29 11:42:53 +00:00
credentials,
2022-12-20 06:12:22 +00:00
thread_name,
set_child_tid,
clear_child_tid,
2024-12-01 03:41:23 +00:00
file_table,
fs,
2022-12-20 06:12:22 +00:00
sig_mask,
sig_queues,
2025-01-22 06:08:35 +00:00
sched_policy,
2022-12-20 06:12:22 +00:00
} = self;
2023-12-12 07:00:40 +00:00
2024-12-26 13:35:56 +00:00
let file_table = file_table.unwrap_or_else(|| RwArc::new(FileTable::new_with_stdio()));
2024-12-01 03:41:23 +00:00
let fs = fs.unwrap_or_else(|| Arc::new(ThreadFsInfo::default()));
Arc::new_cyclic(|weak_task| {
let root_vmar = process
.upgrade()
.map(|process| process.lock_root_vmar().unwrap().dup().unwrap());
let posix_thread = {
let prof_clock = ProfClock::new();
let virtual_timer_manager = TimerManager::new(prof_clock.user_clock().clone());
let prof_timer_manager = TimerManager::new(prof_clock.clone());
PosixThread {
process,
tid,
name: Mutex::new(thread_name),
credentials,
2024-12-26 13:35:56 +00:00
file_table: file_table.clone_ro(),
2024-12-01 03:41:23 +00:00
fs,
sig_mask,
sig_queues,
signalled_waker: SpinLock::new(None),
prof_clock,
virtual_timer_manager,
prof_timer_manager,
}
2022-12-20 06:12:22 +00:00
};
let cpu_affinity = CpuSet::new_full();
let thread = Arc::new(Thread::new(
weak_task.clone(),
posix_thread,
cpu_affinity,
2025-01-22 06:08:35 +00:00
sched_policy,
));
let thread_local =
ThreadLocal::new(set_child_tid, clear_child_tid, root_vmar, file_table);
2024-12-01 01:19:03 +00:00
thread_table::add_thread(tid, thread.clone());
2025-03-17 05:55:53 +00:00
task::create_new_user_task(user_ctx, thread, thread_local)
})
2022-12-20 06:12:22 +00:00
}
}