thread_jandroid.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "thread_jandroid.h"
  31. #include "core/os/thread.h"
  32. #include <android/log.h>
  33. static JavaVM *java_vm = nullptr;
  34. static thread_local JNIEnv *env = nullptr;
  35. // The logic here need to improve, init_thread/term_tread are designed to work with Thread::callback
  36. // Calling init_thread from setup_android_thread and get_jni_env to setup an env we're keeping and not detaching
  37. // could cause issues on app termination.
  38. //
  39. // We should be making sure that any thread started calls a nice cleanup function when it's done,
  40. // especially now that we use many more threads.
  41. static void init_thread() {
  42. if (env) {
  43. // thread never detached! just keep using...
  44. return;
  45. }
  46. java_vm->AttachCurrentThread(&env, nullptr);
  47. }
  48. static void term_thread() {
  49. java_vm->DetachCurrentThread();
  50. // this is no longer valid, must called init_thread to re-establish
  51. env = nullptr;
  52. }
  53. void init_thread_jandroid(JavaVM *p_jvm, JNIEnv *p_env) {
  54. java_vm = p_jvm;
  55. env = p_env;
  56. Thread::_set_platform_functions({ .init = init_thread, .term = &term_thread });
  57. }
  58. void setup_android_thread() {
  59. if (!env) {
  60. // !BAS! see remarks above
  61. init_thread();
  62. }
  63. }
  64. JNIEnv *get_jni_env() {
  65. if (!env) {
  66. // !BAS! see remarks above
  67. init_thread();
  68. }
  69. return env;
  70. }