Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/apple/thread_apple.cpp
9973 views
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
31
#include "thread_apple.h"
32
33
#include "core/error/error_macros.h"
34
#include "core/object/script_language.h"
35
#include "core/string/ustring.h"
36
37
SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
38
thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
39
40
struct ThreadData {
41
Thread::Callback callback;
42
void *userdata;
43
Thread::ID caller_id;
44
};
45
46
void *Thread::thread_callback(void *p_data) {
47
ThreadData *thread_data = static_cast<ThreadData *>(p_data);
48
49
// Set the caller ID for this thread
50
caller_id = thread_data->caller_id;
51
52
ScriptServer::thread_enter(); // Scripts may need to attach a stack.
53
54
// Call the actual callback
55
thread_data->callback(thread_data->userdata);
56
57
ScriptServer::thread_exit();
58
59
// Clean up
60
memdelete(thread_data);
61
62
return nullptr;
63
}
64
65
Error Thread::set_name(const String &p_name) {
66
int err = pthread_setname_np(p_name.utf8().get_data());
67
return err == 0 ? OK : ERR_INVALID_PARAMETER;
68
}
69
70
Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
71
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.");
72
id = id_counter.increment();
73
74
ThreadData *thread_data = memnew(ThreadData);
75
thread_data->callback = p_callback;
76
thread_data->userdata = p_user;
77
thread_data->caller_id = id;
78
79
// Create the thread
80
pthread_attr_t attr;
81
pthread_attr_init(&attr);
82
83
switch (p_settings.priority) {
84
case PRIORITY_LOW:
85
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_UTILITY, 0);
86
break;
87
case PRIORITY_NORMAL:
88
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INITIATED, 0);
89
break;
90
case PRIORITY_HIGH:
91
pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
92
break;
93
}
94
95
if (p_settings.stack_size > 0) {
96
pthread_attr_setstacksize(&attr, p_settings.stack_size);
97
}
98
99
// Create the thread
100
pthread_create(&pthread, &attr, thread_callback, thread_data);
101
102
// Clean up attributes
103
pthread_attr_destroy(&attr);
104
105
return id;
106
}
107
108
void Thread::wait_to_finish() {
109
ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
110
ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
111
112
int err = pthread_join(pthread, nullptr);
113
if (err != 0) {
114
ERR_FAIL_MSG("Thread::wait_to_finish() failed to join thread.");
115
}
116
pthread = pthread_t();
117
id = UNASSIGNED_ID;
118
}
119
120
Thread::~Thread() {
121
if (id != UNASSIGNED_ID) {
122
#ifdef DEBUG_ENABLED
123
WARN_PRINT(
124
"A Thread object is being destroyed without its completion having been realized.\n"
125
"Please call wait_to_finish() on it to ensure correct cleanup.");
126
#endif
127
pthread_detach(pthread);
128
}
129
}
130
131