/**************************************************************************/1/* thread.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "platform_config.h"3132#ifndef PLATFORM_THREAD_OVERRIDE // See details in thread.h.3334#include "thread.h"3536#ifdef THREADS_ENABLED37#include "core/object/script_language.h"3839SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.40thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();4142#endif4344Thread::PlatformFunctions Thread::platform_functions;4546void Thread::_set_platform_functions(const PlatformFunctions &p_functions) {47platform_functions = p_functions;48}4950#ifdef THREADS_ENABLED51void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_callback, void *p_userdata) {52Thread::caller_id = p_caller_id;53if (platform_functions.set_priority) {54platform_functions.set_priority(p_settings.priority);55}56if (platform_functions.init) {57platform_functions.init();58}59ScriptServer::thread_enter(); // Scripts may need to attach a stack.60if (platform_functions.wrapper) {61platform_functions.wrapper(p_callback, p_userdata);62} else {63p_callback(p_userdata);64}65ScriptServer::thread_exit();66if (platform_functions.term) {67platform_functions.term();68}69}7071Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {72ERR_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.");73id = id_counter.increment();74thread = THREADING_NAMESPACE::thread(&Thread::callback, id, p_settings, p_callback, p_user);75return id;76}7778bool Thread::is_started() const {79return id != UNASSIGNED_ID;80}8182void Thread::wait_to_finish() {83ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");84ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");85thread.join();86thread = THREADING_NAMESPACE::thread();87id = UNASSIGNED_ID;88}8990Error Thread::set_name(const String &p_name) {91if (platform_functions.set_name) {92return platform_functions.set_name(p_name);93}9495return ERR_UNAVAILABLE;96}9798Thread::Thread() {99}100101Thread::~Thread() {102if (id != UNASSIGNED_ID) {103#ifdef DEBUG_ENABLED104WARN_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#endif108thread.detach();109}110}111112#endif // THREADS_ENABLED113114#endif // PLATFORM_THREAD_OVERRIDE115116117