blob: 0d4d64a43720e923e93eb67ba70e78aef55b23e7 [file] [log] [blame]
Aaron Whyteab6ec382013-10-22 17:17:17 -07001/*
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 Whyteab6ec382013-10-22 17:17:17 -070017#include <pthread.h>
Aaron Whyteab6ec382013-10-22 17:17:17 -070018
Mark Salyzynd88dfe82017-04-11 08:56:09 -070019#include <algorithm>
20
21#include <log/log.h>
22
23#include <hardware/sensors.h>
Aaron Whyteab6ec382013-10-22 17:17:17 -070024#include "SensorEventQueue.h"
25
26SensorEventQueue::SensorEventQueue(int capacity) {
27 mCapacity = capacity;
Aaron Whyte92863c12013-10-28 17:18:06 -070028
Aaron Whyteab6ec382013-10-22 17:17:17 -070029 mStart = 0;
30 mSize = 0;
31 mData = new sensors_event_t[mCapacity];
Aaron Whyteab6ec382013-10-22 17:17:17 -070032 pthread_cond_init(&mSpaceAvailableCondition, NULL);
Aaron Whyteab6ec382013-10-22 17:17:17 -070033}
34
35SensorEventQueue::~SensorEventQueue() {
36 delete[] mData;
37 mData = NULL;
Aaron Whyteab6ec382013-10-22 17:17:17 -070038 pthread_cond_destroy(&mSpaceAvailableCondition);
Aaron Whyteab6ec382013-10-22 17:17:17 -070039}
40
41int SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** out) {
Aaron Whyte92863c12013-10-28 17:18:06 -070042 if (mSize == mCapacity || requestedLength <= 0) {
Aaron Whyteab6ec382013-10-22 17:17:17 -070043 *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
63void SensorEventQueue::markAsWritten(int count) {
64 mSize += count;
Aaron Whyteab6ec382013-10-22 17:17:17 -070065}
66
67int SensorEventQueue::getSize() {
68 return mSize;
69}
70
71sensors_event_t* SensorEventQueue::peek() {
Aaron Whyte92863c12013-10-28 17:18:06 -070072 if (mSize == 0) return NULL;
Aaron Whyteab6ec382013-10-22 17:17:17 -070073 return &mData[mStart];
74}
75
76void SensorEventQueue::dequeue() {
Aaron Whyte92863c12013-10-28 17:18:06 -070077 if (mSize == 0) return;
78 if (mSize == mCapacity) {
79 pthread_cond_broadcast(&mSpaceAvailableCondition);
80 }
Aaron Whyteab6ec382013-10-22 17:17:17 -070081 mSize--;
82 mStart = (mStart + 1) % mCapacity;
Aaron Whyte92863c12013-10-28 17:18:06 -070083}
84
Aaron Whytec69f3a72013-10-29 15:17:01 -070085// returns true if it waited, or false if it was a no-op.
86bool SensorEventQueue::waitForSpace(pthread_mutex_t* mutex) {
87 bool waited = false;
Aaron Whyte92863c12013-10-28 17:18:06 -070088 while (mSize == mCapacity) {
Aaron Whytec69f3a72013-10-29 15:17:01 -070089 waited = true;
Aaron Whyte92863c12013-10-28 17:18:06 -070090 pthread_cond_wait(&mSpaceAvailableCondition, mutex);
91 }
Aaron Whytec69f3a72013-10-29 15:17:01 -070092 return waited;
Aaron Whyteab6ec382013-10-22 17:17:17 -070093}