Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/os/thread.h
9903 views
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
~Thread();
143
};
144
145
#else // No threads.
146
147
class String;
148
149
class Thread {
150
public:
151
typedef void (*Callback)(void *p_userdata);
152
153
typedef uint64_t ID;
154
155
static constexpr size_t CACHE_LINE_BYTES = sizeof(void *);
156
157
enum : ID {
158
UNASSIGNED_ID = 0,
159
MAIN_ID = 1
160
};
161
162
enum Priority {
163
PRIORITY_LOW,
164
PRIORITY_NORMAL,
165
PRIORITY_HIGH
166
};
167
168
struct Settings {
169
Priority priority;
170
Settings() { priority = PRIORITY_NORMAL; }
171
};
172
173
struct PlatformFunctions {
174
Error (*set_name)(const String &) = nullptr;
175
void (*set_priority)(Thread::Priority) = nullptr;
176
void (*init)() = nullptr;
177
void (*wrapper)(Thread::Callback, void *) = nullptr;
178
void (*term)() = nullptr;
179
};
180
181
private:
182
friend class Main;
183
184
static PlatformFunctions platform_functions;
185
186
static void make_main_thread() {}
187
static void release_main_thread() {}
188
189
public:
190
static void _set_platform_functions(const PlatformFunctions &p_functions);
191
192
_FORCE_INLINE_ ID get_id() const { return 0; }
193
_FORCE_INLINE_ static ID get_caller_id() { return MAIN_ID; }
194
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
195
196
_FORCE_INLINE_ static bool is_main_thread() { return true; }
197
198
static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; }
199
200
void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {}
201
bool is_started() const { return false; }
202
void wait_to_finish() {}
203
};
204
205
#endif // THREADS_ENABLED
206
207
#endif // PLATFORM_THREAD_OVERRIDE
208
209