summaryrefslogtreecommitdiff
path: root/sensorservice/libsensorndkbridge/ALooper.cpp
blob: a4cb8cfec1d11de56b9338a24429c854e7a7c4ff (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2017 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.
 */

#include "ALooper.h"

#include "ASensorEventQueue.h"

#define LOG_TAG "libsensorndkbridge"
#include <android/looper.h>
#include <android-base/logging.h>

using android::Mutex;

ALooper::ALooper()
    : mAwoken(false) {
}

void ALooper::signalSensorEvents(const std::shared_ptr<ASensorEventQueue>& queue) {
    Mutex::Autolock autoLock(mLock);
    mReadyQueues.push_back(std::weak_ptr<ASensorEventQueue>(queue));
    mCondition.signal();
}

void ALooper::wake() {
    Mutex::Autolock autoLock(mLock);
    mAwoken = true;
    mCondition.signal();
}

int ALooper::pollOnce(
        int timeoutMillis, int *outFd, int *outEvents, void **outData) {
    if (outFd) { *outFd = 0; }
    if (outEvents) { *outEvents = 0; }
    if (outData) { *outData = NULL; }

    int64_t waitUntilNs;
    if (timeoutMillis < 0) {
        waitUntilNs = -1;
    } else {
        waitUntilNs = systemTime(SYSTEM_TIME_MONOTONIC) + timeoutMillis * 1000000LL;
    }

    Mutex::Autolock autoLock(mLock);
    int64_t nowNs;
    while ((timeoutMillis < 0
                || (nowNs = systemTime(SYSTEM_TIME_MONOTONIC)) < waitUntilNs)
            && mReadyQueues.empty()
            && !mAwoken) {
        if (timeoutMillis < 0) {
            mCondition.wait(mLock);
        } else {
            mCondition.waitRelative(mLock, waitUntilNs - nowNs);
        }
    }

    int result = ALOOPER_POLL_TIMEOUT;

    if (!mReadyQueues.empty()) {
        result = ALOOPER_POLL_CALLBACK;

        for (auto& queue : mReadyQueues) {
            std::shared_ptr<ASensorEventQueue> promotedQueue = queue.lock();
            if (promotedQueue != nullptr) {
                promotedQueue->dispatchCallback();
            }
        }

        mReadyQueues.clear();
    } else if (mAwoken) {
        result = ALOOPER_POLL_WAKE;
        mAwoken = false;
    }

    LOG(VERBOSE) << "pollOnce returning " << result;

    return result;
}

void ALooper::invalidateSensorQueue(const std::shared_ptr<ASensorEventQueue>& queue) {
    Mutex::Autolock autoLock(mLock);

    mReadyQueues.erase(std::remove_if(mReadyQueues.begin(), mReadyQueues.end(),
                                      [&](const std::weak_ptr<ASensorEventQueue>& ptr) {
                                          std::shared_ptr<ASensorEventQueue> in = ptr.lock();
                                          return (in == nullptr) || (in == queue);
                                      }),
                       mReadyQueues.end());
}