thread.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Thread::ID Thread::main_thread_id = 1;
  39. SafeNumeric<Thread::ID> Thread::last_thread_id{ 1 };
  40. thread_local Thread::ID Thread::caller_id = 1;
  41. void Thread::_set_platform_funcs(
  42. Error (*p_set_name_func)(const String &),
  43. void (*p_set_priority_func)(Thread::Priority),
  44. void (*p_init_func)(),
  45. void (*p_term_func)()) {
  46. Thread::set_name_func = p_set_name_func;
  47. Thread::set_priority_func = p_set_priority_func;
  48. Thread::init_func = p_init_func;
  49. Thread::term_func = p_term_func;
  50. }
  51. void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) {
  52. Thread::caller_id = p_self->id;
  53. if (set_priority_func) {
  54. set_priority_func(p_settings.priority);
  55. }
  56. if (init_func) {
  57. init_func();
  58. }
  59. ScriptServer::thread_enter(); //scripts may need to attach a stack
  60. p_callback(p_userdata);
  61. ScriptServer::thread_exit();
  62. if (term_func) {
  63. term_func();
  64. }
  65. }
  66. void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
  67. if (id != 0) {
  68. #ifdef DEBUG_ENABLED
  69. 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.");
  70. #endif
  71. thread.detach();
  72. std::thread empty_thread;
  73. thread.swap(empty_thread);
  74. }
  75. id = last_thread_id.increment();
  76. std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user);
  77. thread.swap(new_thread);
  78. }
  79. bool Thread::is_started() const {
  80. return id != 0;
  81. }
  82. void Thread::wait_to_finish() {
  83. if (id != 0) {
  84. thread.join();
  85. std::thread empty_thread;
  86. thread.swap(empty_thread);
  87. id = 0;
  88. }
  89. }
  90. Error Thread::set_name(const String &p_name) {
  91. if (set_name_func) {
  92. return set_name_func(p_name);
  93. }
  94. return ERR_UNAVAILABLE;
  95. }
  96. Thread::~Thread() {
  97. if (id != 0) {
  98. #ifdef DEBUG_ENABLED
  99. 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.");
  100. #endif
  101. thread.detach();
  102. }
  103. }
  104. #endif