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