The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 22 | import android.os.MessageQueueProto; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.util.Log; |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 24 | import android.util.Printer; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 25 | import android.util.SparseArray; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 26 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 28 | import java.io.FileDescriptor; |
| 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Low-level class holding the list of messages to be dispatched by a |
| 35 | * {@link Looper}. Messages are not added directly to a MessageQueue, |
| 36 | * but rather through {@link Handler} objects associated with the Looper. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 37 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | * <p>You can retrieve the MessageQueue for the current thread with |
| 39 | * {@link Looper#myQueue() Looper.myQueue()}. |
| 40 | */ |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 41 | public final class MessageQueue { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 42 | private static final String TAG = "MessageQueue"; |
| 43 | private static final boolean DEBUG = false; |
| 44 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 45 | // True if the message queue can be quit. |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 46 | @UnsupportedAppUsage |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 47 | private final boolean mQuitAllowed; |
| 48 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 49 | @UnsupportedAppUsage |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 50 | @SuppressWarnings("unused") |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 51 | private long mPtr; // used by native code |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 52 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 53 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | Message mMessages; |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 55 | @UnsupportedAppUsage |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 56 | private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>(); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 57 | private SparseArray<FileDescriptorRecord> mFileDescriptorRecords; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 58 | private IdleHandler[] mPendingIdleHandlers; |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 59 | private boolean mQuitting; |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 60 | |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 61 | // Indicates whether next() is blocked waiting in pollOnce() with a non-zero timeout. |
| 62 | private boolean mBlocked; |
| 63 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 64 | // The next barrier token. |
| 65 | // Barriers are indicated by messages with a null target whose arg1 field carries the token. |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 66 | @UnsupportedAppUsage |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 67 | private int mNextBarrierToken; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 68 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 69 | private native static long nativeInit(); |
| 70 | private native static void nativeDestroy(long ptr); |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 71 | @UnsupportedAppUsage |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 72 | private native void nativePollOnce(long ptr, int timeoutMillis); /*non-static for callbacks*/ |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 73 | private native static void nativeWake(long ptr); |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 74 | private native static boolean nativeIsPolling(long ptr); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 75 | private native static void nativeSetFileDescriptorEvents(long ptr, int fd, int events); |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 76 | |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 77 | MessageQueue(boolean quitAllowed) { |
| 78 | mQuitAllowed = quitAllowed; |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 79 | mPtr = nativeInit(); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 80 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 81 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 82 | @Override |
| 83 | protected void finalize() throws Throwable { |
| 84 | try { |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 85 | dispose(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 86 | } finally { |
| 87 | super.finalize(); |
| 88 | } |
| 89 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 90 | |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 91 | // Disposes of the underlying message queue. |
| 92 | // Must only be called on the looper thread or the finalizer. |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 93 | private void dispose() { |
| 94 | if (mPtr != 0) { |
| 95 | nativeDestroy(mPtr); |
| 96 | mPtr = 0; |
| 97 | } |
| 98 | } |
| 99 | |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Returns true if the looper has no pending messages which are due to be processed. |
| 102 | * |
| 103 | * <p>This method is safe to call from any thread. |
| 104 | * |
| 105 | * @return True if the looper is idle. |
| 106 | */ |
| 107 | public boolean isIdle() { |
| 108 | synchronized (this) { |
| 109 | final long now = SystemClock.uptimeMillis(); |
| 110 | return mMessages == null || now < mMessages.when; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Add a new {@link IdleHandler} to this message queue. This may be |
| 116 | * removed automatically for you by returning false from |
| 117 | * {@link IdleHandler#queueIdle IdleHandler.queueIdle()} when it is |
| 118 | * invoked, or explicitly removing it with {@link #removeIdleHandler}. |
| 119 | * |
| 120 | * <p>This method is safe to call from any thread. |
| 121 | * |
| 122 | * @param handler The IdleHandler to be added. |
| 123 | */ |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 124 | public void addIdleHandler(@NonNull IdleHandler handler) { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 125 | if (handler == null) { |
| 126 | throw new NullPointerException("Can't add a null IdleHandler"); |
| 127 | } |
| 128 | synchronized (this) { |
| 129 | mIdleHandlers.add(handler); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Remove an {@link IdleHandler} from the queue that was previously added |
| 135 | * with {@link #addIdleHandler}. If the given object is not currently |
| 136 | * in the idle list, nothing is done. |
| 137 | * |
| 138 | * <p>This method is safe to call from any thread. |
| 139 | * |
| 140 | * @param handler The IdleHandler to be removed. |
| 141 | */ |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 142 | public void removeIdleHandler(@NonNull IdleHandler handler) { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 143 | synchronized (this) { |
| 144 | mIdleHandlers.remove(handler); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns whether this looper's thread is currently polling for more work to do. |
| 150 | * This is a good signal that the loop is still alive rather than being stuck |
| 151 | * handling a callback. Note that this method is intrinsically racy, since the |
| 152 | * state of the loop can change before you get the result back. |
| 153 | * |
| 154 | * <p>This method is safe to call from any thread. |
| 155 | * |
| 156 | * @return True if the looper is currently polling for events. |
| 157 | * @hide |
| 158 | */ |
| 159 | public boolean isPolling() { |
| 160 | synchronized (this) { |
| 161 | return isPollingLocked(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private boolean isPollingLocked() { |
| 166 | // If the loop is quitting then it must not be idling. |
| 167 | // We can assume mPtr != 0 when mQuitting is false. |
| 168 | return !mQuitting && nativeIsPolling(mPtr); |
| 169 | } |
| 170 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 171 | /** |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 172 | * Adds a file descriptor listener to receive notification when file descriptor |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 173 | * related events occur. |
| 174 | * <p> |
| 175 | * If the file descriptor has already been registered, the specified events |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 176 | * and listener will replace any that were previously associated with it. |
| 177 | * It is not possible to set more than one listener per file descriptor. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 178 | * </p><p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 179 | * It is important to always unregister the listener when the file descriptor |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 180 | * is no longer of use. |
| 181 | * </p> |
| 182 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 183 | * @param fd The file descriptor for which a listener will be registered. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 184 | * @param events The set of events to receive: a combination of the |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 185 | * {@link OnFileDescriptorEventListener#EVENT_INPUT}, |
| 186 | * {@link OnFileDescriptorEventListener#EVENT_OUTPUT}, and |
| 187 | * {@link OnFileDescriptorEventListener#EVENT_ERROR} event masks. If the requested |
| 188 | * set of events is zero, then the listener is unregistered. |
| 189 | * @param listener The listener to invoke when file descriptor events occur. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 190 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 191 | * @see OnFileDescriptorEventListener |
| 192 | * @see #removeOnFileDescriptorEventListener |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 193 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 194 | public void addOnFileDescriptorEventListener(@NonNull FileDescriptor fd, |
| 195 | @OnFileDescriptorEventListener.Events int events, |
| 196 | @NonNull OnFileDescriptorEventListener listener) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 197 | if (fd == null) { |
| 198 | throw new IllegalArgumentException("fd must not be null"); |
| 199 | } |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 200 | if (listener == null) { |
| 201 | throw new IllegalArgumentException("listener must not be null"); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | synchronized (this) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 205 | updateOnFileDescriptorEventListenerLocked(fd, events, listener); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 210 | * Removes a file descriptor listener. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 211 | * <p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 212 | * This method does nothing if no listener has been registered for the |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 213 | * specified file descriptor. |
| 214 | * </p> |
| 215 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 216 | * @param fd The file descriptor whose listener will be unregistered. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 217 | * |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 218 | * @see OnFileDescriptorEventListener |
| 219 | * @see #addOnFileDescriptorEventListener |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 220 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 221 | public void removeOnFileDescriptorEventListener(@NonNull FileDescriptor fd) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 222 | if (fd == null) { |
| 223 | throw new IllegalArgumentException("fd must not be null"); |
| 224 | } |
| 225 | |
| 226 | synchronized (this) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 227 | updateOnFileDescriptorEventListenerLocked(fd, 0, null); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 231 | private void updateOnFileDescriptorEventListenerLocked(FileDescriptor fd, int events, |
| 232 | OnFileDescriptorEventListener listener) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 233 | final int fdNum = fd.getInt$(); |
| 234 | |
| 235 | int index = -1; |
| 236 | FileDescriptorRecord record = null; |
| 237 | if (mFileDescriptorRecords != null) { |
| 238 | index = mFileDescriptorRecords.indexOfKey(fdNum); |
| 239 | if (index >= 0) { |
| 240 | record = mFileDescriptorRecords.valueAt(index); |
| 241 | if (record != null && record.mEvents == events) { |
| 242 | return; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (events != 0) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 248 | events |= OnFileDescriptorEventListener.EVENT_ERROR; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 249 | if (record == null) { |
| 250 | if (mFileDescriptorRecords == null) { |
| 251 | mFileDescriptorRecords = new SparseArray<FileDescriptorRecord>(); |
| 252 | } |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 253 | record = new FileDescriptorRecord(fd, events, listener); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 254 | mFileDescriptorRecords.put(fdNum, record); |
| 255 | } else { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 256 | record.mListener = listener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 257 | record.mEvents = events; |
| 258 | record.mSeq += 1; |
| 259 | } |
| 260 | nativeSetFileDescriptorEvents(mPtr, fdNum, events); |
| 261 | } else if (record != null) { |
| 262 | record.mEvents = 0; |
| 263 | mFileDescriptorRecords.removeAt(index); |
David Pursell | c16b775 | 2017-08-07 16:32:36 -0700 | [diff] [blame] | 264 | nativeSetFileDescriptorEvents(mPtr, fdNum, 0); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | // Called from native code. |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 269 | @UnsupportedAppUsage |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 270 | private int dispatchEvents(int fd, int events) { |
| 271 | // Get the file descriptor record and any state that might change. |
| 272 | final FileDescriptorRecord record; |
| 273 | final int oldWatchedEvents; |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 274 | final OnFileDescriptorEventListener listener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 275 | final int seq; |
| 276 | synchronized (this) { |
| 277 | record = mFileDescriptorRecords.get(fd); |
| 278 | if (record == null) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 279 | return 0; // spurious, no listener registered |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | oldWatchedEvents = record.mEvents; |
| 283 | events &= oldWatchedEvents; // filter events based on current watched set |
| 284 | if (events == 0) { |
| 285 | return oldWatchedEvents; // spurious, watched events changed |
| 286 | } |
| 287 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 288 | listener = record.mListener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 289 | seq = record.mSeq; |
| 290 | } |
| 291 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 292 | // Invoke the listener outside of the lock. |
| 293 | int newWatchedEvents = listener.onFileDescriptorEvents( |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 294 | record.mDescriptor, events); |
| 295 | if (newWatchedEvents != 0) { |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 296 | newWatchedEvents |= OnFileDescriptorEventListener.EVENT_ERROR; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 299 | // Update the file descriptor record if the listener changed the set of |
| 300 | // events to watch and the listener itself hasn't been updated since. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 301 | if (newWatchedEvents != oldWatchedEvents) { |
| 302 | synchronized (this) { |
| 303 | int index = mFileDescriptorRecords.indexOfKey(fd); |
| 304 | if (index >= 0 && mFileDescriptorRecords.valueAt(index) == record |
| 305 | && record.mSeq == seq) { |
| 306 | record.mEvents = newWatchedEvents; |
| 307 | if (newWatchedEvents == 0) { |
| 308 | mFileDescriptorRecords.removeAt(index); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Return the new set of events to watch for native code to take care of. |
| 315 | return newWatchedEvents; |
| 316 | } |
| 317 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 318 | @UnsupportedAppUsage |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 319 | Message next() { |
Jeff Brown | 6058390 | 2013-09-09 16:14:20 -0700 | [diff] [blame] | 320 | // Return here if the message loop has already quit and been disposed. |
| 321 | // This can happen if the application tries to restart a looper after quit |
| 322 | // which is not supported. |
Narayan Kamath | ab86434 | 2014-01-16 11:22:52 +0000 | [diff] [blame] | 323 | final long ptr = mPtr; |
Jeff Brown | 6058390 | 2013-09-09 16:14:20 -0700 | [diff] [blame] | 324 | if (ptr == 0) { |
| 325 | return null; |
| 326 | } |
| 327 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 328 | int pendingIdleHandlerCount = -1; // -1 only during first iteration |
| 329 | int nextPollTimeoutMillis = 0; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 330 | for (;;) { |
| 331 | if (nextPollTimeoutMillis != 0) { |
| 332 | Binder.flushPendingCommands(); |
| 333 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 334 | |
Jeff Brown | 6058390 | 2013-09-09 16:14:20 -0700 | [diff] [blame] | 335 | nativePollOnce(ptr, nextPollTimeoutMillis); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | synchronized (this) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 338 | // Try to retrieve the next message. Return if found. |
| 339 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 340 | Message prevMsg = null; |
| 341 | Message msg = mMessages; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 342 | if (msg != null && msg.target == null) { |
| 343 | // Stalled by a barrier. Find the next asynchronous message in the queue. |
| 344 | do { |
| 345 | prevMsg = msg; |
| 346 | msg = msg.next; |
| 347 | } while (msg != null && !msg.isAsynchronous()); |
| 348 | } |
| 349 | if (msg != null) { |
| 350 | if (now < msg.when) { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 351 | // Next message is not ready. Set a timeout to wake up when it is ready. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 352 | nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE); |
| 353 | } else { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 354 | // Got a message. |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 355 | mBlocked = false; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 356 | if (prevMsg != null) { |
| 357 | prevMsg.next = msg.next; |
| 358 | } else { |
| 359 | mMessages = msg.next; |
| 360 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 361 | msg.next = null; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 362 | if (DEBUG) Log.v(TAG, "Returning message: " + msg); |
| 363 | msg.markInUse(); |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 364 | return msg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 366 | } else { |
| 367 | // No more messages. |
| 368 | nextPollTimeoutMillis = -1; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 371 | // Process the quit message now that all pending messages have been handled. |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 372 | if (mQuitting) { |
Jeff Brown | 024136f | 2013-04-11 19:21:32 -0700 | [diff] [blame] | 373 | dispose(); |
| 374 | return null; |
| 375 | } |
| 376 | |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 377 | // If first time idle, then get the number of idlers to run. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 378 | // Idle handles only run if the queue is empty or if the first message |
| 379 | // in the queue (possibly a barrier) is due to be handled in the future. |
| 380 | if (pendingIdleHandlerCount < 0 |
| 381 | && (mMessages == null || now < mMessages.when)) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 382 | pendingIdleHandlerCount = mIdleHandlers.size(); |
| 383 | } |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 384 | if (pendingIdleHandlerCount <= 0) { |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 385 | // No idle handlers to run. Loop and wait some more. |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 386 | mBlocked = true; |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 387 | continue; |
| 388 | } |
| 389 | |
| 390 | if (mPendingIdleHandlers == null) { |
| 391 | mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)]; |
| 392 | } |
| 393 | mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); |
| 394 | } |
| 395 | |
| 396 | // Run the idle handlers. |
| 397 | // We only ever reach this code block during the first iteration. |
| 398 | for (int i = 0; i < pendingIdleHandlerCount; i++) { |
| 399 | final IdleHandler idler = mPendingIdleHandlers[i]; |
| 400 | mPendingIdleHandlers[i] = null; // release the reference to the handler |
| 401 | |
| 402 | boolean keep = false; |
| 403 | try { |
| 404 | keep = idler.queueIdle(); |
| 405 | } catch (Throwable t) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 406 | Log.wtf(TAG, "IdleHandler threw exception", t); |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (!keep) { |
| 410 | synchronized (this) { |
| 411 | mIdleHandlers.remove(idler); |
| 412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 416 | // Reset the idle handler count to 0 so we do not run them again. |
| 417 | pendingIdleHandlerCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | |
Jeff Brown | ed73932 | 2010-09-21 15:14:14 -0700 | [diff] [blame] | 419 | // While calling an idle handler, a new message could have been delivered |
| 420 | // so go back and look again for a pending message without waiting. |
| 421 | nextPollTimeoutMillis = 0; |
| 422 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 425 | void quit(boolean safe) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 426 | if (!mQuitAllowed) { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 427 | throw new IllegalStateException("Main thread not allowed to quit."); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | synchronized (this) { |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 431 | if (mQuitting) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 434 | mQuitting = true; |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 435 | |
| 436 | if (safe) { |
| 437 | removeAllFutureMessagesLocked(); |
| 438 | } else { |
| 439 | removeAllMessagesLocked(); |
| 440 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 441 | |
| 442 | // We can assume mPtr != 0 because mQuitting was previously false. |
| 443 | nativeWake(mPtr); |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 444 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 447 | /** |
| 448 | * Posts a synchronization barrier to the Looper's message queue. |
| 449 | * |
| 450 | * Message processing occurs as usual until the message queue encounters the |
| 451 | * synchronization barrier that has been posted. When the barrier is encountered, |
| 452 | * later synchronous messages in the queue are stalled (prevented from being executed) |
| 453 | * until the barrier is released by calling {@link #removeSyncBarrier} and specifying |
| 454 | * the token that identifies the synchronization barrier. |
| 455 | * |
| 456 | * This method is used to immediately postpone execution of all subsequently posted |
| 457 | * synchronous messages until a condition is met that releases the barrier. |
| 458 | * Asynchronous messages (see {@link Message#isAsynchronous} are exempt from the barrier |
| 459 | * and continue to be processed as usual. |
| 460 | * |
| 461 | * This call must be always matched by a call to {@link #removeSyncBarrier} with |
| 462 | * the same token to ensure that the message queue resumes normal operation. |
| 463 | * Otherwise the application will probably hang! |
| 464 | * |
| 465 | * @return A token that uniquely identifies the barrier. This token must be |
| 466 | * passed to {@link #removeSyncBarrier} to release the barrier. |
| 467 | * |
| 468 | * @hide |
| 469 | */ |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 470 | @UnsupportedAppUsage |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 471 | public int postSyncBarrier() { |
| 472 | return postSyncBarrier(SystemClock.uptimeMillis()); |
| 473 | } |
| 474 | |
| 475 | private int postSyncBarrier(long when) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 476 | // Enqueue a new sync barrier token. |
| 477 | // We don't need to wake the queue because the purpose of a barrier is to stall it. |
| 478 | synchronized (this) { |
| 479 | final int token = mNextBarrierToken++; |
| 480 | final Message msg = Message.obtain(); |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 481 | msg.markInUse(); |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 482 | msg.when = when; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 483 | msg.arg1 = token; |
| 484 | |
| 485 | Message prev = null; |
| 486 | Message p = mMessages; |
| 487 | if (when != 0) { |
| 488 | while (p != null && p.when <= when) { |
| 489 | prev = p; |
| 490 | p = p.next; |
| 491 | } |
| 492 | } |
| 493 | if (prev != null) { // invariant: p == prev.next |
| 494 | msg.next = p; |
| 495 | prev.next = msg; |
| 496 | } else { |
| 497 | msg.next = p; |
| 498 | mMessages = msg; |
| 499 | } |
| 500 | return token; |
| 501 | } |
| 502 | } |
| 503 | |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 504 | /** |
| 505 | * Removes a synchronization barrier. |
| 506 | * |
| 507 | * @param token The synchronization barrier token that was returned by |
| 508 | * {@link #postSyncBarrier}. |
| 509 | * |
| 510 | * @throws IllegalStateException if the barrier was not found. |
| 511 | * |
| 512 | * @hide |
| 513 | */ |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 514 | @UnsupportedAppUsage |
Jeff Brown | 3d4e7efe | 2015-02-26 15:34:16 -0800 | [diff] [blame] | 515 | public void removeSyncBarrier(int token) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 516 | // Remove a sync barrier token from the queue. |
| 517 | // If the queue is no longer stalled by a barrier then wake it. |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 518 | synchronized (this) { |
| 519 | Message prev = null; |
| 520 | Message p = mMessages; |
| 521 | while (p != null && (p.target != null || p.arg1 != token)) { |
| 522 | prev = p; |
| 523 | p = p.next; |
| 524 | } |
| 525 | if (p == null) { |
| 526 | throw new IllegalStateException("The specified message queue synchronization " |
| 527 | + " barrier token has not been posted or has already been removed."); |
| 528 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 529 | final boolean needWake; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 530 | if (prev != null) { |
| 531 | prev.next = p.next; |
| 532 | needWake = false; |
| 533 | } else { |
| 534 | mMessages = p.next; |
| 535 | needWake = mMessages == null || mMessages.target != null; |
| 536 | } |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 537 | p.recycleUnchecked(); |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 538 | |
| 539 | // If the loop is quitting then it is already awake. |
| 540 | // We can assume mPtr != 0 when mQuitting is false. |
| 541 | if (needWake && !mQuitting) { |
| 542 | nativeWake(mPtr); |
| 543 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 547 | boolean enqueueMessage(Message msg, long when) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 548 | if (msg.target == null) { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 549 | throw new IllegalArgumentException("Message must have a target."); |
| 550 | } |
| 551 | if (msg.isInUse()) { |
| 552 | throw new IllegalStateException(msg + " This message is already in use."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | synchronized (this) { |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 556 | if (mQuitting) { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 557 | IllegalStateException e = new IllegalStateException( |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 558 | msg.target + " sending message to a Handler on a dead thread"); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 559 | Log.w(TAG, e.getMessage(), e); |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 560 | msg.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 564 | msg.markInUse(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | msg.when = when; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | Message p = mMessages; |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 567 | boolean needWake; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | if (p == null || when == 0 || when < p.when) { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 569 | // New head, wake up the event queue if blocked. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | msg.next = p; |
| 571 | mMessages = msg; |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 572 | needWake = mBlocked; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | } else { |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 574 | // Inserted within the middle of the queue. Usually we don't have to wake |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 575 | // up the event queue unless there is a barrier at the head of the queue |
| 576 | // and the message is the earliest asynchronous message in the queue. |
| 577 | needWake = mBlocked && p.target == null && msg.isAsynchronous(); |
| 578 | Message prev; |
| 579 | for (;;) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | prev = p; |
| 581 | p = p.next; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 582 | if (p == null || when < p.when) { |
| 583 | break; |
| 584 | } |
| 585 | if (needWake && p.isAsynchronous()) { |
| 586 | needWake = false; |
| 587 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | } |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 589 | msg.next = p; // invariant: p == prev.next |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | prev.next = msg; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | } |
Jeff Brown | 013cf84 | 2013-09-06 16:24:36 -0700 | [diff] [blame] | 592 | |
| 593 | // We can assume mPtr != 0 because mQuitting is false. |
| 594 | if (needWake) { |
| 595 | nativeWake(mPtr); |
| 596 | } |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 601 | boolean hasMessages(Handler h, int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 602 | if (h == null) { |
| 603 | return false; |
| 604 | } |
| 605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | synchronized (this) { |
| 607 | Message p = mMessages; |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 608 | while (p != null) { |
| 609 | if (p.target == h && p.what == what && (object == null || p.obj == object)) { |
| 610 | return true; |
| 611 | } |
| 612 | p = p.next; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | } |
| 617 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 618 | @UnsupportedAppUsage |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 619 | boolean hasMessages(Handler h, Runnable r, Object object) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 620 | if (h == null) { |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | synchronized (this) { |
| 625 | Message p = mMessages; |
| 626 | while (p != null) { |
| 627 | if (p.target == h && p.callback == r && (object == null || p.obj == object)) { |
| 628 | return true; |
| 629 | } |
| 630 | p = p.next; |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | } |
| 635 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 636 | boolean hasMessages(Handler h) { |
| 637 | if (h == null) { |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | synchronized (this) { |
| 642 | Message p = mMessages; |
| 643 | while (p != null) { |
| 644 | if (p.target == h) { |
| 645 | return true; |
| 646 | } |
| 647 | p = p.next; |
| 648 | } |
| 649 | return false; |
| 650 | } |
| 651 | } |
| 652 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 653 | void removeMessages(Handler h, int what, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 654 | if (h == null) { |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | synchronized (this) { |
| 659 | Message p = mMessages; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | |
| 661 | // Remove all messages at front. |
| 662 | while (p != null && p.target == h && p.what == what |
| 663 | && (object == null || p.obj == object)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | Message n = p.next; |
| 665 | mMessages = n; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 666 | p.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | p = n; |
| 668 | } |
| 669 | |
| 670 | // Remove all messages after front. |
| 671 | while (p != null) { |
| 672 | Message n = p.next; |
| 673 | if (n != null) { |
| 674 | if (n.target == h && n.what == what |
| 675 | && (object == null || n.obj == object)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | Message nn = n.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 677 | n.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | p.next = nn; |
| 679 | continue; |
| 680 | } |
| 681 | } |
| 682 | p = n; |
| 683 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 687 | void removeMessages(Handler h, Runnable r, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 688 | if (h == null || r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | return; |
| 690 | } |
| 691 | |
| 692 | synchronized (this) { |
| 693 | Message p = mMessages; |
| 694 | |
| 695 | // Remove all messages at front. |
| 696 | while (p != null && p.target == h && p.callback == r |
| 697 | && (object == null || p.obj == object)) { |
| 698 | Message n = p.next; |
| 699 | mMessages = n; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 700 | p.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | p = n; |
| 702 | } |
| 703 | |
| 704 | // Remove all messages after front. |
| 705 | while (p != null) { |
| 706 | Message n = p.next; |
| 707 | if (n != null) { |
| 708 | if (n.target == h && n.callback == r |
| 709 | && (object == null || n.obj == object)) { |
| 710 | Message nn = n.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 711 | n.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | p.next = nn; |
| 713 | continue; |
| 714 | } |
| 715 | } |
| 716 | p = n; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Jeff Brown | 67fc67c | 2013-04-01 13:00:33 -0700 | [diff] [blame] | 721 | void removeCallbacksAndMessages(Handler h, Object object) { |
Jeff Brown | 0f85ce3 | 2012-02-16 14:41:10 -0800 | [diff] [blame] | 722 | if (h == null) { |
| 723 | return; |
| 724 | } |
| 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | synchronized (this) { |
| 727 | Message p = mMessages; |
| 728 | |
| 729 | // Remove all messages at front. |
| 730 | while (p != null && p.target == h |
| 731 | && (object == null || p.obj == object)) { |
| 732 | Message n = p.next; |
| 733 | mMessages = n; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 734 | p.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | p = n; |
| 736 | } |
| 737 | |
| 738 | // Remove all messages after front. |
| 739 | while (p != null) { |
| 740 | Message n = p.next; |
| 741 | if (n != null) { |
| 742 | if (n.target == h && (object == null || n.obj == object)) { |
| 743 | Message nn = n.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 744 | n.recycleUnchecked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | p.next = nn; |
| 746 | continue; |
| 747 | } |
| 748 | } |
| 749 | p = n; |
| 750 | } |
| 751 | } |
| 752 | } |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 753 | |
| 754 | private void removeAllMessagesLocked() { |
| 755 | Message p = mMessages; |
| 756 | while (p != null) { |
| 757 | Message n = p.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 758 | p.recycleUnchecked(); |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 759 | p = n; |
| 760 | } |
| 761 | mMessages = null; |
| 762 | } |
| 763 | |
| 764 | private void removeAllFutureMessagesLocked() { |
| 765 | final long now = SystemClock.uptimeMillis(); |
| 766 | Message p = mMessages; |
| 767 | if (p != null) { |
| 768 | if (p.when > now) { |
| 769 | removeAllMessagesLocked(); |
| 770 | } else { |
| 771 | Message n; |
| 772 | for (;;) { |
| 773 | n = p.next; |
| 774 | if (n == null) { |
| 775 | return; |
| 776 | } |
| 777 | if (n.when > now) { |
| 778 | break; |
| 779 | } |
| 780 | p = n; |
| 781 | } |
| 782 | p.next = null; |
| 783 | do { |
| 784 | p = n; |
| 785 | n = p.next; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 786 | p.recycleUnchecked(); |
Jeff Brown | 8b60e45 | 2013-04-18 15:17:48 -0700 | [diff] [blame] | 787 | } while (n != null); |
| 788 | } |
| 789 | } |
| 790 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 791 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 792 | void dump(Printer pw, String prefix, Handler h) { |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 793 | synchronized (this) { |
| 794 | long now = SystemClock.uptimeMillis(); |
| 795 | int n = 0; |
| 796 | for (Message msg = mMessages; msg != null; msg = msg.next) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 797 | if (h == null || h == msg.target) { |
| 798 | pw.println(prefix + "Message " + n + ": " + msg.toString(now)); |
| 799 | } |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 800 | n++; |
| 801 | } |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 802 | pw.println(prefix + "(Total messages: " + n + ", polling=" + isPollingLocked() |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 803 | + ", quitting=" + mQuitting + ")"); |
| 804 | } |
| 805 | } |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 806 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 807 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 808 | final long messageQueueToken = proto.start(fieldId); |
| 809 | synchronized (this) { |
| 810 | for (Message msg = mMessages; msg != null; msg = msg.next) { |
| 811 | msg.writeToProto(proto, MessageQueueProto.MESSAGES); |
| 812 | } |
| 813 | proto.write(MessageQueueProto.IS_POLLING_LOCKED, isPollingLocked()); |
| 814 | proto.write(MessageQueueProto.IS_QUITTING, mQuitting); |
| 815 | } |
| 816 | proto.end(messageQueueToken); |
| 817 | } |
| 818 | |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 819 | /** |
| 820 | * Callback interface for discovering when a thread is going to block |
| 821 | * waiting for more messages. |
| 822 | */ |
| 823 | public static interface IdleHandler { |
| 824 | /** |
| 825 | * Called when the message queue has run out of messages and will now |
| 826 | * wait for more. Return true to keep your idle handler active, false |
| 827 | * to have it removed. This may be called if there are still messages |
| 828 | * pending in the queue, but they are all scheduled to be dispatched |
| 829 | * after the current time. |
| 830 | */ |
| 831 | boolean queueIdle(); |
| 832 | } |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 833 | |
| 834 | /** |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 835 | * A listener which is invoked when file descriptor related events occur. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 836 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 837 | public interface OnFileDescriptorEventListener { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 838 | /** |
| 839 | * File descriptor event: Indicates that the file descriptor is ready for input |
| 840 | * operations, such as reading. |
| 841 | * <p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 842 | * The listener should read all available data from the file descriptor |
| 843 | * then return <code>true</code> to keep the listener active or <code>false</code> |
| 844 | * to remove the listener. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 845 | * </p><p> |
| 846 | * In the case of a socket, this event may be generated to indicate |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 847 | * that there is at least one incoming connection that the listener |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 848 | * should accept. |
| 849 | * </p><p> |
| 850 | * This event will only be generated if the {@link #EVENT_INPUT} event mask was |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 851 | * specified when the listener was added. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 852 | * </p> |
| 853 | */ |
| 854 | public static final int EVENT_INPUT = 1 << 0; |
| 855 | |
| 856 | /** |
| 857 | * File descriptor event: Indicates that the file descriptor is ready for output |
| 858 | * operations, such as writing. |
| 859 | * <p> |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 860 | * The listener should write as much data as it needs. If it could not |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 861 | * write everything at once, then it should return <code>true</code> to |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 862 | * keep the listener active. Otherwise, it should return <code>false</code> |
| 863 | * to remove the listener then re-register it later when it needs to write |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 864 | * something else. |
| 865 | * </p><p> |
| 866 | * This event will only be generated if the {@link #EVENT_OUTPUT} event mask was |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 867 | * specified when the listener was added. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 868 | * </p> |
| 869 | */ |
| 870 | public static final int EVENT_OUTPUT = 1 << 1; |
| 871 | |
| 872 | /** |
| 873 | * File descriptor event: Indicates that the file descriptor encountered a |
| 874 | * fatal error. |
| 875 | * <p> |
| 876 | * File descriptor errors can occur for various reasons. One common error |
| 877 | * is when the remote peer of a socket or pipe closes its end of the connection. |
| 878 | * </p><p> |
| 879 | * This event may be generated at any time regardless of whether the |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 880 | * {@link #EVENT_ERROR} event mask was specified when the listener was added. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 881 | * </p> |
| 882 | */ |
| 883 | public static final int EVENT_ERROR = 1 << 2; |
| 884 | |
| 885 | /** @hide */ |
| 886 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 887 | @IntDef(flag = true, prefix = { "EVENT_" }, value = { |
| 888 | EVENT_INPUT, |
| 889 | EVENT_OUTPUT, |
| 890 | EVENT_ERROR |
| 891 | }) |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 892 | public @interface Events {} |
| 893 | |
| 894 | /** |
| 895 | * Called when a file descriptor receives events. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 896 | * |
| 897 | * @param fd The file descriptor. |
| 898 | * @param events The set of events that occurred: a combination of the |
| 899 | * {@link #EVENT_INPUT}, {@link #EVENT_OUTPUT}, and {@link #EVENT_ERROR} event masks. |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 900 | * @return The new set of events to watch, or 0 to unregister the listener. |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 901 | * |
| 902 | * @see #EVENT_INPUT |
| 903 | * @see #EVENT_OUTPUT |
| 904 | * @see #EVENT_ERROR |
| 905 | */ |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 906 | @Events int onFileDescriptorEvents(@NonNull FileDescriptor fd, @Events int events); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | private static final class FileDescriptorRecord { |
| 910 | public final FileDescriptor mDescriptor; |
| 911 | public int mEvents; |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 912 | public OnFileDescriptorEventListener mListener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 913 | public int mSeq; |
| 914 | |
| 915 | public FileDescriptorRecord(FileDescriptor descriptor, |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 916 | int events, OnFileDescriptorEventListener listener) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 917 | mDescriptor = descriptor; |
| 918 | mEvents = events; |
Jeff Brown | 3f64052 | 2015-05-15 12:26:15 -0700 | [diff] [blame] | 919 | mListener = listener; |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 920 | } |
| 921 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | } |