Skip to content

Commit cacc84f

Browse files
authored
Merge pull request #224 from dhakimTRX/4.0.x-stable
Handle interrupts correctly, switch to a monotonically growing clock function in BlockingCell and SingleShotLinearTimer
2 parents 25576af + 669877f commit cacc84f

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/main/java/com/rabbitmq/utility/BlockingCell.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public synchronized T get(long timeout) throws InterruptedException, TimeoutExce
6666
if (timeout < 0)
6767
throw new AssertionError("Timeout cannot be less than zero");
6868

69-
long maxTime = System.currentTimeMillis() + timeout;
70-
long now;
71-
while (!_filled && (now = System.currentTimeMillis()) < maxTime) {
69+
long now = System.nanoTime() / NANOS_IN_MILLI;
70+
long maxTime = now + timeout;
71+
while (!_filled && (now = (System.nanoTime() / NANOS_IN_MILLI)) < maxTime) {
7272
wait(maxTime - now);
7373
}
7474

@@ -83,12 +83,19 @@ public synchronized T get(long timeout) throws InterruptedException, TimeoutExce
8383
* @return the waited-for value
8484
*/
8585
public synchronized T uninterruptibleGet() {
86-
while (true) {
87-
try {
88-
return get();
89-
} catch (InterruptedException ex) {
90-
// no special handling necessary
86+
boolean wasInterrupted = false;
87+
try {
88+
while (true) {
89+
try {
90+
return get();
91+
} catch (InterruptedException ex) {
92+
// no special handling necessary
93+
wasInterrupted = true;
94+
}
9195
}
96+
} finally {
97+
if (wasInterrupted)
98+
Thread.currentThread().interrupt();
9299
}
93100
}
94101

@@ -104,14 +111,20 @@ public synchronized T uninterruptibleGet() {
104111
public synchronized T uninterruptibleGet(int timeout) throws TimeoutException {
105112
long now = System.nanoTime() / NANOS_IN_MILLI;
106113
long runTime = now + timeout;
107-
108-
do {
109-
try {
110-
return get(runTime - now);
111-
} catch (InterruptedException e) {
112-
// Ignore.
113-
}
114-
} while ((timeout == INFINITY) || ((now = System.nanoTime() / NANOS_IN_MILLI) < runTime));
114+
boolean wasInterrupted = false;
115+
try {
116+
do {
117+
try {
118+
return get(runTime - now);
119+
} catch (InterruptedException e) {
120+
// Ignore.
121+
wasInterrupted = true;
122+
}
123+
} while ((timeout == INFINITY) || ((now = System.nanoTime() / NANOS_IN_MILLI) < runTime));
124+
} finally {
125+
if (wasInterrupted)
126+
Thread.currentThread().interrupt();
127+
}
115128

116129
throw new TimeoutException();
117130
}

src/main/java/com/rabbitmq/utility/SingleShotLinearTimer.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,27 @@ public TimerThread(long timeoutMillisec) {
7272
public void run() {
7373
try {
7474
long now;
75-
while ((now = System.nanoTime() / NANOS_IN_MILLI) < _runTime) {
76-
if (_task == null) break;
75+
boolean wasInterrupted = false;
76+
try {
77+
while ((now = System.nanoTime() / NANOS_IN_MILLI) < _runTime) {
78+
if (_task == null) break;
7779

78-
try {
79-
synchronized(this) {
80-
wait(_runTime - now);
80+
try {
81+
synchronized(this) {
82+
wait(_runTime - now);
83+
}
84+
} catch (InterruptedException e) {
85+
wasInterrupted = true;
8186
}
82-
} catch (InterruptedException e) {
83-
Thread.currentThread().interrupt();
8487
}
88+
} finally {
89+
if (wasInterrupted)
90+
Thread.currentThread().interrupt();
8591
}
8692

8793
Runnable task = _task;
8894
if (task != null) {
89-
task.run();
95+
task.run();
9096
}
9197

9298
} finally {

0 commit comments

Comments
 (0)