Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5da1b3dc authored by Joe Onorato's avatar Joe Onorato
Browse files

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
parent e465eb1b
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -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;
            }