thread_apple.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (p_settings.stack_size > 0) {
  79. pthread_attr_setstacksize(&attr, p_settings.stack_size);
  80. }
  81. // Create the thread
  82. pthread_create(&pthread, &attr, thread_callback, thread_data);
  83. // Clean up attributes
  84. pthread_attr_destroy(&attr);
  85. return id;
  86. }
  87. void Thread::wait_to_finish() {
  88. ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
  89. ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
  90. int err = pthread_join(pthread, nullptr);
  91. if (err != 0) {
  92. ERR_FAIL_MSG("Thread::wait_to_finish() failed to join thread.");
  93. }
  94. pthread = pthread_t();
  95. id = UNASSIGNED_ID;
  96. }
  97. Thread::~Thread() {
  98. if (id != UNASSIGNED_ID) {
  99. #ifdef DEBUG_ENABLED
  100. WARN_PRINT(
  101. "A Thread object is being destroyed without its completion having been realized.\n"
  102. "Please call wait_to_finish() on it to ensure correct cleanup.");
  103. #endif
  104. pthread_detach(pthread);
  105. }
  106. }