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