Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only perform deadlock detection after the first failed lock.
Browse files Browse the repository at this point in the history
headius committed Aug 20, 2016
1 parent fe83dfc commit 2d6762e
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions core/src/main/java/org/jruby/runtime/load/LoadService.java
Original file line number Diff line number Diff line change
@@ -464,6 +464,18 @@ private LockResult lock(String requireName) {

private LockResult lockWithDeadlockDetection(RequireLock lock) {
while (true) {
// Try to lock for a variable amount of time and check again.
// We use a variable amount of time to decrease the likelihood that the deadlocking threads will
// always appear to be running and not waiting on a lock.
try {
boolean locked = lock.tryLock(500 + (int)(random.nextDouble() * 100), TimeUnit.MILLISECONDS);

if (locked) return LockResult.LOCKED;
} catch (InterruptedException ie) {
// ignore, proceed back to deadlock check
}

// failed to acquire lock, see if there's a deadlock involved
Thread owner = lock.getOwner();
if (owner != null) {
// already locked, scan for deadlocks
@@ -475,17 +487,6 @@ private LockResult lockWithDeadlockDetection(RequireLock lock) {
throw runtime.newLoadError("threads \"" + owner.getName() + "\" and \"" + Thread.currentThread().getName() + "\" will deadlock requiring \"" + lock.file + "\"");
}
}

// Otherwise try to lock for a variable amount of time and check again.
// We use a variable amount of time to decrease the likelihood that the deadlocking threads will
// always appear to be running and not waiting on a lock.
try {
boolean locked = lock.tryLock(500 + (int)(random.nextDouble() * 100), TimeUnit.MILLISECONDS);

if (locked) return LockResult.LOCKED;
} catch (InterruptedException ie) {
// ignore, proceed back to deadlock check
}
}
}

0 comments on commit 2d6762e

Please sign in to comment.