Skip to content

Commit 3186dcf

Browse files
authored
Merge pull request #53 from ForAeons/vm-update-join
fix: update JOIN to consume PID on operand stack
2 parents 999b5e2 + 936db73 commit 3186dcf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

vm/ignite/src/micro_code/join.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{Runtime, VmError};
44

55
use super::yield_;
66

7-
/// Peeks the operand stack for the thread ID to join.
7+
/// Pop the operand stack for the thread ID to join.
88
/// If the thread to join is in zombie state, then the current thread will be set to ready and the result
99
/// of the zombie thread will be pushed onto the current thread's operand stack. The zombie thread is deallocated.
1010
/// If the thread to join is not found, then panic.
@@ -25,14 +25,15 @@ pub fn join(mut rt: Runtime) -> Result<Runtime> {
2525
let tid: i64 = rt
2626
.current_thread
2727
.operand_stack
28-
.last()
28+
.pop()
2929
.ok_or(VmError::OperandStackUnderflow)?
3030
.clone()
3131
.try_into()?;
3232

3333
let Some(mut zombie_thread) = rt.zombie_threads.remove(&tid) else {
3434
// If the thread to join is not found, we need to yield control and try again
3535
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
3637
let rt = yield_(rt)?;
3738
return Ok(rt);
3839
};
@@ -69,6 +70,13 @@ mod tests {
6970
// Add this point, both threads are in the ready state, so join should yield the current thread
7071
assert_eq!(rt.current_thread.thread_id, MAIN_THREAD_ID + 1);
7172

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+
7280
Ok(())
7381
}
7482

@@ -92,6 +100,9 @@ mod tests {
92100
Value::Int(0) // SPAWN adds 0 to the operand stack for the new thread
93101
);
94102

103+
// The PID of child should be popped off the operand stack
104+
assert!(rt.current_thread.operand_stack.is_empty());
105+
95106
Ok(())
96107
}
97108
}

0 commit comments

Comments
 (0)