sensor: minimize the number of epolls

Drain the event queue between ALooper_pollAll calls. ALooper_pollAll
makes an epoll to see if there is any I/O going on the underlying
resources and is relatively a costly operation. Try to consume the
queue first and block on pollAll again if necessary.

We should stop retrying if timeout occurs and return immediately.

Change-Id: I6e9f85df2dd275fb014f90f13de4f05d776d4b7d
Reviewed-on: https://go-review.googlesource.com/8590
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Burcu Dogan 2015-04-07 17:19:45 -04:00
parent e5a0a60135
commit 7858a6e1a7
2 changed files with 5 additions and 3 deletions

View File

@ -149,7 +149,6 @@ func readEvents(m *manager, e []Event) (n int, err error) {
timestamps := make([]C.int64_t, num)
vectors := make([]C.float, 3*num)
// TODO(jbd): add timeout.
n = int(C.android_readQueue(
m.m.looperId, m.m.queue,
C.int(num),

View File

@ -44,12 +44,15 @@ int android_readQueue(int looperId, ASensorEventQueue* q, int n, int32_t* types,
int events;
ASensorEvent event;
int i = 0;
// Block for 30 secs, timeout if nothing happens.
// Block for 30 secs at most, timeout if nothing happens.
// Try n times read from the event queue.
// If anytime timeout occurs, don't retry to read and immediately return.
// Consume the event queue entirely between polls.
while (i < n && (id = ALooper_pollAll(30*1000, NULL, &events, NULL)) >= 0) {
if (id != looperId) {
continue;
}
if (ASensorEventQueue_getEvents(q, &event, 1)) {
while (i < n && ASensorEventQueue_getEvents(q, &event, 1)) {
types[i] = event.type;
timestamps[i] = event.timestamp;
vectors[i*3] = event.vector.x;