-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Play Deadlock Empire in Fray! #153
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR introduces several translated test cases under the "Deadlock Empire" theme that simulate common concurrency pitfalls. Key changes include the addition of new concurrency test classes (e.g. CountdownEvent, CountdownEventRevisited, etc.), the use of various synchronization mechanisms (CountDownLatch, CyclicBarrier, ReentrantLock, Semaphore, condition variables), and minor documentation enhancements in the CHANGELOG.
Reviewed Changes
File | Description |
---|---|
example/src/test/java/org/pastalab/fray/example/deadlockempire/CountdownEvent.java | Adds a test demonstrating potential deadlock with CountDownLatch. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/CountdownEventRevisited.java | Adds a test to trigger an exception by over-signaling a CountDownLatch. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/ConfusedCounter.java | Adds a test to showcase non-atomic operations in a multithreaded context. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/Barrier.java | Adds a test using a misconfigured CyclicBarrier with multiple threads. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/Deadlock.java | Adds a classic deadlock demonstration using ReentrantLock. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/BooleanFlags.java | Adds a test demonstrating weak synchronization using a boolean flag. |
example/src/main/java/org/pastalab/fray/example/Main.java | Introduces a simple main method for application startup. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/BossFight.java | Adds a complex test involving semaphores and condition variables. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/ConditionVariables.java | Adds a test demonstrating thread coordination with wait/notify. |
example/src/test/java/org/pastalab/fray/example/deadlockempire/DeadlockEmpireTestBase.java | Provides common helper methods for the test cases. |
CHANGELOG.md | Updates the changelog to reflect the new tests and debugging features. |
Copilot reviewed 49 out of 49 changed files in this pull request and generated 3 comments.
while (true) { | ||
synchronized (mutex) { | ||
// If queue is empty, wait until producer adds something | ||
if (queue.isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using an 'if' statement before calling wait(), consider using a 'while' loop to handle spurious wakeups in the consumer thread.
if (queue.isEmpty()) { | |
while (queue.isEmpty()) { |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
while (true) { | ||
synchronized (mutex) { | ||
// If queue is empty, wait until producer adds something | ||
if (queue.isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace the 'if' condition with a 'while' loop to guard against spurious wakeups in the consumer thread.
if (queue.isEmpty()) { | |
while (queue.isEmpty()) { |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
try { | ||
fortress.acquire(); // Wait for a permit | ||
|
||
synchronized (sanctum) { | ||
try { | ||
sanctum.wait(); // Wait to be notified | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
|
||
criticalSection(); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acquiring the semaphore immediately after a successful tryAcquire() may lead to a deadlock if the semaphore’s permits are mismatched; consider reviewing the semaphore usage to ensure that permits are acquired and released symmetrically.
try { | |
fortress.acquire(); // Wait for a permit | |
synchronized (sanctum) { | |
try { | |
sanctum.wait(); // Wait to be notified | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
} | |
criticalSection(); | |
} | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
synchronized (sanctum) { | |
try { | |
sanctum.wait(); // Wait to be notified | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
} | |
criticalSection(); |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
No description provided.