Skip to content

Commit e886be5

Browse files
committed
refactor: thread trait
1 parent 45f5fa4 commit e886be5

File tree

3 files changed

+28
-73
lines changed

3 files changed

+28
-73
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ extern crate alloc;
88
mod process;
99
mod process_group;
1010
mod session;
11-
mod thread;
1211

1312
/// A process ID, also used as session ID, process group ID, and thread ID.
1413
pub type Pid = u32;
1514

16-
pub use process::{Process, init_proc};
15+
pub use process::{Process, Thread, init_proc};
1716
pub use process_group::ProcessGroup;
1817
pub use session::Session;
19-
pub use thread::Thread;

src/process.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ use kspin::SpinNoIrq;
1111
use lazyinit::LazyInit;
1212
use weak_map::{StrongMap, WeakMap};
1313

14-
use crate::{Pid, ProcessGroup, Session, Thread};
14+
use crate::{Pid, ProcessGroup, Session};
15+
16+
/// Represents a thread belonging to a process.
17+
pub trait Thread: Send + Sync {
18+
/// The thread ID.
19+
fn tid(&self) -> Pid;
20+
}
1521

1622
pub(crate) struct ThreadGroup {
17-
pub(crate) threads: WeakMap<Pid, Weak<Thread>>,
23+
pub(crate) threads: WeakMap<Pid, Weak<dyn Thread>>,
1824
pub(crate) exit_code: i32,
1925
pub(crate) group_exited: bool,
2026
}
@@ -35,7 +41,7 @@ pub struct Process {
3541
is_zombie: AtomicBool,
3642
pub(crate) tg: SpinNoIrq<ThreadGroup>,
3743

38-
// TODO: child subreaper
44+
// TODO: child subreaper9
3945
children: SpinNoIrq<StrongMap<Pid, Arc<Process>>>,
4046
parent: SpinNoIrq<Weak<Process>>,
4147

@@ -156,15 +162,26 @@ impl Process {
156162

157163
/// Threads
158164
impl Process {
159-
/// Creates a new [`Thread`] in this [`Process`].
160-
pub fn new_thread(self: &Arc<Self>, tid: Pid) -> Arc<Thread> {
161-
let thread = Arc::new(Thread::new(tid, self.clone()));
162-
self.tg.lock().threads.insert(tid, &thread);
163-
thread
165+
/// Adds a [`Thread`] to this [`Process`] with the given thread ID.
166+
pub fn add_thread(self: &Arc<Self>, thread: &Arc<dyn Thread>) {
167+
self.tg.lock().threads.insert(thread.tid(), thread);
168+
}
169+
170+
/// Removes a [`Thread`] from this [`Process`] and sets the exit code if the
171+
/// group has not exited.
172+
///
173+
/// Returns `true` if this was the last thread in the process.
174+
pub fn exit_thread(self: &Arc<Self>, thread: &dyn Thread, exit_code: i32) -> bool {
175+
let mut tg = self.tg.lock();
176+
if !tg.group_exited {
177+
tg.exit_code = exit_code;
178+
}
179+
tg.threads.remove(&thread.tid());
180+
tg.threads.is_empty()
164181
}
165182

166-
/// The [`Thread`]s in this [`Process`].
167-
pub fn threads(&self) -> Vec<Arc<Thread>> {
183+
/// Get all [`Thread`]s in this [`Process`].
184+
pub fn threads(&self) -> Vec<Arc<dyn Thread>> {
168185
self.tg.lock().threads.values().collect()
169186
}
170187

src/thread.rs

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)