@@ -11,10 +11,16 @@ use kspin::SpinNoIrq;
1111use lazyinit:: LazyInit ;
1212use 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
1622pub ( 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
158164impl 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
0 commit comments