2
0
mirror of synced 2025-02-23 14:58:12 +00:00

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 <jbd@google.com>
This commit is contained in:
Burcu Dogan 2015-04-07 15:56:44 -04:00
parent bf1596d2ef
commit dad00d93e6
3 changed files with 18 additions and 39 deletions

View File

@ -62,7 +62,6 @@ type inOut struct {
type manager struct {
m *C.android_SensorManager
inout chan inOut
minDelay int64 // read timeout in milliseconds
}
// 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(&timestamps[0])),

View File

@ -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++;
}
return num;
}
return i;
}
void android_destroyManager(android_SensorManager* m) {

View File

@ -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