thread.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**************************************************************************/
  2. /* thread.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 "platform_config.h"
  31. #ifndef PLATFORM_THREAD_OVERRIDE // See details in thread.h
  32. #include "thread.h"
  33. #include "core/script_language.h"
  34. #if !defined(NO_THREADS)
  35. #include "core/safe_refcount.h"
  36. Error (*Thread::set_name_func)(const String &) = nullptr;
  37. void (*Thread::set_priority_func)(Thread::Priority) = nullptr;
  38. void (*Thread::init_func)() = nullptr;
  39. void (*Thread::term_func)() = nullptr;
  40. uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) {
  41. static std::hash<std::thread::id> hasher;
  42. return hasher(p_t);
  43. }
  44. Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id());
  45. static thread_local Thread::ID caller_id = 0;
  46. static thread_local bool caller_id_cached = false;
  47. void Thread::_set_platform_funcs(
  48. Error (*p_set_name_func)(const String &),
  49. void (*p_set_priority_func)(Thread::Priority),
  50. void (*p_init_func)(),
  51. void (*p_term_func)()) {
  52. Thread::set_name_func = p_set_name_func;
  53. Thread::set_priority_func = p_set_priority_func;
  54. Thread::init_func = p_init_func;
  55. Thread::term_func = p_term_func;
  56. }
  57. void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) {
  58. caller_id = _thread_id_hash(p_self->thread.get_id());
  59. caller_id_cached = true;
  60. if (set_priority_func) {
  61. set_priority_func(p_settings.priority);
  62. }
  63. if (init_func) {
  64. init_func();
  65. }
  66. ScriptServer::thread_enter(); //scripts may need to attach a stack
  67. p_callback(p_userdata);
  68. ScriptServer::thread_exit();
  69. if (term_func) {
  70. term_func();
  71. }
  72. }
  73. void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
  74. if (id != _thread_id_hash(std::thread::id())) {
  75. #ifdef DEBUG_ENABLED
  76. WARN_PRINT("A Thread object has been re-started without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread.");
  77. #endif
  78. thread.detach();
  79. std::thread empty_thread;
  80. thread.swap(empty_thread);
  81. }
  82. std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user);
  83. thread.swap(new_thread);
  84. id = _thread_id_hash(thread.get_id());
  85. }
  86. bool Thread::is_started() const {
  87. return id != _thread_id_hash(std::thread::id());
  88. }
  89. void Thread::wait_to_finish() {
  90. if (id != _thread_id_hash(std::thread::id())) {
  91. ERR_FAIL_COND_MSG(id == get_caller_id(), "A Thread can't wait for itself to finish.");
  92. thread.join();
  93. std::thread empty_thread;
  94. thread.swap(empty_thread);
  95. id = _thread_id_hash(std::thread::id());
  96. }
  97. }
  98. Error Thread::set_name(const String &p_name) {
  99. if (set_name_func) {
  100. return set_name_func(p_name);
  101. }
  102. return ERR_UNAVAILABLE;
  103. }
  104. Thread::~Thread() {
  105. if (id != _thread_id_hash(std::thread::id())) {
  106. #ifdef DEBUG_ENABLED
  107. WARN_PRINT(
  108. "A Thread object is being destroyed without its completion having been realized.\n"
  109. "Please call wait_to_finish() on it to ensure correct cleanup.");
  110. #endif
  111. thread.detach();
  112. }
  113. }
  114. Thread::ID Thread::get_caller_id() {
  115. if (likely(caller_id_cached)) {
  116. return caller_id;
  117. } else {
  118. caller_id = _thread_id_hash(std::this_thread::get_id());
  119. caller_id_cached = true;
  120. return caller_id;
  121. }
  122. }
  123. #endif
  124. #endif // PLATFORM_THREAD_OVERRIDE