thread_jandroid.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*************************************************************************/
  2. /* thread_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "thread_jandroid.h"
  30. #include "os/memory.h"
  31. Thread::ID ThreadAndroid::get_ID() const {
  32. return id;
  33. }
  34. Thread* ThreadAndroid::create_thread_jandroid() {
  35. return memnew( ThreadAndroid );
  36. }
  37. void *ThreadAndroid::thread_callback(void *userdata) {
  38. ThreadAndroid *t=reinterpret_cast<ThreadAndroid*>(userdata);
  39. setup_thread();
  40. t->id=(ID)pthread_self();
  41. t->callback(t->user);
  42. return NULL;
  43. }
  44. Thread* ThreadAndroid::create_func_jandroid(ThreadCreateCallback p_callback,void *p_user,const Settings&) {
  45. ThreadAndroid *tr= memnew(ThreadAndroid);
  46. tr->callback=p_callback;
  47. tr->user=p_user;
  48. pthread_attr_init(&tr->pthread_attr);
  49. pthread_attr_setdetachstate(&tr->pthread_attr, PTHREAD_CREATE_JOINABLE);
  50. pthread_create(&tr->pthread, &tr->pthread_attr, thread_callback, tr);
  51. return tr;
  52. }
  53. Thread::ID ThreadAndroid::get_thread_ID_func_jandroid() {
  54. return (ID)pthread_self();
  55. }
  56. void ThreadAndroid::wait_to_finish_func_jandroid(Thread* p_thread) {
  57. ThreadAndroid *tp=static_cast<ThreadAndroid*>(p_thread);
  58. ERR_FAIL_COND(!tp);
  59. ERR_FAIL_COND(tp->pthread==0);
  60. pthread_join(tp->pthread,NULL);
  61. tp->pthread=0;
  62. }
  63. void ThreadAndroid::_thread_destroyed(void* value) {
  64. /* The thread is being destroyed, detach it from the Java VM and set the mThreadKey value to NULL as required */
  65. JNIEnv *env = (JNIEnv*) value;
  66. if (env != NULL) {
  67. java_vm->DetachCurrentThread();
  68. pthread_setspecific(jvm_key, NULL);
  69. }
  70. }
  71. pthread_key_t ThreadAndroid::jvm_key;
  72. JavaVM* ThreadAndroid::java_vm=NULL;
  73. void ThreadAndroid::setup_thread() {
  74. if (pthread_getspecific(jvm_key))
  75. return; //already setup
  76. JNIEnv *env;
  77. java_vm->AttachCurrentThread(&env, NULL);
  78. pthread_setspecific(jvm_key, (void*) env);
  79. }
  80. void ThreadAndroid::make_default(JavaVM* p_java_vm) {
  81. java_vm=p_java_vm;
  82. create_func=create_func_jandroid;
  83. get_thread_ID_func=get_thread_ID_func_jandroid;
  84. wait_to_finish_func=wait_to_finish_func_jandroid;
  85. pthread_key_create(&jvm_key, _thread_destroyed);
  86. setup_thread();
  87. }
  88. JNIEnv *ThreadAndroid::get_env() {
  89. if (!pthread_getspecific(jvm_key)) {
  90. setup_thread();
  91. }
  92. JNIEnv *env=NULL;
  93. int status = java_vm->AttachCurrentThread(&env, NULL);
  94. return env;
  95. }
  96. ThreadAndroid::ThreadAndroid() {
  97. pthread=0;
  98. }
  99. ThreadAndroid::~ThreadAndroid() {
  100. }