/**************************************************************************/1/* thread_posix.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#if defined(UNIX_ENABLED)3132#include "thread_posix.h"3334#include "core/os/thread.h"35#include "core/string/ustring.h"3637#if defined(PLATFORM_THREAD_OVERRIDE) && defined(__APPLE__)38void init_thread_posix() {39}40#else4142#ifdef PTHREAD_BSD_SET_NAME43#include <pthread_np.h>44#endif4546static Error set_name(const String &p_name) {47#ifdef PTHREAD_NO_RENAME48return ERR_UNAVAILABLE;4950#else5152#ifdef PTHREAD_RENAME_SELF5354// check if thread is the same as caller55int err = pthread_setname_np(p_name.utf8().get_data());5657#else5859pthread_t running_thread = pthread_self();60#ifdef PTHREAD_BSD_SET_NAME61pthread_set_name_np(running_thread, p_name.utf8().get_data());62int err = 0; // Open/FreeBSD ignore errors in this function63#elif defined(PTHREAD_NETBSD_SET_NAME)64int err = pthread_setname_np(running_thread, "%s", const_cast<char *>(p_name.utf8().get_data()));65#else66int err = pthread_setname_np(running_thread, p_name.utf8().get_data());67#endif // PTHREAD_BSD_SET_NAME6869#endif // PTHREAD_RENAME_SELF7071return err == 0 ? OK : ERR_INVALID_PARAMETER;7273#endif // PTHREAD_NO_RENAME74}7576void init_thread_posix() {77Thread::_set_platform_functions({ .set_name = set_name });78}7980#endif // PLATFORM_THREAD_OVERRIDE && __APPLE__8182#endif // UNIX_ENABLED838485