From 5da1b3dc059ec71308383842707a248b40f5cade Mon Sep 17 00:00:00 2001 From: Joe Onorato Date: Thu, 27 Dec 2018 13:16:02 -0800 Subject: [PATCH] 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 --- core/java/android/os/ConditionVariable.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/java/android/os/ConditionVariable.java b/core/java/android/os/ConditionVariable.java index 1e820f9456897..a13eaa6f19bf5 100644 --- a/core/java/android/os/ConditionVariable.java +++ b/core/java/android/os/ConditionVariable.java @@ -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. * *

* 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; }