1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "GeneratorHub"
18
19 #include <log/log.h>
20
21 #include "GeneratorHub.h"
22
23 namespace android {
24 namespace hardware {
25 namespace automotive {
26 namespace vehicle {
27 namespace V2_0 {
28
29 namespace impl {
30
GeneratorHub(const OnHalEvent & onHalEvent)31 GeneratorHub::GeneratorHub(const OnHalEvent& onHalEvent)
32 : mOnHalEvent(onHalEvent), mThread(&GeneratorHub::run, this) {}
33
registerGenerator(int32_t cookie,FakeValueGeneratorPtr generator)34 void GeneratorHub::registerGenerator(int32_t cookie, FakeValueGeneratorPtr generator) {
35 {
36 std::lock_guard<std::mutex> g(mLock);
37 // Register only if the generator can produce event
38 if (generator->hasNext()) {
39 // Push the next event if it is a new generator
40 if (mGenerators.find(cookie) == mGenerators.end()) {
41 ALOGI("%s: Registering new generator, cookie: %d", __func__, cookie);
42 mEventQueue.push({cookie, generator->nextEvent()});
43 }
44 mGenerators[cookie] = std::move(generator);
45 ALOGI("%s: Registered generator, cookie: %d", __func__, cookie);
46 }
47 }
48 mCond.notify_one();
49 }
50
unregisterGenerator(int32_t cookie)51 void GeneratorHub::unregisterGenerator(int32_t cookie) {
52 {
53 std::lock_guard<std::mutex> g(mLock);
54 mGenerators.erase(cookie);
55 }
56 mCond.notify_one();
57 ALOGI("%s: Unregistered generator, cookie: %d", __func__, cookie);
58 }
59
run()60 void GeneratorHub::run() {
61 while (true) {
62 std::unique_lock<std::mutex> g(mLock);
63 // Pop events whose generator does not exist (may be already unregistered)
64 while (!mEventQueue.empty()
65 && mGenerators.find(mEventQueue.top().cookie) == mGenerators.end()) {
66 mEventQueue.pop();
67 }
68 // Wait until event queue is not empty
69 mCond.wait(g, [this] { return !mEventQueue.empty(); });
70
71 const VhalEvent& curEvent = mEventQueue.top();
72
73 TimePoint eventTime(Nanos(curEvent.val.timestamp));
74 // Wait until the soonest event happen
75 if (mCond.wait_until(g, eventTime) != std::cv_status::timeout) {
76 // It is possible that a new generator is registered and produced a sooner event, or current
77 // generator is unregistered, in this case the thread will re-evaluate the soonest event
78 ALOGI("Something happened while waiting");
79 continue;
80 }
81 // Now it's time to handle current event.
82 mOnHalEvent(curEvent.val);
83 // Update queue by popping current event and producing next event from the same generator
84 int32_t cookie = curEvent.cookie;
85 mEventQueue.pop();
86 if (hasNext(cookie)) {
87 mEventQueue.push({cookie, mGenerators[cookie]->nextEvent()});
88 } else {
89 ALOGI("%s: Generator ended, unregister it, cookie: %d", __func__, cookie);
90 mGenerators.erase(cookie);
91 }
92 }
93 }
94
hasNext(int32_t cookie)95 bool GeneratorHub::hasNext(int32_t cookie) {
96 return mGenerators.find(cookie) != mGenerators.end() && mGenerators[cookie]->hasNext();
97 }
98
99 } // namespace impl
100
101 } // namespace V2_0
102 } // namespace vehicle
103 } // namespace automotive
104 } // namespace hardware
105 } // namespace android
106