mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-28 20:03:22 +00:00
Move Tid from Thread to PosixThread
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
ceb6e2b242
commit
81b0f265b5
@ -5,7 +5,7 @@ use ostd::{
|
||||
task::{Priority, TaskOptions},
|
||||
};
|
||||
|
||||
use super::{allocate_tid, status::ThreadStatus, thread_table, Thread};
|
||||
use super::{status::ThreadStatus, thread_table, Thread};
|
||||
use crate::prelude::*;
|
||||
|
||||
/// The inner data of a kernel thread
|
||||
@ -38,21 +38,21 @@ impl KernelThreadExt for Thread {
|
||||
let current_thread = current_thread!();
|
||||
// ensure the thread is exit
|
||||
current_thread.exit();
|
||||
thread_table::remove_kernel_thread(current_thread);
|
||||
};
|
||||
let tid = allocate_tid();
|
||||
let thread = Arc::new_cyclic(|thread_ref| {
|
||||
let weal_thread = thread_ref.clone();
|
||||
let weak_thread = thread_ref.clone();
|
||||
let task = TaskOptions::new(thread_fn)
|
||||
.data(weal_thread)
|
||||
.data(weak_thread)
|
||||
.priority(thread_options.priority)
|
||||
.cpu_affinity(thread_options.cpu_affinity)
|
||||
.build()
|
||||
.unwrap();
|
||||
let status = ThreadStatus::Init;
|
||||
let kernel_thread = KernelThread;
|
||||
Thread::new(tid, task, kernel_thread, status)
|
||||
Thread::new(task, kernel_thread, status)
|
||||
});
|
||||
thread_table::add_thread(thread.clone());
|
||||
thread_table::add_kernel_thread(thread.clone());
|
||||
thread
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
//! Posix thread implementation
|
||||
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
use core::sync::atomic::Ordering;
|
||||
|
||||
use ostd::task::Task;
|
||||
|
||||
@ -18,13 +18,9 @@ pub mod work_queue;
|
||||
|
||||
pub type Tid = u32;
|
||||
|
||||
static TID_ALLOCATOR: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
/// A thread is a wrapper on top of task.
|
||||
pub struct Thread {
|
||||
// immutable part
|
||||
/// Thread id
|
||||
tid: Tid,
|
||||
/// Low-level info
|
||||
task: Arc<Task>,
|
||||
/// Data: Posix thread info/Kernel thread Info
|
||||
@ -36,14 +32,8 @@ pub struct Thread {
|
||||
|
||||
impl Thread {
|
||||
/// Never call these function directly
|
||||
pub fn new(
|
||||
tid: Tid,
|
||||
task: Arc<Task>,
|
||||
data: impl Send + Sync + Any,
|
||||
status: ThreadStatus,
|
||||
) -> Self {
|
||||
pub fn new(task: Arc<Task>, data: impl Send + Sync + Any, status: ThreadStatus) -> Self {
|
||||
Thread {
|
||||
tid,
|
||||
task,
|
||||
data: Box::new(data),
|
||||
status: AtomicThreadStatus::new(status),
|
||||
@ -94,10 +84,6 @@ impl Thread {
|
||||
Task::yield_now()
|
||||
}
|
||||
|
||||
pub fn tid(&self) -> Tid {
|
||||
self.tid
|
||||
}
|
||||
|
||||
/// Returns the associated data.
|
||||
///
|
||||
/// The return type must be borrowed box, otherwise the `downcast_ref` will fail.
|
||||
@ -106,8 +92,3 @@ impl Thread {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocates a new tid for the new thread
|
||||
pub fn allocate_tid() -> Tid {
|
||||
TID_ALLOCATOR.fetch_add(1, Ordering::SeqCst)
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ pub fn create_new_user_task(user_space: Arc<UserSpace>, thread_ref: Weak<Thread>
|
||||
// in the child process.
|
||||
if is_userspace_vaddr(child_tid_ptr) {
|
||||
CurrentUserSpace::get()
|
||||
.write_val(child_tid_ptr, ¤t_thread.tid())
|
||||
.write_val(child_tid_ptr, ¤t_posix_thread.tid())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ pub fn create_new_user_task(user_space: Arc<UserSpace>, thread_ref: Weak<Thread>
|
||||
// If current is suspended, wait for a signal to wake up self
|
||||
while current_thread.status().is_stopped() {
|
||||
Thread::yield_now();
|
||||
debug!("{} is suspended.", current_thread.tid());
|
||||
debug!("{} is suspended.", current_posix_thread.tid());
|
||||
handle_pending_signal(user_ctx, ¤t_thread).unwrap();
|
||||
}
|
||||
if current_thread.status().is_exited() {
|
||||
|
@ -1,21 +1,30 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use keyable_arc::KeyableArc;
|
||||
|
||||
use super::{Thread, Tid};
|
||||
use crate::prelude::*;
|
||||
use crate::{prelude::*, process::posix_thread::PosixThreadExt};
|
||||
|
||||
lazy_static! {
|
||||
static ref THREAD_TABLE: SpinLock<BTreeMap<Tid, Arc<Thread>>> = SpinLock::new(BTreeMap::new());
|
||||
static POSIX_THREAD_TABLE: SpinLock<BTreeMap<Tid, Arc<Thread>>> = SpinLock::new(BTreeMap::new());
|
||||
static KERNEL_THREAD_TABLE: SpinLock<BTreeSet<KeyableArc<Thread>>> = SpinLock::new(BTreeSet::new());
|
||||
|
||||
pub fn add_posix_thread(tid: Tid, thread: Arc<Thread>) {
|
||||
debug_assert_eq!(tid, thread.tid());
|
||||
POSIX_THREAD_TABLE.lock().insert(tid, thread);
|
||||
}
|
||||
|
||||
pub fn add_thread(thread: Arc<Thread>) {
|
||||
let tid = thread.tid();
|
||||
THREAD_TABLE.lock().insert(tid, thread);
|
||||
pub fn remove_posix_thread(tid: Tid) {
|
||||
POSIX_THREAD_TABLE.lock().remove(&tid);
|
||||
}
|
||||
|
||||
pub fn remove_thread(tid: Tid) {
|
||||
THREAD_TABLE.lock().remove(&tid);
|
||||
pub fn get_posix_thread(tid: Tid) -> Option<Arc<Thread>> {
|
||||
POSIX_THREAD_TABLE.lock().get(&tid).cloned()
|
||||
}
|
||||
|
||||
pub fn get_thread(tid: Tid) -> Option<Arc<Thread>> {
|
||||
THREAD_TABLE.lock().get(&tid).cloned()
|
||||
pub(super) fn add_kernel_thread(thread: Arc<Thread>) {
|
||||
KERNEL_THREAD_TABLE.lock().insert(KeyableArc::from(thread));
|
||||
}
|
||||
|
||||
pub(super) fn remove_kernel_thread(thread: Arc<Thread>) {
|
||||
KERNEL_THREAD_TABLE.lock().remove(&KeyableArc::from(thread));
|
||||
}
|
||||
|
Reference in New Issue
Block a user