thread_jandroid.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*************************************************************************/
  2. /* thread_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/memory.h"
  32. #include "core/safe_refcount.h"
  33. #include "core/script_language.h"
  34. static void _thread_id_key_destr_callback(void *p_value) {
  35. memdelete(static_cast<Thread::ID *>(p_value));
  36. }
  37. static pthread_key_t _create_thread_id_key() {
  38. pthread_key_t key;
  39. pthread_key_create(&key, &_thread_id_key_destr_callback);
  40. return key;
  41. }
  42. pthread_key_t ThreadAndroid::thread_id_key = _create_thread_id_key();
  43. Thread::ID ThreadAndroid::next_thread_id = 0;
  44. Thread::ID ThreadAndroid::get_id() const {
  45. return id;
  46. }
  47. Thread *ThreadAndroid::create_thread_jandroid() {
  48. return memnew(ThreadAndroid);
  49. }
  50. void *ThreadAndroid::thread_callback(void *userdata) {
  51. ThreadAndroid *t = reinterpret_cast<ThreadAndroid *>(userdata);
  52. setup_thread();
  53. ScriptServer::thread_enter(); //scripts may need to attach a stack
  54. t->id = atomic_increment(&next_thread_id);
  55. pthread_setspecific(thread_id_key, (void *)memnew(ID(t->id)));
  56. t->callback(t->user);
  57. ScriptServer::thread_exit();
  58. return NULL;
  59. }
  60. Thread *ThreadAndroid::create_func_jandroid(ThreadCreateCallback p_callback, void *p_user, const Settings &) {
  61. ThreadAndroid *tr = memnew(ThreadAndroid);
  62. tr->callback = p_callback;
  63. tr->user = p_user;
  64. pthread_attr_init(&tr->pthread_attr);
  65. pthread_attr_setdetachstate(&tr->pthread_attr, PTHREAD_CREATE_JOINABLE);
  66. pthread_create(&tr->pthread, &tr->pthread_attr, thread_callback, tr);
  67. return tr;
  68. }
  69. Thread::ID ThreadAndroid::get_thread_id_func_jandroid() {
  70. void *value = pthread_getspecific(thread_id_key);
  71. if (value)
  72. return *static_cast<ID *>(value);
  73. ID new_id = atomic_increment(&next_thread_id);
  74. pthread_setspecific(thread_id_key, (void *)memnew(ID(new_id)));
  75. return new_id;
  76. }
  77. void ThreadAndroid::wait_to_finish_func_jandroid(Thread *p_thread) {
  78. ThreadAndroid *tp = static_cast<ThreadAndroid *>(p_thread);
  79. ERR_FAIL_COND(!tp);
  80. ERR_FAIL_COND(tp->pthread == 0);
  81. pthread_join(tp->pthread, NULL);
  82. tp->pthread = 0;
  83. }
  84. void ThreadAndroid::_thread_destroyed(void *value) {
  85. /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
  86. JNIEnv *env = (JNIEnv *)value;
  87. if (env != NULL) {
  88. java_vm->DetachCurrentThread();
  89. pthread_setspecific(jvm_key, NULL);
  90. }
  91. }
  92. pthread_key_t ThreadAndroid::jvm_key;
  93. JavaVM *ThreadAndroid::java_vm = NULL;
  94. void ThreadAndroid::setup_thread() {
  95. if (pthread_getspecific(jvm_key))
  96. return; //already setup
  97. JNIEnv *env;
  98. java_vm->AttachCurrentThread(&env, NULL);
  99. pthread_setspecific(jvm_key, (void *)env);
  100. }
  101. void ThreadAndroid::make_default(JavaVM *p_java_vm) {
  102. java_vm = p_java_vm;
  103. create_func = create_func_jandroid;
  104. get_thread_id_func = get_thread_id_func_jandroid;
  105. wait_to_finish_func = wait_to_finish_func_jandroid;
  106. pthread_key_create(&jvm_key, _thread_destroyed);
  107. setup_thread();
  108. }
  109. JNIEnv *ThreadAndroid::get_env() {
  110. if (!pthread_getspecific(jvm_key)) {
  111. setup_thread();
  112. }
  113. JNIEnv *env = NULL;
  114. java_vm->AttachCurrentThread(&env, NULL);
  115. return env;
  116. }
  117. ThreadAndroid::ThreadAndroid() {
  118. pthread = 0;
  119. }
  120. ThreadAndroid::~ThreadAndroid() {
  121. }