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 #ifndef ANDROID_CARSERVICE_EVSCALLBACKTHREAD_H 17 #define ANDROID_CARSERVICE_EVSCALLBACKTHREAD_H 18 19 #include <android-base/macros.h> 20 #include <android-base/thread_annotations.h> 21 22 #include <jni.h> 23 24 #include <queue> 25 #include <thread> 26 27 namespace android { 28 namespace automotive { 29 namespace evs { 30 31 /* 32 * This class defines the thread that handles callbacks from the native Extended 33 * View System service. 34 */ 35 class EvsCallbackThread { 36 using Task = std::function<void(JNIEnv*)>; 37 38 JavaVM* mVm; 39 40 std::atomic<bool> mRunning; 41 std::thread mThread; 42 std::mutex mLock; 43 std::condition_variable mCondition; 44 std::queue<Task> mTaskQueue GUARDED_BY(mLock); 45 46 // Main loop to handle enqueued tasks continuously 47 void threadLoop(); 48 49 DISALLOW_COPY_AND_ASSIGN(EvsCallbackThread); 50 51 public: 52 explicit EvsCallbackThread(JavaVM* vm); 53 virtual ~EvsCallbackThread(); 54 55 // Adds a new task to the queue 56 void enqueue(const Task& task) ACQUIRE(mLock); 57 58 // Stops a task handler thread 59 void stop(); 60 }; 61 62 } // namespace evs 63 } // namespace automotive 64 } // namespace android 65 66 #endif // ANDROID_CARSERVICE_EVSCALLBACKTHREAD_H 67