1 /**
2 * Copyright (C) 2021 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 #include "EvsCallbackThread.h"
18
19 #include <android-base/logging.h>
20
21 namespace {
22
23 constexpr const char kCallbackThreadName[] = "EvsCallbackThread";
24
25 } // namespace
26
27 namespace android::automotive::evs {
28
EvsCallbackThread(JavaVM * vm)29 EvsCallbackThread::EvsCallbackThread(JavaVM* vm) : mVm(vm), mRunning(true) {
30 mThread = std::thread(&EvsCallbackThread::threadLoop, this);
31 LOG(DEBUG) << "Started the native callback handler thread = " << this;
32 }
33
~EvsCallbackThread()34 EvsCallbackThread::~EvsCallbackThread() {
35 // Stops the loop
36 stop();
37 }
38
threadLoop()39 void EvsCallbackThread::threadLoop() {
40 // Attaches the current thread to the Java VM.
41 JNIEnv* env = nullptr;
42 JavaVMAttachArgs args = {JNI_VERSION_1_4, kCallbackThreadName, nullptr};
43 if (mVm->AttachCurrentThread(&env, &args) != JNI_OK || env == nullptr) {
44 LOG(ERROR) << "Failed to be attached to the VM.";
45 mRunning = false;
46 return;
47 }
48
49 while (true) {
50 Task task;
51 {
52 std::unique_lock<std::mutex> lock(mLock);
53 android::base::ScopedLockAssertion lock_assertion(mLock);
54 if (!mRunning) {
55 break;
56 }
57
58 if (mTaskQueue.empty()) {
59 mCondition.wait(lock);
60 // The conditional variable is signalled when either a new task
61 // is enqueued or we are requested to stop. If we wake up
62 // spuriously, the task queue must be empty so go back to sleep.
63 if (!mRunning) {
64 break;
65 } else if (mTaskQueue.empty()) {
66 LOG(DEBUG) << "No pending tasks; continue.";
67 continue;
68 }
69 }
70
71 task = mTaskQueue.front();
72 mTaskQueue.pop();
73 }
74
75 // Executes the task and check the exception
76 task(env);
77 if (env->ExceptionCheck()) {
78 LOG(ERROR) << "Exception happens while handling a task:";
79 env->ExceptionDescribe();
80 env->ExceptionClear();
81 }
82 }
83
84 {
85 std::lock_guard lock(mLock);
86 auto res = mVm->DetachCurrentThread();
87 if (res != JNI_OK) {
88 LOG(WARNING) << "Failed to be detached from the VM.";
89 }
90
91 if (!mTaskQueue.empty()) {
92 LOG(WARNING) << mTaskQueue.size() << " tasks are ignored.";
93 }
94 }
95
96 LOG(DEBUG) << "Exiting a callback handler thread.";
97 }
98
enqueue(const Task & task)99 void EvsCallbackThread::enqueue(const Task& task) {
100 std::lock_guard<std::mutex> lock(mLock);
101 if (!mRunning) {
102 LOG(WARNING) << "A callback handler thread is not running.";
103 return;
104 }
105
106 mTaskQueue.push(task);
107 mCondition.notify_one();
108 }
109
stop()110 void EvsCallbackThread::stop() {
111 {
112 std::lock_guard<std::mutex> lock(mLock);
113 if (!mRunning) {
114 // Nothing to do if the handler thread is not running.
115 return;
116 }
117
118 mRunning = false;
119 mCondition.notify_all();
120 }
121
122 if (mThread.get_id() == std::this_thread::get_id()) {
123 // Should not join by myself
124 mThread.detach();
125 } else if (mThread.joinable()) {
126 mThread.join();
127 }
128 }
129
130 } // namespace android::automotive::evs
131