thread.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* thread.h */
  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. #pragma once
  31. #include "platform_config.h"
  32. // Define PLATFORM_THREAD_OVERRIDE in your platform's `platform_config.h`
  33. // to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h`.
  34. // Overriding the Thread implementation is required in some proprietary platforms.
  35. #ifdef PLATFORM_THREAD_OVERRIDE
  36. #include "platform_thread.h"
  37. #else
  38. #include "core/typedefs.h"
  39. #ifdef THREADS_ENABLED
  40. #include "core/templates/safe_refcount.h"
  41. #include <new> // IWYU pragma: keep // For hardware interference size.
  42. #ifdef MINGW_ENABLED
  43. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  44. #include "thirdparty/mingw-std-threads/mingw.thread.h"
  45. #define THREADING_NAMESPACE mingw_stdthread
  46. #else
  47. #include <thread>
  48. #define THREADING_NAMESPACE std
  49. #endif
  50. class String;
  51. class Thread {
  52. public:
  53. typedef void (*Callback)(void *p_userdata);
  54. typedef uint64_t ID;
  55. enum : ID {
  56. UNASSIGNED_ID = 0,
  57. MAIN_ID = 1
  58. };
  59. enum Priority {
  60. PRIORITY_LOW,
  61. PRIORITY_NORMAL,
  62. PRIORITY_HIGH
  63. };
  64. struct Settings {
  65. Priority priority;
  66. Settings() { priority = PRIORITY_NORMAL; }
  67. };
  68. struct PlatformFunctions {
  69. Error (*set_name)(const String &) = nullptr;
  70. void (*set_priority)(Thread::Priority) = nullptr;
  71. void (*init)() = nullptr;
  72. void (*wrapper)(Thread::Callback, void *) = nullptr;
  73. void (*term)() = nullptr;
  74. };
  75. #if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) // This would be OK with NDK >= 26.
  76. GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")
  77. static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
  78. GODOT_GCC_WARNING_POP
  79. #else
  80. // At a negligible memory cost, we use a conservatively high value.
  81. static constexpr size_t CACHE_LINE_BYTES = 128;
  82. #endif
  83. private:
  84. friend class Main;
  85. static PlatformFunctions platform_functions;
  86. ID id = UNASSIGNED_ID;
  87. static SafeNumeric<uint64_t> id_counter;
  88. static thread_local ID caller_id;
  89. THREADING_NAMESPACE::thread thread;
  90. static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
  91. static void make_main_thread() { caller_id = MAIN_ID; }
  92. static void release_main_thread() { caller_id = id_counter.increment(); }
  93. public:
  94. static void _set_platform_functions(const PlatformFunctions &p_functions);
  95. _FORCE_INLINE_ static void yield() { std::this_thread::yield(); }
  96. _FORCE_INLINE_ ID get_id() const { return id; }
  97. // get the ID of the caller thread
  98. _FORCE_INLINE_ static ID get_caller_id() {
  99. return caller_id;
  100. }
  101. // get the ID of the main thread
  102. _FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
  103. _FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; } // Gain a tiny bit of perf here because there is no need to validate caller_id here, because only main thread will be set as 1.
  104. static Error set_name(const String &p_name);
  105. ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
  106. bool is_started() const;
  107. ///< waits until thread is finished, and deallocates it.
  108. void wait_to_finish();
  109. Thread();
  110. ~Thread();
  111. };
  112. #else // No threads.
  113. class String;
  114. class Thread {
  115. public:
  116. typedef void (*Callback)(void *p_userdata);
  117. typedef uint64_t ID;
  118. static constexpr size_t CACHE_LINE_BYTES = sizeof(void *);
  119. enum : ID {
  120. UNASSIGNED_ID = 0,
  121. MAIN_ID = 1
  122. };
  123. enum Priority {
  124. PRIORITY_LOW,
  125. PRIORITY_NORMAL,
  126. PRIORITY_HIGH
  127. };
  128. struct Settings {
  129. Priority priority;
  130. Settings() { priority = PRIORITY_NORMAL; }
  131. };
  132. struct PlatformFunctions {
  133. Error (*set_name)(const String &) = nullptr;
  134. void (*set_priority)(Thread::Priority) = nullptr;
  135. void (*init)() = nullptr;
  136. void (*wrapper)(Thread::Callback, void *) = nullptr;
  137. void (*term)() = nullptr;
  138. };
  139. private:
  140. friend class Main;
  141. static PlatformFunctions platform_functions;
  142. static void make_main_thread() {}
  143. static void release_main_thread() {}
  144. public:
  145. static void _set_platform_functions(const PlatformFunctions &p_functions);
  146. _FORCE_INLINE_ ID get_id() const { return 0; }
  147. _FORCE_INLINE_ static ID get_caller_id() { return MAIN_ID; }
  148. _FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
  149. _FORCE_INLINE_ static bool is_main_thread() { return true; }
  150. static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; }
  151. void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {}
  152. bool is_started() const { return false; }
  153. void wait_to_finish() {}
  154. };
  155. #endif // THREADS_ENABLED
  156. #endif // PLATFORM_THREAD_OVERRIDE