@@ -224,10 +224,16 @@ void InputDispatcher::dispatchOnce() {
224224 AutoMutex _l (mLock );
225225 mDispatcherIsAliveCondition .broadcast ();
226226
227- dispatchOnceInnerLocked (&nextWakeupTime);
227+ // Run a dispatch loop if there are no pending commands.
228+ // The dispatch loop might enqueue commands to run afterwards.
229+ if (!haveCommandsLocked ()) {
230+ dispatchOnceInnerLocked (&nextWakeupTime);
231+ }
228232
233+ // Run all pending commands if there are any.
234+ // If any commands were run then force the next poll to wake up immediately.
229235 if (runCommandsLockedInterruptible ()) {
230- nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
236+ nextWakeupTime = LONG_LONG_MIN;
231237 }
232238 } // release lock
233239
@@ -562,6 +568,10 @@ bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry)
562568 return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT;
563569}
564570
571+ bool InputDispatcher::haveCommandsLocked () const {
572+ return !mCommandQueue .isEmpty ();
573+ }
574+
565575bool InputDispatcher::runCommandsLockedInterruptible () {
566576 if (mCommandQueue .isEmpty ()) {
567577 return false ;
@@ -3247,9 +3257,10 @@ status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChan
32473257 }
32483258
32493259 mLooper ->addFd (fd, 0 , ALOOPER_EVENT_INPUT, handleReceiveCallback, this );
3250-
3251- runCommandsLockedInterruptible ();
32523260 } // release lock
3261+
3262+ // Wake the looper because some connections have changed.
3263+ mLooper ->wake ();
32533264 return OK;
32543265}
32553266
@@ -3294,8 +3305,6 @@ status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& i
32943305 nsecs_t currentTime = now ();
32953306 abortBrokenDispatchCycleLocked (currentTime, connection, notify);
32963307
3297- runCommandsLockedInterruptible ();
3298-
32993308 connection->status = Connection::STATUS_ZOMBIE;
33003309 return OK;
33013310}
0 commit comments