From dad00d93e606dd47357aa1f2c0715abe129b6c84 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Tue, 7 Apr 2015 15:56:44 -0400 Subject: [PATCH] Revert "mobile/sensor: block until timeout occurs" If the looper is waking up, the current timeout limit is exceeded. There are no external APIs to query if a looper is trying to wake up or blocking to wait for the event data. Therefore, I'm this change and will replace the timeout with a significantly high number that could only be exceeded if sensor fails with a fatal problem. The goal is not to wait on ALooper_pollAll forever. This reverts commit 1575a42f33e3354db87760bcc8c9d951e35347dd. Change-Id: I861d81f2550c4435247a0b1d4ce141e94d1e3ec9 Reviewed-on: https://go-review.googlesource.com/8565 Reviewed-by: Burcu Dogan --- sensor/sensor_android.go | 14 +++++--------- sensor/sensors_android.c | 35 ++++++++++++----------------------- sensor/sensors_android.h | 8 +------- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/sensor/sensor_android.go b/sensor/sensor_android.go index b33b446..0ffd544 100644 --- a/sensor/sensor_android.go +++ b/sensor/sensor_android.go @@ -60,9 +60,8 @@ type inOut struct { // manager is the Android-specific implementation of Manager. type manager struct { - m *C.android_SensorManager - inout chan inOut - minDelay int64 // read timeout in milliseconds + m *C.android_SensorManager + inout chan inOut } // initialize inits the manager and creates a goroutine to proxy the CGO calls. @@ -115,9 +114,6 @@ func (m *manager) initialize() { func (m *manager) enable(t Type, delay time.Duration) error { var err error - if d := delay.Nanoseconds() * 1000 * 1000; m.minDelay == 0 || d < m.minDelay { - m.minDelay = d - } done := make(chan struct{}) m.inout <- inOut{ in: enableSignal{t: t, delay: delay, err: &err}, @@ -152,10 +148,10 @@ func readEvents(m *manager, e []Event) (n int, err error) { types := make([]C.int32_t, num) 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(2*m.minDelay), // wait twice as much than the min-expected delay + m.m.looperId, m.m.queue, C.int(num), (*C.int32_t)(unsafe.Pointer(&types[0])), (*C.int64_t)(unsafe.Pointer(×tamps[0])), diff --git a/sensor/sensors_android.c b/sensor/sensors_android.c index e20ea8f..2e1dd51 100644 --- a/sensor/sensors_android.c +++ b/sensor/sensors_android.c @@ -39,38 +39,27 @@ void android_disableSensor(ASensorEventQueue* q, int s) { ASensorEventQueue_disableSensor(q, sensor); } -// TODO(jbd): Introduce a struct of type, timestamp and vectors. -// CGO doesn't support union types, therefore we will not be able -// to reuse ASensorEvent. -int android_readQueue( - int looperId, - ASensorEventQueue* q, - int timeoutMillis, - int n, - int32_t* types, - int64_t* timestamps, - float* vectors -) { +int android_readQueue(int looperId, ASensorEventQueue* q, int n, int32_t* types, int64_t* timestamps, float* vectors) { int id; int events; ASensorEvent event; - int i = 0, num = 0; - // TODO(jbd): Make sure we don't have to consume the sensor queue entirely. - while (i < n && (id = ALooper_pollAll(timeoutMillis, NULL, &events, NULL)) >= 0) { + // TODO(jbd): Timeout if pollAll blocks longer than it should. + int i = 0; + // Block forever until new events are on the queue. + while (i < n && (id = ALooper_pollAll(-1, NULL, &events, NULL)) >= 0) { if (id != looperId) { continue; } if (ASensorEventQueue_getEvents(q, &event, 1)) { - types[num] = event.type; - timestamps[num] = event.timestamp; - vectors[num*3] = event.vector.x; - vectors[num*3+1] = event.vector.y; - vectors[num*3+2] = event.vector.z; - num++; + types[i] = event.type; + timestamps[i] = event.timestamp; + vectors[i*3] = event.vector.x; + vectors[i*3+1] = event.vector.y; + vectors[i*3+2] = event.vector.z; + i++; } - i++; } - return num; + return i; } void android_destroyManager(android_SensorManager* m) { diff --git a/sensor/sensors_android.h b/sensor/sensors_android.h index 2393ad9..6f7d820 100644 --- a/sensor/sensors_android.h +++ b/sensor/sensors_android.h @@ -15,12 +15,6 @@ void android_createManager(int looperId, android_SensorManager* dst); void android_destroyManager(android_SensorManager* m); int android_enableSensor(ASensorEventQueue*, int, int32_t); void android_disableSensor(ASensorEventQueue*, int); -int android_readQueue( - int looperId, ASensorEventQueue* q, - int timeoutMillis, int n, - int32_t* types, - int64_t* timestamps, - float* vectors -); +int android_readQueue(int looperId, ASensorEventQueue* q, int n, int32_t* types, int64_t* timestamps, float* vectors); #endif