mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-28 20:03:22 +00:00
Add ThreadExt
and clean up PosixThreadExt
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
a4a8807a20
commit
9233d1cdbb
@ -5,7 +5,7 @@ use ostd::{
|
||||
task::{Task, TaskOptions},
|
||||
};
|
||||
|
||||
use super::{oops, status::ThreadStatus, Thread};
|
||||
use super::{oops, status::ThreadStatus, Thread, ThreadExt};
|
||||
use crate::{prelude::*, sched::priority::Priority};
|
||||
|
||||
/// The inner data of a kernel thread.
|
||||
@ -77,7 +77,7 @@ impl ThreadOptions {
|
||||
/// Builds a new kernel thread and runs it immediately.
|
||||
pub fn spawn(self) -> Arc<Thread> {
|
||||
let task = self.build();
|
||||
let thread = Thread::borrow_from_task(&task).clone();
|
||||
let thread = task.as_thread().unwrap().clone();
|
||||
thread.run();
|
||||
thread
|
||||
}
|
||||
|
@ -65,10 +65,7 @@ impl Thread {
|
||||
/// This function returns `None` if the current task is not associated with
|
||||
/// a thread, or if called within the bootstrap context.
|
||||
pub fn current() -> Option<Arc<Self>> {
|
||||
Task::current()?
|
||||
.data()
|
||||
.downcast_ref::<Arc<Thread>>()
|
||||
.cloned()
|
||||
Task::current()?.as_thread().cloned()
|
||||
}
|
||||
|
||||
/// Returns the task associated with this thread.
|
||||
@ -76,15 +73,6 @@ impl Thread {
|
||||
self.task.upgrade().unwrap()
|
||||
}
|
||||
|
||||
/// Gets the Thread from task's data.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This method panics if the task is not a thread.
|
||||
pub fn borrow_from_task(task: &Task) -> &Arc<Self> {
|
||||
task.data().downcast_ref::<Arc<Thread>>().unwrap()
|
||||
}
|
||||
|
||||
/// Runs this thread at once.
|
||||
pub fn run(&self) {
|
||||
self.status.store(ThreadStatus::Running, Ordering::Release);
|
||||
@ -173,3 +161,15 @@ impl Thread {
|
||||
&*self.data
|
||||
}
|
||||
}
|
||||
|
||||
/// An extension trait for [`Thread`]-like types.
|
||||
pub trait ThreadExt {
|
||||
/// Returns the associated [`Thread`].
|
||||
fn as_thread(&self) -> Option<&Arc<Thread>>;
|
||||
}
|
||||
|
||||
impl ThreadExt for Task {
|
||||
fn as_thread(&self) -> Option<&Arc<Thread>> {
|
||||
self.data().downcast_ref::<Arc<Thread>>()
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,7 @@ use super::worker_pool::WorkerPool;
|
||||
use crate::{
|
||||
prelude::*,
|
||||
sched::priority::{Priority, PriorityRange},
|
||||
thread::kernel_thread::ThreadOptions,
|
||||
Thread,
|
||||
thread::{kernel_thread::ThreadOptions, ThreadExt},
|
||||
};
|
||||
|
||||
/// A worker thread. A `Worker` will attempt to retrieve unfinished
|
||||
@ -72,7 +71,7 @@ impl Worker {
|
||||
}
|
||||
|
||||
pub(super) fn run(&self) {
|
||||
let thread = Thread::borrow_from_task(&self.bound_task);
|
||||
let thread = self.bound_task.as_thread().unwrap();
|
||||
thread.run();
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,7 @@ use super::{simple_scheduler::SimpleScheduler, worker::Worker, WorkItem, WorkPri
|
||||
use crate::{
|
||||
prelude::*,
|
||||
sched::priority::{Priority, PriorityRange},
|
||||
thread::kernel_thread::ThreadOptions,
|
||||
Thread,
|
||||
thread::{kernel_thread::ThreadOptions, ThreadExt},
|
||||
};
|
||||
|
||||
/// A pool of workers.
|
||||
@ -83,7 +82,7 @@ impl LocalWorkerPool {
|
||||
fn add_worker(&self) {
|
||||
let worker = Worker::new(self.parent.clone(), self.cpu_id);
|
||||
self.workers.disable_irq().lock().push_back(worker.clone());
|
||||
Thread::borrow_from_task(worker.bound_task()).run();
|
||||
worker.bound_task().as_thread().unwrap().run();
|
||||
}
|
||||
|
||||
fn remove_worker(&self) {
|
||||
@ -258,7 +257,7 @@ impl Monitor {
|
||||
}
|
||||
|
||||
pub fn run(&self) {
|
||||
Thread::borrow_from_task(&self.bound_task).run()
|
||||
self.bound_task.as_thread().unwrap().run()
|
||||
}
|
||||
|
||||
fn run_monitor_loop(self: &Arc<Self>) {
|
||||
|
Reference in New Issue
Block a user