thread_jandroid.cpp 4.9 KB

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