thread.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifdef THREADS_ENABLED
  34. #include "core/object/script_language.h"
  35. #include "core/templates/safe_refcount.h"
  36. SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
  37. thread_local Thread::ID Thread::caller_id = Thread::UNASSIGNED_ID;
  38. #endif
  39. Thread::PlatformFunctions Thread::platform_functions;
  40. void Thread::_set_platform_functions(const PlatformFunctions &p_functions) {
  41. platform_functions = p_functions;
  42. }
  43. #ifdef THREADS_ENABLED
  44. void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_callback, void *p_userdata) {
  45. Thread::caller_id = p_caller_id;
  46. if (platform_functions.set_priority) {
  47. platform_functions.set_priority(p_settings.priority);
  48. }
  49. if (platform_functions.init) {
  50. platform_functions.init();
  51. }
  52. ScriptServer::thread_enter(); // Scripts may need to attach a stack.
  53. if (platform_functions.wrapper) {
  54. platform_functions.wrapper(p_callback, p_userdata);
  55. } else {
  56. p_callback(p_userdata);
  57. }
  58. ScriptServer::thread_exit();
  59. if (platform_functions.term) {
  60. platform_functions.term();
  61. }
  62. }
  63. Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
  64. ERR_FAIL_COND_V_MSG(id != UNASSIGNED_ID, UNASSIGNED_ID, "A Thread object has been re-started without wait_to_finish() having been called on it.");
  65. id = id_counter.increment();
  66. thread = THREADING_NAMESPACE::thread(&Thread::callback, id, p_settings, p_callback, p_user);
  67. return id;
  68. }
  69. bool Thread::is_started() const {
  70. return id != UNASSIGNED_ID;
  71. }
  72. void Thread::wait_to_finish() {
  73. ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
  74. ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
  75. thread.join();
  76. thread = THREADING_NAMESPACE::thread();
  77. id = UNASSIGNED_ID;
  78. }
  79. Error Thread::set_name(const String &p_name) {
  80. if (platform_functions.set_name) {
  81. return platform_functions.set_name(p_name);
  82. }
  83. return ERR_UNAVAILABLE;
  84. }
  85. Thread::Thread() {
  86. }
  87. Thread::~Thread() {
  88. if (id != UNASSIGNED_ID) {
  89. #ifdef DEBUG_ENABLED
  90. WARN_PRINT(
  91. "A Thread object is being destroyed without its completion having been realized.\n"
  92. "Please call wait_to_finish() on it to ensure correct cleanup.");
  93. #endif
  94. thread.detach();
  95. }
  96. }
  97. #endif // THREADS_ENABLED
  98. #endif // PLATFORM_THREAD_OVERRIDE