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

Commit 92de8485 authored by Shai Barack's avatar Shai Barack
Browse files

Avoid races between nextMessage() and stackHasMessages()

Apply ag/32896611 to ConcurrentMessageQueue for reference.

Bug: 396569494
Flag: EXEMPT - bugfix
Test: atest MessageQueueTest
Change-Id: I30261b54d0acd1befa3a049b8fe2eda51f27e2da
parent ad3850c3
Loading
Loading
Loading
Loading
+5 −14
Original line number Diff line number Diff line
@@ -148,10 +148,6 @@ public final class MessageQueue {
            return mMessage.when;
        }

        boolean isRemovedFromStack() {
            return mRemovedFromStackValue;
        }

        boolean removeFromStack() {
            return sRemovedFromStack.compareAndSet(this, false, true);
        }
@@ -1221,16 +1217,10 @@ public final class MessageQueue {
        StateNode bottom = getStateNode(top);

        /*
         * If the top node is a state node, there are no reachable messages.
         * If it's anything other than Active, we can quit as we know that next() is not
         * consuming items.
         * If the top node is Active then we know that next() is currently consuming items.
         * In that case we should wait next() has drained the stack.
         * If the top node is a state node, there are no reachable messages. We should still
         * wait for next to complete draining the stack.
         */
        if (top == bottom) {
            if (bottom != sStackStateActive) {
                return false;
            }
            waitForDrainCompleted();
            return false;
        }
@@ -1258,7 +1248,7 @@ public final class MessageQueue {
                        }
                    }
                } else {
                    return true;
                    break;
                }
            }

@@ -1268,7 +1258,6 @@ public final class MessageQueue {
                if (DEBUG) {
                    Log.d(TAG, "stackHasMessages next() is walking the stack, we must re-sample");
                }
                waitForDrainCompleted();
                break;
            }
            if (!n.isMessageNode()) {
@@ -1278,6 +1267,8 @@ public final class MessageQueue {
            p = (MessageNode) n;
        }

        waitForDrainCompleted();

        return found;
    }