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