File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -158,10 +158,7 @@ impl Process {
158158impl Process {
159159 /// Creates a new [`Thread`] in this [`Process`].
160160 pub fn new_thread ( self : & Arc < Self > , tid : Pid ) -> Arc < Thread > {
161- let thread = Arc :: new ( Thread {
162- tid,
163- process : self . clone ( ) ,
164- } ) ;
161+ let thread = Arc :: new ( Thread :: new ( tid, self . clone ( ) ) ) ;
165162 self . tg . lock ( ) . threads . insert ( tid, & thread) ;
166163 thread
167164 }
Original file line number Diff line number Diff line change 11use alloc:: sync:: Arc ;
2- use core:: fmt;
2+ use core:: {
3+ fmt,
4+ sync:: atomic:: { AtomicBool , Ordering } ,
5+ } ;
36
47use crate :: { Pid , Process } ;
58
69/// A thread.
710pub struct Thread {
8- pub ( crate ) tid : Pid ,
9- pub ( crate ) process : Arc < Process > ,
11+ tid : Pid ,
12+ process : Arc < Process > ,
13+ exited : AtomicBool ,
1014}
1115
1216impl Thread {
17+ pub ( crate ) fn new ( tid : Pid , process : Arc < Process > ) -> Self {
18+ Self {
19+ tid,
20+ process,
21+ exited : AtomicBool :: new ( false ) ,
22+ }
23+ }
24+
1325 /// The [`Thread`] ID.
1426 pub fn tid ( & self ) -> Pid {
1527 self . tid
@@ -24,13 +36,21 @@ impl Thread {
2436 ///
2537 /// Returns `true` if the thread was the last one in the thread group.
2638 pub fn exit ( & self , exit_code : i32 ) -> bool {
39+ if self . exited . swap ( true , Ordering :: SeqCst ) {
40+ return false ; // Already exited
41+ }
2742 let mut tg = self . process . tg . lock ( ) ;
2843 if !tg. group_exited {
2944 tg. exit_code = exit_code;
3045 }
3146 tg. threads . remove ( & self . tid ) ;
3247 tg. threads . is_empty ( )
3348 }
49+
50+ /// Returns `true` if the thread has exited.
51+ pub fn is_exited ( & self ) -> bool {
52+ self . exited . load ( Ordering :: SeqCst )
53+ }
3454}
3555
3656impl fmt:: Debug for Thread {
You can’t perform that action at this time.
0 commit comments