thread_apple.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**************************************************************************/
  2. /* thread_apple.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 "thread_apple.h"
  31. #include "core/error/error_macros.h"
  32. #include "core/object/script_language.h"
  33. #include "core/string/ustring.h"
  34. SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
  35. thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
  36. struct ThreadData {
  37. Thread::Callback callback;
  38. void *userdata;
  39. Thread::ID caller_id;
  40. };
  41. void *Thread::thread_callback(void *p_data) {
  42. ThreadData *thread_data = static_cast<ThreadData *>(p_data);
  43. // Set the caller ID for this thread
  44. caller_id = thread_data->caller_id;
  45. ScriptServer::thread_enter(); // Scripts may need to attach a stack.
  46. // Call the actual callback
  47. thread_data->callback(thread_data->userdata);
  48. ScriptServer::thread_exit();
  49. // Clean up
  50. memdelete(thread_data);
  51. return nullptr;
  52. }
  53. Error Thread::set_name(const String &p_name) {
  54. int err = pthread_setname_np(p_name.utf8().get_data());
  55. return err == 0 ? OK : ERR_INVALID_PARAMETER;
  56. }
  57. Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
  58. 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.");
  59. id = id_counter.increment();
  60. ThreadData *thread_data = memnew(ThreadData);
  61. thread_data->callback = p_callback;
  62. thread_data->userdata = p_user;
  63. thread_data->caller_id = id;
  64. // Create the thread
  65. pthread_attr_t attr;
  66. pthread_attr_init(&attr);
  67. switch (p_settings.priority) {
  68. case PRIORITY_LOW:
  69. pthread_attr_set_qos_class_np(&attr, QOS_CLASS_UTILITY, 0);
  70. break;
  71. case PRIORITY_NORMAL:
  72. pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0);
  73. break;
  74. case PRIORITY_HIGH:
  75. pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
  76. break;
  77. }
  78. // The default stack size for secondary threads on Apple platforms is 512KiB.
  79. // This is insufficient when using a library like SPIRV-Cross, which can generate deep stacks and result in a stack overflow.
  80. // It also creates a problematic discrepancy with other platforms, where secondary threads are often at least 1 MiB.
  81. pthread_attr_setstacksize(&attr,
  82. #if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer)
  83. // ASan (and to some degree TSan) needs a lot of extra stack size.
  84. 4 * 1024 * 1024 // 4 MiB
  85. #elif !defined(__OPTIMIZE__)
  86. // Unoptimized builds also need a larger stack size.
  87. 2 * 1024 * 1024 // 2 MiB
  88. #else
  89. 1 * 1024 * 1024 // 1 MiB
  90. #endif
  91. );
  92. // Create the thread
  93. pthread_create(&pthread, &attr, thread_callback, thread_data);
  94. // Clean up attributes
  95. pthread_attr_destroy(&attr);
  96. return id;
  97. }
  98. void Thread::wait_to_finish() {
  99. ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
  100. ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
  101. int err = pthread_join(pthread, nullptr);
  102. if (err != 0) {
  103. ERR_FAIL_MSG("Thread::wait_to_finish() failed to join thread.");
  104. }
  105. pthread = pthread_t();
  106. id = UNASSIGNED_ID;
  107. }
  108. Thread::~Thread() {
  109. if (id != UNASSIGNED_ID) {
  110. #ifdef DEBUG_ENABLED
  111. WARN_PRINT(
  112. "A Thread object is being destroyed without its completion having been realized.\n"
  113. "Please call wait_to_finish() on it to ensure correct cleanup.");
  114. #endif
  115. pthread_detach(pthread);
  116. }
  117. }