Debian Codegraph @

platform/android/thread_jandroid.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* thread_jandroid.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "thread_jandroid.h"
32
33 #include "core/os/thread.h"
34
35 #include <android/log.h>
36
37 static JavaVM *java_vm = nullptr;
38 static thread_local JNIEnv *env = nullptr;
39
40 // The logic here need to improve, init_thread/term_tread are designed to work with Thread::callback
41 // Calling init_thread from setup_android_thread and get_jni_env to setup an env we're keeping and not detaching
42 // could cause issues on app termination.
43 //
44 // We should be making sure that any thread started calls a nice cleanup function when it's done,
45 // especially now that we use many more threads.
46
47 static void init_thread() {
48 if (env) {
49 // thread never detached! just keep using...
50 return;
51 }
52
53 java_vm->AttachCurrentThread(&env, nullptr);
54 }
55
56 static void term_thread() {
57 java_vm->DetachCurrentThread();
58
59 // this is no longer valid, must called init_thread to re-establish
60 env = nullptr;
61 }
62
63 void init_thread_jandroid(JavaVM *p_jvm, JNIEnv *p_env) {
64 java_vm = p_jvm;
65 env = p_env;
66 Thread::_set_platform_functions({ .init = init_thread, .term = &term_thread });
67 }
68
69 void setup_android_thread() {
70 if (!env) {
71 // !BAS! see remarks above
72 init_thread();
73 }
74 }
75
76 JNIEnv *get_jni_env() {
77 if (!env) {
78 // !BAS! see remarks above
79 init_thread();
80 }
81
82 return env;
83 }