Debian Codegraph @

core/os/thread.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* thread.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 "platform_config.h"
32
33 #ifndef PLATFORM_THREAD_OVERRIDE // See details in thread.h.
34
35 #include "thread.h"
36
37 #ifdef THREADS_ENABLED
38 #include "core/object/script_language.h"
39
40 SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
41 thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
42
43 #endif
44
45 Thread::PlatformFunctions Thread::platform_functions;
46
47 void Thread::_set_platform_functions(const PlatformFunctions &p_functions) {
48 platform_functions = p_functions;
49 }
50
51 #ifdef THREADS_ENABLED
52 void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_callback, void *p_userdata) {
53 Thread::caller_id = p_caller_id;
54 if (platform_functions.set_priority) {
55 platform_functions.set_priority(p_settings.priority);
56 }
57 if (platform_functions.init) {
58 platform_functions.init();
59 }
60 ScriptServer::thread_enter(); // Scripts may need to attach a stack.
61 if (platform_functions.wrapper) {
62 platform_functions.wrapper(p_callback, p_userdata);
63 } else {
64 p_callback(p_userdata);
65 }
66 ScriptServer::thread_exit();
67 if (platform_functions.term) {
68 platform_functions.term();
69 }
70 }
71
72 Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
73 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.");
74 id = id_counter.increment();
75 thread = THREADING_NAMESPACE::thread(&Thread::callback, id, p_settings, p_callback, p_user);
76 return id;
77 }
78
79 bool Thread::is_started() const {
80 return id != UNASSIGNED_ID;
81 }
82
83 void Thread::wait_to_finish() {
84 ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
85 ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
86 thread.join();
87 thread = THREADING_NAMESPACE::thread();
88 id = UNASSIGNED_ID;
89 }
90
91 Error Thread::set_name(const String &p_name) {
92 if (platform_functions.set_name) {
93 return platform_functions.set_name(p_name);
94 }
95
96 return ERR_UNAVAILABLE;
97 }
98
99 Thread::~Thread() {
100 if (id != UNASSIGNED_ID) {
101 #ifdef DEBUG_ENABLED
102 WARN_PRINT(
103 "A Thread object is being destroyed without its completion having been realized.\n"
104 "Please call wait_to_finish() on it to ensure correct cleanup.");
105 #endif
106 thread.detach();
107 }
108 }
109
110 #endif // THREADS_ENABLED
111
112 #endif // PLATFORM_THREAD_OVERRIDE