Fix ConditionVariable.block to use elapsedRealtime.

System.currentTimeMillis() is NEVER okay to use to calculate a time delta.
The original spirit of this probably should have used SystemClock.uptimeMillis(),
but use SystemClock.elapsedRealtime because it's the same as
System.currentTimeMillis() in all the cases where System.currentTimeMillis() is
correct.

Test: Treehugger
Change-Id: Ib6e090425af83e7e822fb12814d87e8a37e2d2ac
This commit is contained in:
Joe Onorato
2018-12-27 13:16:02 -08:00
parent e465eb1be5
commit 5da1b3dc05

View File

@@ -104,32 +104,32 @@ public class ConditionVariable
/**
* Block the current thread until the condition is opened or until
* timeout milliseconds have passed.
* timeoutMs milliseconds have passed.
*
* <p>
* If the condition is already opened, return immediately.
*
* @param timeout the maximum time to wait in milliseconds.
* @param timeoutMs the maximum time to wait in milliseconds.
*
* @return true if the condition was opened, false if the call returns
* because of the timeout.
*/
public boolean block(long timeout)
public boolean block(long timeoutMs)
{
// Object.wait(0) means wait forever, to mimic this, we just
// call the other block() method in that case. It simplifies
// this code for the common case.
if (timeout != 0) {
if (timeoutMs != 0) {
synchronized (this) {
long now = System.currentTimeMillis();
long end = now + timeout;
long now = SystemClock.elapsedRealtime();
long end = now + timeoutMs;
while (!mCondition && now < end) {
try {
this.wait(end-now);
}
catch (InterruptedException e) {
}
now = System.currentTimeMillis();
now = SystemClock.elapsedRealtime();
}
return mCondition;
}