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

Commit 76dfa5ef authored by Mark Fasheh's avatar Mark Fasheh
Browse files

ConcurrentMessageQueue: Skip trieber stack if enqueuing from Looper thread

Test: boot phone
Flag: build.RELEASE_PACKAGE_MESSAGEQUEUE_IMPLEMENTATION
Bug: 336880969
Change-Id: I8814c178e2b942b72df6e28fe8cbe7db2467b91a
parent bb62651d
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.TestApi;
import android.os.Handler;
import android.os.Looper;
import android.os.Trace;
import android.util.Log;
import android.util.Printer;
@@ -535,6 +536,7 @@ public final class MessageQueue {
    /* This is only read/written from the Looper thread */
    private int mNextPollTimeoutMillis;
    private static final AtomicLong mMessagesDelivered = new AtomicLong();
    private boolean mMessageDirectlyQueued;

    private Message nextMessage() {
        int i = 0;
@@ -729,6 +731,7 @@ public final class MessageQueue {
                Binder.flushPendingCommands();
            }

            mMessageDirectlyQueued = false;
            nativePollOnce(ptr, mNextPollTimeoutMillis);

            Message msg = nextMessage();
@@ -841,6 +844,22 @@ public final class MessageQueue {
                    + node.isAsync() + " now: " + SystemClock.uptimeMillis());
        }

        final Looper myLooper = Looper.myLooper();
        /* If we are running on the looper thread we can add directly to the priority queue */
        if (myLooper != null && myLooper.getQueue() == this) {
            node.removeFromStack();
            insertIntoPriorityQueue(node);
            /*
             * We still need to do this even though we are the current thread,
             * otherwise next() may sleep indefinitely.
             */
            if (!mMessageDirectlyQueued) {
                mMessageDirectlyQueued = true;
                nativeWake(mPtr);
            }
            return true;
        }

        while (true) {
            StackNode old = (StackNode) sState.getVolatile(this);
            boolean wakeNeeded;