| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "platform_config.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#ifdef PLATFORM_THREAD_OVERRIDE |
| 40 |
|
| 41 |
#include "platform_thread.h" |
| 42 |
|
| 43 |
#else |
| 44 |
|
| 45 |
#include "core/typedefs.h" |
| 46 |
|
| 47 |
#ifdef THREADS_ENABLED |
| 48 |
|
| 49 |
#include "core/templates/safe_refcount.h" |
| 50 |
|
| 51 |
#include <new> |
| 52 |
|
| 53 |
#ifdef MINGW_ENABLED |
| 54 |
#define MINGW_STDTHREAD_REDUNDANCY_WARNING |
| 55 |
#include "thirdparty/mingw-std-threads/mingw.thread.h" |
| 56 |
#define THREADING_NAMESPACE mingw_stdthread |
| 57 |
#else |
| 58 |
#include <thread> |
| 59 |
#define THREADING_NAMESPACE std |
| 60 |
#endif |
| 61 |
|
| 62 |
class String; |
| 63 |
|
| 64 |
class Thread { |
| 65 |
public: |
| 66 |
typedef void (*Callback)(void *p_userdata); |
| 67 |
|
| 68 |
typedef uint64_t ID; |
| 69 |
|
| 70 |
enum : ID { |
| 71 |
UNASSIGNED_ID = 0, |
| 72 |
MAIN_ID = 1 |
| 73 |
}; |
| 74 |
|
| 75 |
enum Priority { |
| 76 |
PRIORITY_LOW, |
| 77 |
PRIORITY_NORMAL, |
| 78 |
PRIORITY_HIGH |
| 79 |
}; |
| 80 |
|
| 81 |
struct Settings { |
| 82 |
Priority priority; |
| 83 |
Settings() { priority = PRIORITY_NORMAL; } |
| 84 |
}; |
| 85 |
|
| 86 |
struct PlatformFunctions { |
| 87 |
Error (*set_name)(const String &) = nullptr; |
| 88 |
void (*set_priority)(Thread::Priority) = nullptr; |
| 89 |
void (*init)() = nullptr; |
| 90 |
void (*wrapper)(Thread::Callback, void *) = nullptr; |
| 91 |
void (*term)() = nullptr; |
| 92 |
}; |
| 93 |
|
| 94 |
#if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) |
| 95 |
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size") |
| 96 |
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size; |
| 97 |
GODOT_GCC_WARNING_POP |
| 98 |
#else |
| 99 |
|
| 100 |
static constexpr size_t CACHE_LINE_BYTES = 128; |
| 101 |
#endif |
| 102 |
|
| 103 |
private: |
| 104 |
friend class Main; |
| 105 |
|
| 106 |
static PlatformFunctions platform_functions; |
| 107 |
|
| 108 |
ID id = UNASSIGNED_ID; |
| 109 |
|
| 110 |
static SafeNumeric<uint64_t> id_counter; |
| 111 |
static thread_local ID caller_id; |
| 112 |
THREADING_NAMESPACE::thread thread; |
| 113 |
|
| 114 |
static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); |
| 115 |
|
| 116 |
static void make_main_thread() { caller_id = MAIN_ID; } |
| 117 |
static void release_main_thread() { caller_id = id_counter.increment(); } |
| 118 |
|
| 119 |
public: |
| 120 |
static void _set_platform_functions(const PlatformFunctions &p_functions); |
| 121 |
|
| 122 |
_FORCE_INLINE_ static void yield() { std::this_thread::yield(); } |
| 123 |
|
| 124 |
_FORCE_INLINE_ ID get_id() const { return id; } |
| 125 |
|
| 126 |
_FORCE_INLINE_ static ID get_caller_id() { |
| 127 |
return caller_id; |
| 128 |
} |
| 129 |
|
| 130 |
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; } |
| 131 |
|
| 132 |
_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; } |
| 133 |
|
| 134 |
static Error set_name(const String &p_name); |
| 135 |
|
| 136 |
ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()); |
| 137 |
bool is_started() const; |
| 138 |
|
| 139 |
void wait_to_finish(); |
| 140 |
|
| 141 |
~Thread(); |
| 142 |
}; |
| 143 |
|
| 144 |
#else |
| 145 |
|
| 146 |
class String; |
| 147 |
|
| 148 |
class Thread { |
| 149 |
public: |
| 150 |
typedef void (*Callback)(void *p_userdata); |
| 151 |
|
| 152 |
typedef uint64_t ID; |
| 153 |
|
| 154 |
static constexpr size_t CACHE_LINE_BYTES = sizeof(void *); |
| 155 |
|
| 156 |
enum : ID { |
| 157 |
UNASSIGNED_ID = 0, |
| 158 |
MAIN_ID = 1 |
| 159 |
}; |
| 160 |
|
| 161 |
enum Priority { |
| 162 |
PRIORITY_LOW, |
| 163 |
PRIORITY_NORMAL, |
| 164 |
PRIORITY_HIGH |
| 165 |
}; |
| 166 |
|
| 167 |
struct Settings { |
| 168 |
Priority priority; |
| 169 |
Settings() { priority = PRIORITY_NORMAL; } |
| 170 |
}; |
| 171 |
|
| 172 |
struct PlatformFunctions { |
| 173 |
Error (*set_name)(const String &) = nullptr; |
| 174 |
void (*set_priority)(Thread::Priority) = nullptr; |
| 175 |
void (*init)() = nullptr; |
| 176 |
void (*wrapper)(Thread::Callback, void *) = nullptr; |
| 177 |
void (*term)() = nullptr; |
| 178 |
}; |
| 179 |
|
| 180 |
private: |
| 181 |
friend class Main; |
| 182 |
|
| 183 |
static PlatformFunctions platform_functions; |
| 184 |
|
| 185 |
static void make_main_thread() {} |
| 186 |
static void release_main_thread() {} |
| 187 |
|
| 188 |
public: |
| 189 |
static void _set_platform_functions(const PlatformFunctions &p_functions); |
| 190 |
|
| 191 |
_FORCE_INLINE_ ID get_id() const { return 0; } |
| 192 |
_FORCE_INLINE_ static ID get_caller_id() { return MAIN_ID; } |
| 193 |
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; } |
| 194 |
|
| 195 |
_FORCE_INLINE_ static bool is_main_thread() { return true; } |
| 196 |
|
| 197 |
static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; } |
| 198 |
|
| 199 |
void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {} |
| 200 |
bool is_started() const { return false; } |
| 201 |
void wait_to_finish() {} |
| 202 |
}; |
| 203 |
|
| 204 |
#endif |
| 205 |
|
| 206 |
#endif |