MultiHal multithreaded polling
Change-Id: I3ebe380169eed1c8deeca2860d1788be6c14837e
This commit is contained in:
committed by
Mike Lockwood
parent
ab6ec384c4
commit
92863c14b7
@@ -2,6 +2,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <hardware/sensors.h>
|
||||
#include <pthread.h>
|
||||
#include <cutils/atomic.h>
|
||||
|
||||
#include "SensorEventQueue.cpp"
|
||||
|
||||
// Unit tests for the SensorEventQueue.
|
||||
@@ -78,93 +80,9 @@ bool testWrappingWriteSizeCounts() {
|
||||
return true;
|
||||
}
|
||||
|
||||
static const int TTOQ_EVENT_COUNT = 10000;
|
||||
|
||||
struct TaskContext {
|
||||
bool success;
|
||||
SensorEventQueue* queue;
|
||||
};
|
||||
|
||||
void* writerTask(void* ptr) {
|
||||
printf("writerTask starts\n");
|
||||
TaskContext* ctx = (TaskContext*)ptr;
|
||||
SensorEventQueue* queue = ctx->queue;
|
||||
int totalWrites = 0;
|
||||
sensors_event_t* buffer;
|
||||
while (totalWrites < TTOQ_EVENT_COUNT) {
|
||||
queue->waitForSpaceAndLock();
|
||||
int writableSize = queue->getWritableRegion(rand() % 10 + 1, &buffer);
|
||||
queue->unlock();
|
||||
for (int i = 0; i < writableSize; i++) {
|
||||
// serialize the events
|
||||
buffer[i].timestamp = totalWrites++;
|
||||
}
|
||||
queue->lock();
|
||||
queue->markAsWritten(writableSize);
|
||||
queue->unlock();
|
||||
}
|
||||
printf("writerTask ends normally\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* readerTask(void* ptr) {
|
||||
printf("readerTask starts\n");
|
||||
TaskContext* ctx = (TaskContext*)ptr;
|
||||
SensorEventQueue* queue = ctx->queue;
|
||||
int totalReads = 0;
|
||||
while (totalReads < TTOQ_EVENT_COUNT) {
|
||||
queue->waitForDataAndLock();
|
||||
int maxReads = rand() % 20 + 1;
|
||||
int reads = 0;
|
||||
while (queue->getSize() && reads < maxReads) {
|
||||
sensors_event_t* event = queue->peek();
|
||||
if (totalReads != event->timestamp) {
|
||||
printf("FAILURE: readerTask expected timestamp %d; actual was %d\n",
|
||||
totalReads, (int)(event->timestamp));
|
||||
ctx->success = false;
|
||||
return NULL;
|
||||
}
|
||||
queue->dequeue();
|
||||
totalReads++;
|
||||
reads++;
|
||||
}
|
||||
queue->unlock();
|
||||
}
|
||||
printf("readerTask ends normally\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// Create a short queue, and write and read a ton of data through it.
|
||||
// Write serial timestamps into the events, and expect to read them in the right order.
|
||||
bool testTwoThreadsOneQueue() {
|
||||
printf("TEST testTwoThreadsOneQueue\n");
|
||||
SensorEventQueue* queue = new SensorEventQueue(100);
|
||||
|
||||
TaskContext readerCtx;
|
||||
readerCtx.success = true;
|
||||
readerCtx.queue = queue;
|
||||
|
||||
TaskContext writerCtx;
|
||||
writerCtx.success = true;
|
||||
writerCtx.queue = queue;
|
||||
|
||||
pthread_t writer, reader;
|
||||
pthread_create(&reader, NULL, readerTask, &readerCtx);
|
||||
pthread_create(&writer, NULL, writerTask, &writerCtx);
|
||||
|
||||
pthread_join(writer, NULL);
|
||||
pthread_join(reader, NULL);
|
||||
|
||||
printf("testTwoThreadsOneQueue done\n");
|
||||
return readerCtx.success && writerCtx.success;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (testSimpleWriteSizeCounts() &&
|
||||
testWrappingWriteSizeCounts() &&
|
||||
testTwoThreadsOneQueue()) {
|
||||
testWrappingWriteSizeCounts()) {
|
||||
printf("ALL PASSED\n");
|
||||
} else {
|
||||
printf("SOMETHING FAILED\n");
|
||||
|
||||
Reference in New Issue
Block a user