1ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte/* 2ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * Copyright (C) 2013 The Android Open Source Project 3ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * 4ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * Licensed under the Apache License, Version 2.0 (the "License"); 5ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * you may not use this file except in compliance with the License. 6ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * You may obtain a copy of the License at 7ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * 8ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * http://www.apache.org/licenses/LICENSE-2.0 9ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * 10ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * Unless required by applicable law or agreed to in writing, software 11ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * distributed under the License is distributed on an "AS IS" BASIS, 12ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * See the License for the specific language governing permissions and 14ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte * limitations under the License. 15ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte */ 16ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 17ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte#include <hardware/sensors.h> 18ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte#include <algorithm> 19ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte#include <pthread.h> 20ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte#include <cutils/log.h> 21ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 22ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte#include "SensorEventQueue.h" 23ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 24ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron WhyteSensorEventQueue::SensorEventQueue(int capacity) { 25ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mCapacity = capacity; 2692863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte 27ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mStart = 0; 28ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mSize = 0; 29ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mData = new sensors_event_t[mCapacity]; 30ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte pthread_cond_init(&mSpaceAvailableCondition, NULL); 31ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 32ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 33ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron WhyteSensorEventQueue::~SensorEventQueue() { 34ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte delete[] mData; 35ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mData = NULL; 36ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte pthread_cond_destroy(&mSpaceAvailableCondition); 37ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 38ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 39ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyteint SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** out) { 4092863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte if (mSize == mCapacity || requestedLength <= 0) { 41ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte *out = NULL; 42ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte return 0; 43ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte } 44ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte // Start writing after the last readable record. 45ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte int firstWritable = (mStart + mSize) % mCapacity; 46ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 47ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte int lastWritable = firstWritable + requestedLength - 1; 48ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 49ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte // Don't go past the end of the data array. 50ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte if (lastWritable > mCapacity - 1) { 51ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte lastWritable = mCapacity - 1; 52ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte } 53ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte // Don't go into the readable region. 54ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte if (firstWritable < mStart && lastWritable >= mStart) { 55ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte lastWritable = mStart - 1; 56ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte } 57ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte *out = &mData[firstWritable]; 58ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte return lastWritable - firstWritable + 1; 59ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 60ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 61ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whytevoid SensorEventQueue::markAsWritten(int count) { 62ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mSize += count; 63ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 64ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 65ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyteint SensorEventQueue::getSize() { 66ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte return mSize; 67ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 68ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 69ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whytesensors_event_t* SensorEventQueue::peek() { 7092863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte if (mSize == 0) return NULL; 71ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte return &mData[mStart]; 72ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 73ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte 74ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whytevoid SensorEventQueue::dequeue() { 7592863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte if (mSize == 0) return; 7692863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte if (mSize == mCapacity) { 7792863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte pthread_cond_broadcast(&mSpaceAvailableCondition); 7892863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte } 79ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mSize--; 80ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte mStart = (mStart + 1) % mCapacity; 8192863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte} 8292863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte 83c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05Aaron Whyte// returns true if it waited, or false if it was a no-op. 84c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05Aaron Whytebool SensorEventQueue::waitForSpace(pthread_mutex_t* mutex) { 85c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05Aaron Whyte bool waited = false; 8692863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte while (mSize == mCapacity) { 87c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05Aaron Whyte waited = true; 8892863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte pthread_cond_wait(&mSpaceAvailableCondition, mutex); 8992863c14b7d36f74ec715b45ca6adc8bf95dc87cAaron Whyte } 90c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05Aaron Whyte return waited; 91ab6ec384c456022f37a9c6183d3afbcefcb436a9Aaron Whyte} 92