thread.cpp 5.1 KB

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