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:
parent
bf1596d2ef
commit
dad00d93e6
@ -60,9 +60,8 @@ type inOut struct {
|
|||||||
|
|
||||||
// manager is the Android-specific implementation of Manager.
|
// manager is the Android-specific implementation of Manager.
|
||||||
type manager struct {
|
type manager struct {
|
||||||
m *C.android_SensorManager
|
m *C.android_SensorManager
|
||||||
inout chan inOut
|
inout chan inOut
|
||||||
minDelay int64 // read timeout in milliseconds
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize inits the manager and creates a goroutine to proxy the CGO calls.
|
// 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 {
|
func (m *manager) enable(t Type, delay time.Duration) error {
|
||||||
var err error
|
var err error
|
||||||
if d := delay.Nanoseconds() * 1000 * 1000; m.minDelay == 0 || d < m.minDelay {
|
|
||||||
m.minDelay = d
|
|
||||||
}
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
m.inout <- inOut{
|
m.inout <- inOut{
|
||||||
in: enableSignal{t: t, delay: delay, err: &err},
|
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)
|
types := make([]C.int32_t, num)
|
||||||
timestamps := make([]C.int64_t, num)
|
timestamps := make([]C.int64_t, num)
|
||||||
vectors := make([]C.float, 3*num)
|
vectors := make([]C.float, 3*num)
|
||||||
|
|
||||||
|
// TODO(jbd): add timeout.
|
||||||
n = int(C.android_readQueue(
|
n = int(C.android_readQueue(
|
||||||
m.m.looperId,
|
m.m.looperId, m.m.queue,
|
||||||
m.m.queue,
|
|
||||||
C.int(2*m.minDelay), // wait twice as much than the min-expected delay
|
|
||||||
C.int(num),
|
C.int(num),
|
||||||
(*C.int32_t)(unsafe.Pointer(&types[0])),
|
(*C.int32_t)(unsafe.Pointer(&types[0])),
|
||||||
(*C.int64_t)(unsafe.Pointer(×tamps[0])),
|
(*C.int64_t)(unsafe.Pointer(×tamps[0])),
|
||||||
|
@ -39,38 +39,27 @@ void android_disableSensor(ASensorEventQueue* q, int s) {
|
|||||||
ASensorEventQueue_disableSensor(q, sensor);
|
ASensorEventQueue_disableSensor(q, sensor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(jbd): Introduce a struct of type, timestamp and vectors.
|
int android_readQueue(int looperId, ASensorEventQueue* q, int n, int32_t* types, int64_t* timestamps, float* 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 id;
|
int id;
|
||||||
int events;
|
int events;
|
||||||
ASensorEvent event;
|
ASensorEvent event;
|
||||||
int i = 0, num = 0;
|
// TODO(jbd): Timeout if pollAll blocks longer than it should.
|
||||||
// TODO(jbd): Make sure we don't have to consume the sensor queue entirely.
|
int i = 0;
|
||||||
while (i < n && (id = ALooper_pollAll(timeoutMillis, NULL, &events, NULL)) >= 0) {
|
// Block forever until new events are on the queue.
|
||||||
|
while (i < n && (id = ALooper_pollAll(-1, NULL, &events, NULL)) >= 0) {
|
||||||
if (id != looperId) {
|
if (id != looperId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (ASensorEventQueue_getEvents(q, &event, 1)) {
|
if (ASensorEventQueue_getEvents(q, &event, 1)) {
|
||||||
types[num] = event.type;
|
types[i] = event.type;
|
||||||
timestamps[num] = event.timestamp;
|
timestamps[i] = event.timestamp;
|
||||||
vectors[num*3] = event.vector.x;
|
vectors[i*3] = event.vector.x;
|
||||||
vectors[num*3+1] = event.vector.y;
|
vectors[i*3+1] = event.vector.y;
|
||||||
vectors[num*3+2] = event.vector.z;
|
vectors[i*3+2] = event.vector.z;
|
||||||
num++;
|
i++;
|
||||||
}
|
}
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
return num;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void android_destroyManager(android_SensorManager* m) {
|
void android_destroyManager(android_SensorManager* m) {
|
||||||
|
@ -15,12 +15,6 @@ void android_createManager(int looperId, android_SensorManager* dst);
|
|||||||
void android_destroyManager(android_SensorManager* m);
|
void android_destroyManager(android_SensorManager* m);
|
||||||
int android_enableSensor(ASensorEventQueue*, int, int32_t);
|
int android_enableSensor(ASensorEventQueue*, int, int32_t);
|
||||||
void android_disableSensor(ASensorEventQueue*, int);
|
void android_disableSensor(ASensorEventQueue*, int);
|
||||||
int android_readQueue(
|
int android_readQueue(int looperId, ASensorEventQueue* q, int n, int32_t* types, int64_t* timestamps, float* vectors);
|
||||||
int looperId, ASensorEventQueue* q,
|
|
||||||
int timeoutMillis, int n,
|
|
||||||
int32_t* types,
|
|
||||||
int64_t* timestamps,
|
|
||||||
float* vectors
|
|
||||||
);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user