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

Commit d1d0550a authored by Mark Fasheh's avatar Mark Fasheh
Browse files

DeliQueue: store sync barrier state in mWaitState

nextMessage() stores whether we are waiting on a sync barrier.
If enqueueMessageUnchecked() sees this bit as set it will skip
the wakeup call to the looper thread.

Test: atest MessageQueueTest
Bug: 421623328
Flag: EXEMPT new data structure isn't used yet; usages will be flagged.
Change-Id: I0e413e44a5d8dcfee6385af93becde4689f1e9d4
parent b1963dc6
Loading
Loading
Loading
Loading
+27 −23
Original line number Diff line number Diff line
@@ -316,8 +316,8 @@ public final class MessageQueue {
                newWaitState = WaitState.incrementCounter(waitState);
            } else {
                final long TSmillis = WaitState.getTSMillis(waitState);

                if (msg.when < TSmillis) {
                if (msg.when < TSmillis
                        && (!WaitState.hasSyncBarrier(waitState) || msg.isAsynchronous())) {
                    newWaitState = WaitState.initCounter();
                    needWake = true;
                } else {
@@ -415,6 +415,7 @@ public final class MessageQueue {
            */
            Message next = null;

            boolean waitingOnSyncBarrier = false;
            /*
            * If we have a barrier we should return the async node (if it exists and is ready)
            */
@@ -422,6 +423,7 @@ public final class MessageQueue {
                if (asyncMsg != null && (returnEarliest || now >= asyncMsg.when)) {
                    found = asyncMsg;
                } else {
                    waitingOnSyncBarrier = true;
                    next = asyncMsg;
                }
            } else { /* No barrier. */
@@ -512,11 +514,9 @@ public final class MessageQueue {
            /*
             * Try to swap waitstate back from a counter to a deadline. If we can't then that means
             * the counter was incremented and we need to loop back to pick up any new items.
             *
             * TODO: Encode sync barrier state here
             */
            if (!sWaitState.compareAndSet(this, oldWaitState,
                    WaitState.composeDeadline(nextDeadline, false))) {
                    WaitState.composeDeadline(nextDeadline, waitingOnSyncBarrier))) {
                continue;
            }
            if (found != null || nextDeadline != 0) {
@@ -744,16 +744,21 @@ public final class MessageQueue {
                    + " barrier token has not been posted or has already been removed.");
        }

        if (Thread.currentThread() != mLooperThread) {
        boolean needWake;
        while (true) {
            long waitState = mWaitState;
            long newWaitState;

            if (WaitState.isCounter(waitState)) {
                // Thread is already awake and processing messages
                newWaitState = WaitState.incrementCounter(waitState);
                needWake = false;
            } else if (!WaitState.hasSyncBarrier(waitState)) {
                // Thread is asleep but not waiting on sync barrier
                newWaitState = WaitState.incrementDeadline(waitState);
                needWake = false;
            } else {
                // Thread is asleep, wake up
                newWaitState = WaitState.initCounter();
                needWake = true;
            }
@@ -766,7 +771,6 @@ public final class MessageQueue {
            concurrentWake();
        }
    }
    }

    /**
     * Get the timestamp of the next executable message in our priority queue.