From 3d4e7efe37a4b0dfc5807444e8c3b98a28953377 Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Thu, 26 Feb 2015 15:34:16 -0800 Subject: [PATCH] Move sync barrier methods into MessageQueue. The methods were previously defined on Looper but on reflection they actually make more sense on the MessageQueue instead since the Looper class is primarily concerned with thread lifecycle rather than the actual messages themselves. Change-Id: Iff356b94754fc9960774fa17e3eec9604229cba6 --- core/java/android/os/Looper.java | 42 ------------------------ core/java/android/os/MessageQueue.java | 41 +++++++++++++++++++++-- core/java/android/view/ViewRootImpl.java | 6 ++-- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/core/java/android/os/Looper.java b/core/java/android/os/Looper.java index 8b991965110b6..7384dd295ef7b 100644 --- a/core/java/android/os/Looper.java +++ b/core/java/android/os/Looper.java @@ -242,48 +242,6 @@ public final class Looper { mQueue.quit(true); } - /** - * Posts a synchronization barrier to the Looper's message queue. - * - * Message processing occurs as usual until the message queue encounters the - * synchronization barrier that has been posted. When the barrier is encountered, - * later synchronous messages in the queue are stalled (prevented from being executed) - * until the barrier is released by calling {@link #removeSyncBarrier} and specifying - * the token that identifies the synchronization barrier. - * - * This method is used to immediately postpone execution of all subsequently posted - * synchronous messages until a condition is met that releases the barrier. - * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier - * and continue to be processed as usual. - * - * This call must be always matched by a call to {@link #removeSyncBarrier} with - * the same token to ensure that the message queue resumes normal operation. - * Otherwise the application will probably hang! - * - * @return A token that uniquely identifies the barrier. This token must be - * passed to {@link #removeSyncBarrier} to release the barrier. - * - * @hide - */ - public int postSyncBarrier() { - return mQueue.enqueueSyncBarrier(SystemClock.uptimeMillis()); - } - - - /** - * Removes a synchronization barrier. - * - * @param token The synchronization barrier token that was returned by - * {@link #postSyncBarrier}. - * - * @throws IllegalStateException if the barrier was not found. - * - * @hide - */ - public void removeSyncBarrier(int token) { - mQueue.removeSyncBarrier(token); - } - /** * Return the Thread associated with this Looper. */ diff --git a/core/java/android/os/MessageQueue.java b/core/java/android/os/MessageQueue.java index f4d609c694d70..d672f9bbf935b 100644 --- a/core/java/android/os/MessageQueue.java +++ b/core/java/android/os/MessageQueue.java @@ -275,7 +275,34 @@ public final class MessageQueue { } } - int enqueueSyncBarrier(long when) { + /** + * Posts a synchronization barrier to the Looper's message queue. + * + * Message processing occurs as usual until the message queue encounters the + * synchronization barrier that has been posted. When the barrier is encountered, + * later synchronous messages in the queue are stalled (prevented from being executed) + * until the barrier is released by calling {@link #removeSyncBarrier} and specifying + * the token that identifies the synchronization barrier. + * + * This method is used to immediately postpone execution of all subsequently posted + * synchronous messages until a condition is met that releases the barrier. + * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier + * and continue to be processed as usual. + * + * This call must be always matched by a call to {@link #removeSyncBarrier} with + * the same token to ensure that the message queue resumes normal operation. + * Otherwise the application will probably hang! + * + * @return A token that uniquely identifies the barrier. This token must be + * passed to {@link #removeSyncBarrier} to release the barrier. + * + * @hide + */ + public int postSyncBarrier() { + return postSyncBarrier(SystemClock.uptimeMillis()); + } + + private int postSyncBarrier(long when) { // Enqueue a new sync barrier token. // We don't need to wake the queue because the purpose of a barrier is to stall it. synchronized (this) { @@ -304,7 +331,17 @@ public final class MessageQueue { } } - void removeSyncBarrier(int token) { + /** + * Removes a synchronization barrier. + * + * @param token The synchronization barrier token that was returned by + * {@link #postSyncBarrier}. + * + * @throws IllegalStateException if the barrier was not found. + * + * @hide + */ + public void removeSyncBarrier(int token) { // Remove a sync barrier token from the queue. // If the queue is no longer stalled by a barrier then wake it. synchronized (this) { diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 14b867e2ab3bf..7cd12d52c2570 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -1049,7 +1049,7 @@ public final class ViewRootImpl implements ViewParent, void scheduleTraversals() { if (!mTraversalScheduled) { mTraversalScheduled = true; - mTraversalBarrier = mHandler.getLooper().postSyncBarrier(); + mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); mChoreographer.postCallback( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); if (!mUnbufferedInputDispatch) { @@ -1063,7 +1063,7 @@ public final class ViewRootImpl implements ViewParent, void unscheduleTraversals() { if (mTraversalScheduled) { mTraversalScheduled = false; - mHandler.getLooper().removeSyncBarrier(mTraversalBarrier); + mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); mChoreographer.removeCallbacks( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); } @@ -1072,7 +1072,7 @@ public final class ViewRootImpl implements ViewParent, void doTraversal() { if (mTraversalScheduled) { mTraversalScheduled = false; - mHandler.getLooper().removeSyncBarrier(mTraversalBarrier); + mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); if (mProfile) { Debug.startMethodTracing("ViewAncestor");