Skip to content

Commit 45f5fa4

Browse files
committed
feat: thread is exited
1 parent f9470cf commit 45f5fa4

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/process.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ impl Process {
158158
impl 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
}

src/thread.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
use alloc::sync::Arc;
2-
use core::fmt;
2+
use core::{
3+
fmt,
4+
sync::atomic::{AtomicBool, Ordering},
5+
};
36

47
use crate::{Pid, Process};
58

69
/// A thread.
710
pub 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

1216
impl 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

3656
impl fmt::Debug for Thread {

0 commit comments

Comments
 (0)