Skip to content

Commit

Permalink
Merge pull request #53 from ForAeons/vm-update-join
Browse files Browse the repository at this point in the history
fix: update JOIN to consume PID on operand stack
  • Loading branch information
wxiaoyun authored Apr 18, 2024
2 parents 999b5e2 + 936db73 commit 3186dcf
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions vm/ignite/src/micro_code/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{Runtime, VmError};

use super::yield_;

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

let Some(mut zombie_thread) = rt.zombie_threads.remove(&tid) else {
// If the thread to join is not found, we need to yield control and try again
rt.current_thread.pc -= 1; // Decrement the program counter to re-execute the join instruction
rt.current_thread.operand_stack.push(tid.into()); // Add the pid back to the operand stack
let rt = yield_(rt)?;
return Ok(rt);
};
Expand Down Expand Up @@ -69,6 +70,13 @@ mod tests {
// Add this point, both threads are in the ready state, so join should yield the current thread
assert_eq!(rt.current_thread.thread_id, MAIN_THREAD_ID + 1);

// Add the parent thread ID to the operand stack of the child
rt = yield_(rt)?;
assert_eq!(rt.current_thread.thread_id, MAIN_THREAD_ID);

// PID should remain on the operand stack
assert_eq!(rt.current_thread.operand_stack.len(), 1);

Ok(())
}

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

// The PID of child should be popped off the operand stack
assert!(rt.current_thread.operand_stack.is_empty());

Ok(())
}
}

0 comments on commit 3186dcf

Please sign in to comment.