thread.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/object/script_language.h"
  34. #include "core/templates/safe_refcount.h"
  35. Thread::PlatformFunctions Thread::platform_functions;
  36. uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) {
  37. static std::hash<std::thread::id> hasher;
  38. return hasher(p_t);
  39. }
  40. Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id());
  41. thread_local Thread::ID Thread::caller_id = 0;
  42. void Thread::_set_platform_functions(const PlatformFunctions &p_functions) {
  43. platform_functions = p_functions;
  44. }
  45. void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) {
  46. Thread::caller_id = _thread_id_hash(p_self->thread.get_id());
  47. if (platform_functions.set_priority) {
  48. platform_functions.set_priority(p_settings.priority);
  49. }
  50. if (platform_functions.init) {
  51. platform_functions.init();
  52. }
  53. ScriptServer::thread_enter(); // Scripts may need to attach a stack.
  54. if (platform_functions.wrapper) {
  55. platform_functions.wrapper(p_callback, p_userdata);
  56. } else {
  57. p_callback(p_userdata);
  58. }
  59. ScriptServer::thread_exit();
  60. if (platform_functions.term) {
  61. platform_functions.term();
  62. }
  63. }
  64. void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
  65. if (id != _thread_id_hash(std::thread::id())) {
  66. #ifdef DEBUG_ENABLED
  67. 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.");
  68. #endif
  69. thread.detach();
  70. std::thread empty_thread;
  71. thread.swap(empty_thread);
  72. }
  73. std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user);
  74. thread.swap(new_thread);
  75. id = _thread_id_hash(thread.get_id());
  76. }
  77. bool Thread::is_started() const {
  78. return id != _thread_id_hash(std::thread::id());
  79. }
  80. void Thread::wait_to_finish() {
  81. if (id != _thread_id_hash(std::thread::id())) {
  82. ERR_FAIL_COND_MSG(id == get_caller_id(), "A Thread can't wait for itself to finish.");
  83. thread.join();
  84. std::thread empty_thread;
  85. thread.swap(empty_thread);
  86. id = _thread_id_hash(std::thread::id());
  87. }
  88. }
  89. Error Thread::set_name(const String &p_name) {
  90. if (platform_functions.set_name) {
  91. return platform_functions.set_name(p_name);
  92. }
  93. return ERR_UNAVAILABLE;
  94. }
  95. Thread::Thread() {
  96. caller_id = _thread_id_hash(std::this_thread::get_id());
  97. }
  98. Thread::~Thread() {
  99. if (id != _thread_id_hash(std::thread::id())) {
  100. #ifdef DEBUG_ENABLED
  101. 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.");
  102. #endif
  103. thread.detach();
  104. }
  105. }
  106. #endif // PLATFORM_THREAD_OVERRIDE