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/SensorEventQueue.cpp | |
parent | ab6ec384c456022f37a9c6183d3afbcefcb436a9 (diff) |
MultiHal multithreaded polling
Change-Id: I3ebe380169eed1c8deeca2860d1788be6c14837e
Diffstat (limited to 'modules/sensors/SensorEventQueue.cpp')
-rw-r--r-- | modules/sensors/SensorEventQueue.cpp | 48 |
1 files changed, 13 insertions, 35 deletions
diff --git a/modules/sensors/SensorEventQueue.cpp b/modules/sensors/SensorEventQueue.cpp index c139944d..00013ded 100644 --- a/modules/sensors/SensorEventQueue.cpp +++ b/modules/sensors/SensorEventQueue.cpp @@ -17,55 +17,28 @@ #include <hardware/sensors.h> #include <algorithm> #include <pthread.h> - #include <linux/input.h> -#include <cutils/atomic.h> #include <cutils/log.h> #include "SensorEventQueue.h" SensorEventQueue::SensorEventQueue(int capacity) { mCapacity = capacity; + mStart = 0; mSize = 0; mData = new sensors_event_t[mCapacity]; - pthread_cond_init(&mDataAvailableCondition, NULL); pthread_cond_init(&mSpaceAvailableCondition, NULL); - pthread_mutex_init(&mMutex, NULL); } SensorEventQueue::~SensorEventQueue() { delete[] mData; mData = NULL; - pthread_cond_destroy(&mDataAvailableCondition); pthread_cond_destroy(&mSpaceAvailableCondition); - pthread_mutex_destroy(&mMutex); -} - -void SensorEventQueue::lock() { - pthread_mutex_lock(&mMutex); -} - -void SensorEventQueue::unlock() { - pthread_mutex_unlock(&mMutex); -} - -void SensorEventQueue::waitForSpaceAndLock() { - lock(); - while (mSize >= mCapacity) { - pthread_cond_wait(&mSpaceAvailableCondition, &mMutex); - } -} - -void SensorEventQueue::waitForDataAndLock() { - lock(); - while (mSize <= 0) { - pthread_cond_wait(&mDataAvailableCondition, &mMutex); - } } int SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** out) { - if (mSize >= mCapacity || requestedLength <= 0) { + if (mSize == mCapacity || requestedLength <= 0) { *out = NULL; return 0; } @@ -88,9 +61,6 @@ int SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** o void SensorEventQueue::markAsWritten(int count) { mSize += count; - if (mSize) { - pthread_cond_broadcast(&mDataAvailableCondition); - } } int SensorEventQueue::getSize() { @@ -98,13 +68,21 @@ int SensorEventQueue::getSize() { } sensors_event_t* SensorEventQueue::peek() { - if (mSize <= 0) return NULL; + if (mSize == 0) return NULL; return &mData[mStart]; } void SensorEventQueue::dequeue() { - if (mSize <= 0) return; + if (mSize == 0) return; + if (mSize == mCapacity) { + pthread_cond_broadcast(&mSpaceAvailableCondition); + } mSize--; mStart = (mStart + 1) % mCapacity; - pthread_cond_broadcast(&mSpaceAvailableCondition); +} + +void SensorEventQueue::waitForSpace(pthread_mutex_t* mutex) { + while (mSize == mCapacity) { + pthread_cond_wait(&mSpaceAvailableCondition, mutex); + } } |