blob: c139944dcdf08fbff026aa71718a68d33fb18006 [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
17#include <hardware/sensors.h>
18#include <algorithm>
19#include <pthread.h>
20
21#include <linux/input.h>
22#include <cutils/atomic.h>
23#include <cutils/log.h>
24
25#include "SensorEventQueue.h"
26
27SensorEventQueue::SensorEventQueue(int capacity) {
28 mCapacity = capacity;
29 mStart = 0;
30 mSize = 0;
31 mData = new sensors_event_t[mCapacity];
32 pthread_cond_init(&mDataAvailableCondition, NULL);
33 pthread_cond_init(&mSpaceAvailableCondition, NULL);
34 pthread_mutex_init(&mMutex, NULL);
35}
36
37SensorEventQueue::~SensorEventQueue() {
38 delete[] mData;
39 mData = NULL;
40 pthread_cond_destroy(&mDataAvailableCondition);
41 pthread_cond_destroy(&mSpaceAvailableCondition);
42 pthread_mutex_destroy(&mMutex);
43}
44
45void SensorEventQueue::lock() {
46 pthread_mutex_lock(&mMutex);
47}
48
49void SensorEventQueue::unlock() {
50 pthread_mutex_unlock(&mMutex);
51}
52
53void SensorEventQueue::waitForSpaceAndLock() {
54 lock();
55 while (mSize >= mCapacity) {
56 pthread_cond_wait(&mSpaceAvailableCondition, &mMutex);
57 }
58}
59
60void SensorEventQueue::waitForDataAndLock() {
61 lock();
62 while (mSize <= 0) {
63 pthread_cond_wait(&mDataAvailableCondition, &mMutex);
64 }
65}
66
67int SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** out) {
68 if (mSize >= mCapacity || requestedLength <= 0) {
69 *out = NULL;
70 return 0;
71 }
72 // Start writing after the last readable record.
73 int firstWritable = (mStart + mSize) % mCapacity;
74
75 int lastWritable = firstWritable + requestedLength - 1;
76
77 // Don't go past the end of the data array.
78 if (lastWritable > mCapacity - 1) {
79 lastWritable = mCapacity - 1;
80 }
81 // Don't go into the readable region.
82 if (firstWritable < mStart && lastWritable >= mStart) {
83 lastWritable = mStart - 1;
84 }
85 *out = &mData[firstWritable];
86 return lastWritable - firstWritable + 1;
87}
88
89void SensorEventQueue::markAsWritten(int count) {
90 mSize += count;
91 if (mSize) {
92 pthread_cond_broadcast(&mDataAvailableCondition);
93 }
94}
95
96int SensorEventQueue::getSize() {
97 return mSize;
98}
99
100sensors_event_t* SensorEventQueue::peek() {
101 if (mSize <= 0) return NULL;
102 return &mData[mStart];
103}
104
105void SensorEventQueue::dequeue() {
106 if (mSize <= 0) return;
107 mSize--;
108 mStart = (mStart + 1) % mCapacity;
109 pthread_cond_broadcast(&mSpaceAvailableCondition);
110}