• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2017 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 #ifndef _ANDROID_NATIVE_CALLBACK_THREAD_H
18 #define _ANDROID_NATIVE_CALLBACK_THREAD_H
19 
20 #include <android-base/macros.h>
21 #include <functional>
22 #include <jni.h>
23 #include <queue>
24 #include <thread>
25 
26 namespace android {
27 
28 class NativeCallbackThread {
29     typedef std::function<void(JNIEnv*)> Task;
30 
31     JavaVM *mvm;
32     std::queue<Task> mQueue;
33 
34     std::mutex mQueueMutex;
35     std::condition_variable mQueueCond;
36     std::atomic<bool> mExiting;
37     std::thread mThread;
38 
39     void threadLoop();
40 
41     DISALLOW_COPY_AND_ASSIGN(NativeCallbackThread);
42 
43 public:
44     explicit NativeCallbackThread(JavaVM *vm);
45     virtual ~NativeCallbackThread();
46 
47     void enqueue(const Task &task);
48     void stop();
49 };
50 
51 } // namespace android
52 
53 #endif // _ANDROID_NATIVE_CALLBACK_THREAD_H
54