Search code examples
android-ndkandroid-looper

When does ALooper_pollAll return ALOOPER_POLL_TIMEOUT?


Let's assume, that a couple of file descriptors with corresponding callbacks are added to the looper, and then ALooper_pollAll() is called with a timeout of 1000 milliseconds. Soon after that some of the file descriptors become available and the looper starts to calling callbacks on them. Let's say that the last callback called has finished exactly after 200ms ellapsed since the start of ALooper_pollAll. Now, if there's no more data on either of the descriptors, when will the function return ALOOPER_POLL_TIMEOUT? Is it after 800ms (remaining timeout time after the callbacks has finished), or after 1000ms (initial timeout)?


Solution

  • After looking at Looper's sources it became clear that it's the former. Internally ALooper_pollAll calls ALooper_pollOnce in a loop, updating (reducing) the timeout time after each consequent call, so eventually when it becomes 0, ALOOPER_POLL_TIMEOUT is returned.