thread_jandroid.cpp 4.5 KB

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