From d96404938b08d25c553681dd8b5c30aa872d259b Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Tue, 21 Jan 2025 21:29:56 +0000 Subject: Simplify behavior of TestLooperManager#isBlockedOnSyncBarrier. Previously isBlockedOnSyncBarrier would only return true if the corresponding queue had a sync barrier as the next message, AND a another message was waiting to be executed. This is problematic for espresso (and I'd argue non-intuitive), which uses isBlockedOnSyncBarrier to determine if queue is idle or not. The presence of a sync barrier should make the queue idle. This commit simplifies the definition and implementation of isBlockedOnSyncBarrier to strictly mean 'the next message in the queue is a sync barrier'. Flag: android.os.message_queue_testability Test: atest TestLooperManagerTest Bug: 112000181 Change-Id: I443d3c8ed8d439f058136d55f70c9b8c2c444149 --- .../os/CombinedMessageQueue/MessageQueue.java | 48 +--------------------- .../os/ConcurrentMessageQueue/MessageQueue.java | 23 +---------- .../os/LegacyMessageQueue/MessageQueue.java | 30 ++------------ core/java/android/os/TestLooperManager.java | 3 -- 4 files changed, 6 insertions(+), 98 deletions(-) diff --git a/core/java/android/os/CombinedMessageQueue/MessageQueue.java b/core/java/android/os/CombinedMessageQueue/MessageQueue.java index 230fa3fec930..a3b7f01778f0 100644 --- a/core/java/android/os/CombinedMessageQueue/MessageQueue.java +++ b/core/java/android/os/CombinedMessageQueue/MessageQueue.java @@ -36,7 +36,6 @@ import android.util.Printer; import android.util.SparseArray; import android.util.proto.ProtoOutputStream; -import com.android.internal.annotations.GuardedBy; import com.android.internal.ravenwood.RavenwoodEnvironment; import dalvik.annotation.optimization.NeverCompile; @@ -1369,54 +1368,11 @@ public final class MessageQueue { Iterator queueIter = mPriorityQueue.iterator(); MessageNode queueNode = iterateNext(queueIter); - if (queueNode != null && queueNode.isBarrier()) { - long now = SystemClock.uptimeMillis(); - - /* Look for a deliverable async node. If one exists we are not blocked. */ - Iterator asyncQueueIter = mAsyncPriorityQueue.iterator(); - MessageNode asyncNode = iterateNext(asyncQueueIter); - if (asyncNode != null && now >= asyncNode.getWhen()) { - return false; - } - /* - * Look for a deliverable sync node. In this case, if one exists we are blocked - * since the barrier prevents delivery of the Message. - */ - while (queueNode != null && queueNode.isBarrier()) { - queueNode = iterateNext(queueIter); - } - if (queueNode != null && now >= queueNode.getWhen()) { - return true; - } - } + return (queueNode != null && queueNode.isBarrier()); } else { Message msg = mMessages; - if (msg != null && msg.target == null) { - Message iter = msg; - /* Look for a deliverable async node */ - do { - iter = iter.next; - } while (iter != null && !iter.isAsynchronous()); - - long now = SystemClock.uptimeMillis(); - if (iter != null && now >= iter.when) { - return false; - } - /* - * Look for a deliverable sync node. In this case, if one exists we are blocked - * since the barrier prevents delivery of the Message. - */ - iter = msg; - do { - iter = iter.next; - } while (iter != null && (iter.target == null || iter.isAsynchronous())); - - if (iter != null && now >= iter.when) { - return true; - } - } + return msg != null && msg.target == null; } - return false; } private static final class MatchHandlerWhatAndObject extends MessageCompare { diff --git a/core/java/android/os/ConcurrentMessageQueue/MessageQueue.java b/core/java/android/os/ConcurrentMessageQueue/MessageQueue.java index d7d8e4199b33..80da487a1358 100644 --- a/core/java/android/os/ConcurrentMessageQueue/MessageQueue.java +++ b/core/java/android/os/ConcurrentMessageQueue/MessageQueue.java @@ -16,7 +16,6 @@ package android.os; -import android.annotation.FlaggedApi; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -1122,27 +1121,7 @@ public final class MessageQueue { Iterator queueIter = mPriorityQueue.iterator(); MessageNode queueNode = iterateNext(queueIter); - if (queueNode != null && queueNode.isBarrier()) { - long now = SystemClock.uptimeMillis(); - - /* Look for a deliverable async node. If one exists we are not blocked. */ - Iterator asyncQueueIter = mAsyncPriorityQueue.iterator(); - MessageNode asyncNode = iterateNext(asyncQueueIter); - if (asyncNode != null && now >= asyncNode.getWhen()) { - return false; - } - /* - * Look for a deliverable sync node. In this case, if one exists we are blocked - * since the barrier prevents delivery of the Message. - */ - while (queueNode != null && queueNode.isBarrier()) { - queueNode = iterateNext(queueIter); - } - if (queueNode != null && now >= queueNode.getWhen()) { - return true; - } - } - return false; + return queueNode != null && queueNode.isBarrier(); } private StateNode getStateNode(StackNode node) { diff --git a/core/java/android/os/LegacyMessageQueue/MessageQueue.java b/core/java/android/os/LegacyMessageQueue/MessageQueue.java index c0333e914b4d..847cc764a7a3 100644 --- a/core/java/android/os/LegacyMessageQueue/MessageQueue.java +++ b/core/java/android/os/LegacyMessageQueue/MessageQueue.java @@ -16,7 +16,6 @@ package android.os; -import android.annotation.FlaggedApi; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -821,33 +820,10 @@ public final class MessageQueue { */ boolean isBlockedOnSyncBarrier() { throwIfNotTest(); - Message msg = mMessages; - if (msg != null && msg.target == null) { - Message iter = msg; - /* Look for a deliverable async node */ - do { - iter = iter.next; - } while (iter != null && !iter.isAsynchronous()); - - long now = SystemClock.uptimeMillis(); - if (iter != null && now >= iter.when) { - return false; - } - /* - * Look for a deliverable sync node. In this case, if one exists we are blocked - * since the barrier prevents delivery of the Message. - */ - iter = msg; - do { - iter = iter.next; - } while (iter != null && (iter.target == null || iter.isAsynchronous())); - - if (iter != null && now >= iter.when) { - return true; - } - return false; + synchronized (this) { + Message msg = mMessages; + return msg != null && msg.target == null; } - return false; } boolean hasMessages(Handler h, int what, Object object) { diff --git a/core/java/android/os/TestLooperManager.java b/core/java/android/os/TestLooperManager.java index 289b98c9b1d4..d451109554fa 100644 --- a/core/java/android/os/TestLooperManager.java +++ b/core/java/android/os/TestLooperManager.java @@ -129,9 +129,6 @@ public class TestLooperManager { /** * Checks whether the Looper is currently blocked on a sync barrier. - * - * A Looper is blocked on a sync barrier if there is a Message in the Looper's - * queue that is ready for execution but is behind a sync barrier */ @FlaggedApi(Flags.FLAG_MESSAGE_QUEUE_TESTABILITY) public boolean isBlockedOnSyncBarrier() { -- cgit v1.2.3-59-g8ed1b