thread.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/object/script_language.h"
  32. #if !defined(NO_THREADS)
  33. #include "core/templates/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. thread_local Thread::ID Thread::caller_id = 0;
  44. void Thread::_set_platform_funcs(
  45. Error (*p_set_name_func)(const String &),
  46. void (*p_set_priority_func)(Thread::Priority),
  47. void (*p_init_func)(),
  48. void (*p_term_func)()) {
  49. Thread::set_name_func = p_set_name_func;
  50. Thread::set_priority_func = p_set_priority_func;
  51. Thread::init_func = p_init_func;
  52. Thread::term_func = p_term_func;
  53. }
  54. void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) {
  55. Thread::caller_id = _thread_id_hash(p_self->thread.get_id());
  56. if (set_priority_func) {
  57. set_priority_func(p_settings.priority);
  58. }
  59. if (init_func) {
  60. init_func();
  61. }
  62. ScriptServer::thread_enter(); //scripts may need to attach a stack
  63. p_callback(p_userdata);
  64. ScriptServer::thread_exit();
  65. if (term_func) {
  66. term_func();
  67. }
  68. }
  69. void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
  70. if (id != _thread_id_hash(std::thread::id())) {
  71. #ifdef DEBUG_ENABLED
  72. 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.");
  73. #endif
  74. thread.detach();
  75. std::thread empty_thread;
  76. thread.swap(empty_thread);
  77. }
  78. std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user);
  79. thread.swap(new_thread);
  80. id = _thread_id_hash(thread.get_id());
  81. }
  82. bool Thread::is_started() const {
  83. return id != _thread_id_hash(std::thread::id());
  84. }
  85. void Thread::wait_to_finish() {
  86. if (id != _thread_id_hash(std::thread::id())) {
  87. ERR_FAIL_COND_MSG(id == get_caller_id(), "A Thread can't wait for itself to finish.");
  88. thread.join();
  89. std::thread empty_thread;
  90. thread.swap(empty_thread);
  91. id = _thread_id_hash(std::thread::id());
  92. }
  93. }
  94. Error Thread::set_name(const String &p_name) {
  95. if (set_name_func) {
  96. return set_name_func(p_name);
  97. }
  98. return ERR_UNAVAILABLE;
  99. }
  100. Thread::Thread() {
  101. caller_id = _thread_id_hash(std::this_thread::get_id());
  102. }
  103. Thread::~Thread() {
  104. if (id != _thread_id_hash(std::thread::id())) {
  105. #ifdef DEBUG_ENABLED
  106. 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.");
  107. #endif
  108. thread.detach();
  109. }
  110. }
  111. #endif