am af685f3b: am 85a7f99c: Merge "Refactor how timeouts are calculated. (DO NOT MERGE)" into honeycomb-mr2

* commit 'af685f3bb566f297deee1615d55d4f33d5580ba3':
  Refactor how timeouts are calculated. (DO NOT MERGE)
This commit is contained in:
Jeff Brown 2011-05-25 14:42:20 -07:00 committed by Android Git Automerger
commit 9a2c1ca2dd
3 changed files with 35 additions and 12 deletions

View File

@ -88,6 +88,16 @@ nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC);
nsecs_t systemTime(int clock);
#endif // def __cplusplus
/**
* Returns the number of milliseconds to wait between the reference time and the timeout time.
* If the timeout is in the past relative to the reference time, returns 0.
* If the timeout is more than INT_MAX milliseconds in the future relative to the reference time,
* such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay.
* Otherwise, returns the difference between the reference time and timeout time
* rounded up to the next millisecond.
*/
int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -218,14 +218,10 @@ int Looper::pollInner(int timeoutMillis) {
// Adjust the timeout based on when the next message is due.
if (timeoutMillis != 0 && mNextMessageUptime != LLONG_MAX) {
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
if (mNextMessageUptime <= now) {
timeoutMillis = 0;
} else {
uint64_t delay = (mNextMessageUptime - now + 999999LL) / 1000000LL;
if (delay < INT_MAX
&& (timeoutMillis < 0 || int(delay) < timeoutMillis)) {
timeoutMillis = int(delay);
}
int messageTimeoutMillis = toMillisecondTimeoutDelay(now, mNextMessageUptime);
if (messageTimeoutMillis >= 0
&& (timeoutMillis < 0 || messageTimeoutMillis < timeoutMillis)) {
timeoutMillis = messageTimeoutMillis;
}
#if DEBUG_POLL_AND_WAKE
LOGD("%p ~ pollOnce - next message in %lldns, adjusted timeout: timeoutMillis=%d",
@ -444,12 +440,11 @@ int Looper::pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outDat
return result;
}
nsecs_t timeoutNanos = endTime - systemTime(SYSTEM_TIME_MONOTONIC);
if (timeoutNanos <= 0) {
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
timeoutMillis = toMillisecondTimeoutDelay(now, endTime);
if (timeoutMillis == 0) {
return ALOOPER_POLL_TIMEOUT;
}
timeoutMillis = int(nanoseconds_to_milliseconds(timeoutNanos + 999999LL));
}
}
}

View File

@ -26,6 +26,7 @@
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#ifdef HAVE_WIN32_THREADS
#include <windows.h>
@ -53,6 +54,23 @@ nsecs_t systemTime(int clock)
#endif
}
int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime)
{
int timeoutDelayMillis;
if (timeoutTime > referenceTime) {
uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime);
if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) {
timeoutDelayMillis = -1;
} else {
timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL;
}
} else {
timeoutDelayMillis = 0;
}
return timeoutDelayMillis;
}
/*
* ===========================================================================
* DurationTimer