Debian Codegraph @

core/os/thread.h

raw symbols SCIP
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
31 #pragma once
32
33 #include "platform_config.h"
34
35 // Define PLATFORM_THREAD_OVERRIDE in your platform's `platform_config.h`
36 // to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h`.
37 // Overriding the Thread implementation is required in some proprietary platforms.
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> // IWYU pragma: keep // For hardware interference size.
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) // This would be OK with NDK >= 26.
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 // At a negligible memory cost, we use a conservatively high value.
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 // get the ID of the caller thread
126 _FORCE_INLINE_ static ID get_caller_id() {
127 return caller_id;
128 }
129 // get the ID of the main thread
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; } // 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.
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 ///< waits until thread is finished, and deallocates it.
139 void wait_to_finish();
140
141 ~Thread();
142 };
143
144 #else // No threads.
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 // THREADS_ENABLED
205
206 #endif // PLATFORM_THREAD_OVERRIDE