summaryrefslogtreecommitdiffstats
path: root/modules/sensors/SensorEventQueue.h
blob: 11e1f4171289841a8384a2bd45f64d04aa14a75f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SENSOREVENTQUEUE_H_
#define SENSOREVENTQUEUE_H_

#include <hardware/sensors.h>
#include <pthread.h>

/*
 * Fixed-size circular queue, with an API developed around the sensor HAL poll() method.
 * Poll() takes a pointer to a buffer, which is written by poll() before it returns.
 * This class can provide a pointer to a spot in its internal buffer for poll() to
 * write to, instead of using an intermediate buffer and a memcpy.
 *
 * Thread safety:
 * Reading can be done safely after grabbing the mutex lock, while poll() writing in a separate
 * thread without a mutex lock. But there can only be one writer at a time.
 */
class SensorEventQueue {
    int mCapacity;
    int mStart; // start of readable region
    int mSize; // number of readable items
    sensors_event_t* mData;
    pthread_cond_t mSpaceAvailableCondition;

public:
    SensorEventQueue(int capacity);
    ~SensorEventQueue();

    // Returns length of region, between zero and min(capacity, requestedLength). If there is any
    // writable space, it will return a region of at least one. Because it must return
    // a pointer to a contiguous region, it may return smaller regions as we approach the end of
    // the data array.
    // Only call while holding the lock.
    // The region is not marked internally in any way. Subsequent calls may return overlapping
    // regions. This class expects there to be exactly one writer at a time.
    int getWritableRegion(int requestedLength, sensors_event_t** out);

    // After writing to the region returned by getWritableRegion(), call this to indicate how
    // many records were actually written.
    // This increases size() by count.
    // Only call while holding the lock.
    void markAsWritten(int count);

    // Gets the number of readable records.
    // Only call while holding the lock.
    int getSize();

    // Returns pointer to the first readable record, or NULL if size() is zero.
    // Only call this while holding the lock.
    sensors_event_t* peek();

    // This will decrease the size by one, freeing up the oldest readable event's slot for writing.
    // Only call while holding the lock.
    void dequeue();

    // Blocks until space is available. No-op if there is already space.
    // Returns true if it had to wait.
    bool waitForSpace(pthread_mutex_t* mutex);
};

#endif // SENSOREVENTQUEUE_H_