@@ -4,7 +4,7 @@ use crate::{Runtime, VmError};
4
4
5
5
use super :: yield_;
6
6
7
- /// Peeks the operand stack for the thread ID to join.
7
+ /// Pop the operand stack for the thread ID to join.
8
8
/// If the thread to join is in zombie state, then the current thread will be set to ready and the result
9
9
/// of the zombie thread will be pushed onto the current thread's operand stack. The zombie thread is deallocated.
10
10
/// If the thread to join is not found, then panic.
@@ -25,14 +25,15 @@ pub fn join(mut rt: Runtime) -> Result<Runtime> {
25
25
let tid: i64 = rt
26
26
. current_thread
27
27
. operand_stack
28
- . last ( )
28
+ . pop ( )
29
29
. ok_or ( VmError :: OperandStackUnderflow ) ?
30
30
. clone ( )
31
31
. try_into ( ) ?;
32
32
33
33
let Some ( mut zombie_thread) = rt. zombie_threads . remove ( & tid) else {
34
34
// If the thread to join is not found, we need to yield control and try again
35
35
rt. current_thread . pc -= 1 ; // Decrement the program counter to re-execute the join instruction
36
+ rt. current_thread . operand_stack . push ( tid. into ( ) ) ; // Add the pid back to the operand stack
36
37
let rt = yield_ ( rt) ?;
37
38
return Ok ( rt) ;
38
39
} ;
@@ -69,6 +70,13 @@ mod tests {
69
70
// Add this point, both threads are in the ready state, so join should yield the current thread
70
71
assert_eq ! ( rt. current_thread. thread_id, MAIN_THREAD_ID + 1 ) ;
71
72
73
+ // Add the parent thread ID to the operand stack of the child
74
+ rt = yield_ ( rt) ?;
75
+ assert_eq ! ( rt. current_thread. thread_id, MAIN_THREAD_ID ) ;
76
+
77
+ // PID should remain on the operand stack
78
+ assert_eq ! ( rt. current_thread. operand_stack. len( ) , 1 ) ;
79
+
72
80
Ok ( ( ) )
73
81
}
74
82
@@ -92,6 +100,9 @@ mod tests {
92
100
Value :: Int ( 0 ) // SPAWN adds 0 to the operand stack for the new thread
93
101
) ;
94
102
103
+ // The PID of child should be popped off the operand stack
104
+ assert ! ( rt. current_thread. operand_stack. is_empty( ) ) ;
105
+
95
106
Ok ( ( ) )
96
107
}
97
108
}
0 commit comments