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 
17 #ifndef SENSOREVENTQUEUE_H_
18 #define SENSOREVENTQUEUE_H_
19 
20 #include <hardware/sensors.h>
21 #include <pthread.h>
22 
23 /*
24  * Fixed-size circular queue, with an API developed around the sensor HAL poll() method.
25  * Poll() takes a pointer to a buffer, which is written by poll() before it returns.
26  * This class can provide a pointer to a spot in its internal buffer for poll() to
27  * write to, instead of using an intermediate buffer and a memcpy.
28  *
29  * Thread safety:
30  * Reading can be done safely after grabbing the mutex lock, while poll() writing in a separate
31  * thread without a mutex lock. But there can only be one writer at a time.
32  */
33 class SensorEventQueue {
34     int mCapacity;
35     int mStart; // start of readable region
36     int mSize; // number of readable items
37     sensors_event_t* mData;
38     pthread_cond_t mSpaceAvailableCondition;
39 
40 public:
41     explicit SensorEventQueue(int capacity);
42     ~SensorEventQueue();
43 
44     // Returns length of region, between zero and min(capacity, requestedLength). If there is any
45     // writable space, it will return a region of at least one. Because it must return
46     // a pointer to a contiguous region, it may return smaller regions as we approach the end of
47     // the data array.
48     // Only call while holding the lock.
49     // The region is not marked internally in any way. Subsequent calls may return overlapping
50     // regions. This class expects there to be exactly one writer at a time.
51     int getWritableRegion(int requestedLength, sensors_event_t** out);
52 
53     // After writing to the region returned by getWritableRegion(), call this to indicate how
54     // many records were actually written.
55     // This increases size() by count.
56     // Only call while holding the lock.
57     void markAsWritten(int count);
58 
59     // Gets the number of readable records.
60     // Only call while holding the lock.
61     int getSize();
62 
63     // Returns pointer to the first readable record, or NULL if size() is zero.
64     // Only call this while holding the lock.
65     sensors_event_t* peek();
66 
67     // This will decrease the size by one, freeing up the oldest readable event's slot for writing.
68     // Only call while holding the lock.
69     void dequeue();
70 
71     // Blocks until space is available. No-op if there is already space.
72     // Returns true if it had to wait.
73     bool waitForSpace(pthread_mutex_t* mutex);
74 };
75 
76 #endif // SENSOREVENTQUEUE_H_
77