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) :
30 mVm(vm), mRunning(true), mThread(&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 if (!mRunning) {
54 break;
55 }
56
57 if (mTaskQueue.empty()) {
58 mCondition.wait(lock);
59 // The conditional variable is signalled when either a new task
60 // is enqueued or we are requested to stop. If we wake up
61 // spuriously, the task queue must be empty so go back to sleep.
62 if (!mRunning) {
63 break;
64 } else if (mTaskQueue.empty()) {
65 LOG(DEBUG) << "No pending tasks; continue.";
66 continue;
67 }
68 }
69
70 task = mTaskQueue.front();
71 mTaskQueue.pop();
72 }
73
74 // Executes the task and check the exception
75 task(env);
76 if (env->ExceptionCheck()) {
77 LOG(ERROR) << "Exception happens while handling a task:";
78 env->ExceptionDescribe();
79 env->ExceptionClear();
80 }
81 }
82
83 auto res = mVm->DetachCurrentThread();
84 if (res != JNI_OK) {
85 LOG(WARNING) << "Failed to be detached from the VM.";
86 }
87
88 if (!mTaskQueue.empty()) {
89 LOG(WARNING) << mTaskQueue.size() << " tasks are ignored.";
90 }
91
92 LOG(DEBUG) << "Exiting a callback handler thread.";
93 }
94
enqueue(const Task & task)95 void EvsCallbackThread::enqueue(const Task& task) {
96 std::lock_guard<std::mutex> lock(mLock);
97 if (!mRunning) {
98 LOG(WARNING) << "A callback handler thread is not running.";
99 return;
100 }
101
102 mTaskQueue.push(task);
103 mCondition.notify_one();
104 }
105
stop()106 void EvsCallbackThread::stop() {
107 {
108 std::lock_guard<std::mutex> lock(mLock);
109 if (!mRunning) {
110 // Nothing to do if the handler thread is not running.
111 return;
112 }
113
114 mRunning = false;
115 mCondition.notify_all();
116 }
117
118 if (mThread.get_id() == std::this_thread::get_id()) {
119 // Should not join by myself
120 mThread.detach();
121 } else if (mThread.joinable()) {
122 mThread.join();
123 }
124 }
125
126 } // namespace android::automotive::evs
127