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