Skip to content

Commit

Permalink
Replace try/catch/propagate with try/finally idiom.
Browse files Browse the repository at this point in the history
This actually fixes a serious bug: A few of those Throwable.propagate calls were potentially wrapping InterruptedExceptions.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=25813299
  • Loading branch information
kluever committed Nov 30, 2011
1 parent c72ba76 commit 287bc67
Showing 1 changed file with 54 additions and 76 deletions.
130 changes: 54 additions & 76 deletions guava/src/com/google/common/util/concurrent/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,15 @@ public void enterWhen(Guard guard) throws InterruptedException {
}
final ReentrantLock lock = this.lock;
boolean reentrant = lock.isHeldByCurrentThread();
boolean success = false;
lock.lockInterruptibly();
try {
waitInterruptibly(guard, reentrant);
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
success = true;
} finally {
if (!success) {
lock.unlock();
}
}
}

Expand All @@ -378,12 +381,15 @@ public void enterWhenUninterruptibly(Guard guard) {
}
final ReentrantLock lock = this.lock;
boolean reentrant = lock.isHeldByCurrentThread();
boolean success = false;
lock.lock();
try {
waitUninterruptibly(guard, reentrant);
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
success = true;
} finally {
if (!success) {
lock.unlock();
}
}
}

Expand All @@ -404,20 +410,16 @@ public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws Interrupt
if (!lock.tryLock(time, unit)) {
return false;
}
boolean satisfied;
boolean satisfied = false;
try {
long remainingNanos = unit.toNanos(time) - (System.nanoTime() - startNanos);
satisfied = waitInterruptibly(guard, remainingNanos, reentrant);
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
}

/**
Expand Down Expand Up @@ -450,19 +452,15 @@ public boolean enterWhenUninterruptibly(Guard guard, long time, TimeUnit unit) {
remainingNanos = (timeoutNanos - (System.nanoTime() - startNanos));
}
}
boolean satisfied;
boolean satisfied = false;
try {
satisfied = waitUninterruptibly(guard, remainingNanos, reentrant);
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
} finally {
if (interruptIgnored) {
Thread.currentThread().interrupt();
Expand All @@ -482,19 +480,15 @@ public boolean enterIf(Guard guard) {
}
final ReentrantLock lock = this.lock;
lock.lock();
boolean satisfied;
boolean satisfied = false;
try {
satisfied = guard.isSatisfied();
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
}

/**
Expand All @@ -509,19 +503,15 @@ public boolean enterIfInterruptibly(Guard guard) throws InterruptedException {
}
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
boolean satisfied;
boolean satisfied = false;
try {
satisfied = guard.isSatisfied();
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
}

/**
Expand All @@ -538,19 +528,15 @@ public boolean enterIf(Guard guard, long time, TimeUnit unit) {
if (!enter(time, unit)) {
return false;
}
boolean satisfied;
boolean satisfied = false;
try {
satisfied = guard.isSatisfied();
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
}

/**
Expand All @@ -568,19 +554,15 @@ public boolean enterIfInterruptibly(Guard guard, long time, TimeUnit unit)
if (!lock.tryLock(time, unit)) {
return false;
}
boolean satisfied;
boolean satisfied = false;
try {
satisfied = guard.isSatisfied();
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
}

/**
Expand All @@ -599,19 +581,15 @@ public boolean tryEnterIf(Guard guard) {
if (!lock.tryLock()) {
return false;
}
boolean satisfied;
boolean satisfied = false;
try {
satisfied = guard.isSatisfied();
} catch (Throwable throwable) {
lock.unlock();
throw Throwables.propagate(throwable);
}
if (satisfied) {
return true;
} else {
lock.unlock();
return false;
} finally {
if (!satisfied) {
lock.unlock();
}
}
return satisfied;
}

/**
Expand Down

0 comments on commit 287bc67

Please sign in to comment.