Skip to content

Commit

Permalink
Fix cdl deadlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Nov 8, 2024
1 parent e4ae7a3 commit b909213
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Sync(val goal: Int) : Any() {
return
}
isBlocked = true
val threadInterrupted = Thread.currentThread().isInterrupted
// We don't need synchronized here because
// it is already inside a synchronized method
while (count < goal) {
Expand All @@ -30,6 +31,9 @@ class Sync(val goal: Int) : Any() {
// At this point no concurrency.
count = 0
signaler.clear()
if (threadInterrupted) {
Thread.currentThread().interrupt()
}
}

@Synchronized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
import java.util.concurrent.CountDownLatch;

public class CountDownLatchAwaitTimeoutNoDeadlock {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
CountDownLatch cdl = new CountDownLatch(1);
Thread t = new Thread(() -> {
try {
cdl.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
cdl.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
cdl.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
t.start();
try {
cdl.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
cdl.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
cdl.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class ConditionAwaitTimeoutNotifyInterrupt {
public static void main(String[] args) {
Lock l = new ReentrantLock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.pastalab.fray.core.scheduler.POSScheduler;
import org.pastalab.fray.core.scheduler.RandomScheduler;
import org.pastalab.fray.runtime.TargetTerminateException;
import org.pastalab.fray.test.success.cdl.CountDownLatchAwaitTimeoutNoDeadlock;
import org.pastalab.fray.test.success.cdl.CountDownLatchNormalNotify;
import org.pastalab.fray.test.success.condition.ConditionAwaitTimeoutInterrupt;
import org.pastalab.fray.test.success.condition.ConditionAwaitTimeoutNotifyInterrupt;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void testOne() throws Throwable {
new ExecutionInfo(
new LambdaExecutor(() -> {
try {
ReentrantLockTryLock.main(new String[]{});
CountDownLatchAwaitTimeoutNoDeadlock.main(new String[]{});
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit b909213

Please sign in to comment.