diff options
author | Aaron Whyte <awhyte@google.com> | 2013-10-28 17:18:06 -0700 |
---|---|---|
committer | Mike Lockwood <lockwood@google.com> | 2013-11-14 11:24:51 -0800 |
commit | 92863c14b7d36f74ec715b45ca6adc8bf95dc87c (patch) | |
tree | 2cf98925df54b3a6f064404cb3de0b4e36e05801 /modules/sensors/tests | |
parent | ab6ec384c456022f37a9c6183d3afbcefcb436a9 (diff) | |
download | hardware_libhardware-92863c14b7d36f74ec715b45ca6adc8bf95dc87c.zip hardware_libhardware-92863c14b7d36f74ec715b45ca6adc8bf95dc87c.tar.gz hardware_libhardware-92863c14b7d36f74ec715b45ca6adc8bf95dc87c.tar.bz2 |
MultiHal multithreaded polling
Change-Id: I3ebe380169eed1c8deeca2860d1788be6c14837e
Diffstat (limited to 'modules/sensors/tests')
-rw-r--r-- | modules/sensors/tests/SensorEventQueue_test.cpp | 88 |
1 files changed, 3 insertions, 85 deletions
diff --git a/modules/sensors/tests/SensorEventQueue_test.cpp b/modules/sensors/tests/SensorEventQueue_test.cpp index 3b89964..cbe4377 100644 --- a/modules/sensors/tests/SensorEventQueue_test.cpp +++ b/modules/sensors/tests/SensorEventQueue_test.cpp @@ -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"); |