summaryrefslogtreecommitdiffstats
path: root/modules/sensors/SensorEventQueue.cpp
diff options
context:
space:
mode:
authorAaron Whyte <awhyte@google.com>2013-10-22 17:17:17 -0700
committerMike Lockwood <lockwood@google.com>2013-11-14 11:24:15 -0800
commitab6ec384c456022f37a9c6183d3afbcefcb436a9 (patch)
tree2efe5a0ba86680b7a168e834cc05fa94d1f97d20 /modules/sensors/SensorEventQueue.cpp
parent079083281e2a9e479b09ae3fc4d2dc5c738e3d31 (diff)
downloadhardware_libhardware-ab6ec384c456022f37a9c6183d3afbcefcb436a9.zip
hardware_libhardware-ab6ec384c456022f37a9c6183d3afbcefcb436a9.tar.gz
hardware_libhardware-ab6ec384c456022f37a9c6183d3afbcefcb436a9.tar.bz2
Added a SensorEventQueue, a circular buffer meant for reading with one thread
and polling a subhal with another. The writing thread gets access to pointers in the internal buffer. This design avoids a memcpy on write when the multihal fetches subhal events using poll(). Unit-tests include multithreaded reading and writing lots of events, in random-sized chunks. This is not used by the multihal yet. That will be a different CL. Change-Id: I58418d69eebebeb96befb08ba3aed080f0f08551
Diffstat (limited to 'modules/sensors/SensorEventQueue.cpp')
-rw-r--r--modules/sensors/SensorEventQueue.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/modules/sensors/SensorEventQueue.cpp b/modules/sensors/SensorEventQueue.cpp
new file mode 100644
index 0000000..c139944
--- /dev/null
+++ b/modules/sensors/SensorEventQueue.cpp
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#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) {
+ *out = NULL;
+ return 0;
+ }
+ // Start writing after the last readable record.
+ int firstWritable = (mStart + mSize) % mCapacity;
+
+ int lastWritable = firstWritable + requestedLength - 1;
+
+ // Don't go past the end of the data array.
+ if (lastWritable > mCapacity - 1) {
+ lastWritable = mCapacity - 1;
+ }
+ // Don't go into the readable region.
+ if (firstWritable < mStart && lastWritable >= mStart) {
+ lastWritable = mStart - 1;
+ }
+ *out = &mData[firstWritable];
+ return lastWritable - firstWritable + 1;
+}
+
+void SensorEventQueue::markAsWritten(int count) {
+ mSize += count;
+ if (mSize) {
+ pthread_cond_broadcast(&mDataAvailableCondition);
+ }
+}
+
+int SensorEventQueue::getSize() {
+ return mSize;
+}
+
+sensors_event_t* SensorEventQueue::peek() {
+ if (mSize <= 0) return NULL;
+ return &mData[mStart];
+}
+
+void SensorEventQueue::dequeue() {
+ if (mSize <= 0) return;
+ mSize--;
+ mStart = (mStart + 1) % mCapacity;
+ pthread_cond_broadcast(&mSpaceAvailableCondition);
+}