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