/**************************************************************************/1/* thread_apple.h */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#pragma once3132#include "core/templates/safe_refcount.h"33#include "core/typedefs.h"3435#include <pthread.h>36#include <new> // For hardware interference size3738class String;3940class Thread {41public:42typedef void (*Callback)(void *p_userdata);4344typedef uint64_t ID;4546enum : ID {47UNASSIGNED_ID = 0,48MAIN_ID = 149};5051enum Priority {52PRIORITY_LOW,53PRIORITY_NORMAL,54PRIORITY_HIGH55};5657struct Settings {58Priority priority;59/// Override the default stack size (0 means default)60uint64_t stack_size = 0;61Settings() { priority = PRIORITY_NORMAL; }62};6364#if defined(__cpp_lib_hardware_interference_size)65GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")66static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;67GODOT_GCC_WARNING_POP68#else69// At a negligible memory cost, we use a conservatively high value.70static constexpr size_t CACHE_LINE_BYTES = 128;71#endif7273private:74friend class Main;7576ID id = UNASSIGNED_ID;77pthread_t pthread;7879static SafeNumeric<uint64_t> id_counter;80static thread_local ID caller_id;8182static void *thread_callback(void *p_data);8384static void make_main_thread() { caller_id = MAIN_ID; }85static void release_main_thread() { caller_id = id_counter.increment(); }8687public:88_FORCE_INLINE_ static void yield() { pthread_yield_np(); }8990_FORCE_INLINE_ ID get_id() const { return id; }91// get the ID of the caller thread92_FORCE_INLINE_ static ID get_caller_id() {93return caller_id;94}95// get the ID of the main thread96_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }9798_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; }99100static Error set_name(const String &p_name);101102ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());103bool is_started() const { return id != UNASSIGNED_ID; }104/// Waits until thread is finished, and deallocates it.105void wait_to_finish();106107Thread() = default;108~Thread();109};110111112