From 7858a6e1a76405cf0047f8be1264f79b50996547 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Tue, 7 Apr 2015 17:19:45 -0400 Subject: [PATCH] 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 --- sensor/sensor_android.go | 1 - sensor/sensors_android.c | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sensor/sensor_android.go b/sensor/sensor_android.go index 0ffd544..b7f4255 100644 --- a/sensor/sensor_android.go +++ b/sensor/sensor_android.go @@ -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), diff --git a/sensor/sensors_android.c b/sensor/sensors_android.c index 2e85326..2a6d597 100644 --- a/sensor/sensors_android.c +++ b/sensor/sensors_android.c @@ -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;