Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2018-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <bitcoin-build-config.h> // IWYU pragma: keep7#include <util/threadnames.h>8#include <util/check.h>10#include <algorithm>11#include <cstring>12#include <string>14#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))15#include <pthread.h>16#include <pthread_np.h>17#endif19#if __has_include(<sys/prctl.h>)20#include <sys/prctl.h>21#endif23#ifdef HAVE_SETTHREADDESCRIPTION24#include <windows.h>25#endif27//! Set the thread's name at the process level. Does not affect the28//! internal name.29static void SetThreadName(const char* name)30{31#if defined(PR_SET_NAME)32// Only the first 15 characters are used (16 - NUL terminator)33::prctl(PR_SET_NAME, name, 0, 0, 0);34#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))35pthread_set_name_np(pthread_self(), name);36#elif defined(__APPLE__)37pthread_setname_np(name);38#elif defined(HAVE_SETTHREADDESCRIPTION)39// Thread names are ASCII-only, so widening each character is sufficient as40// a conversion to UTF-16.41const std::wstring wname{name, name + std::strlen(name)};42::SetThreadDescription(::GetCurrentThread(), wname.c_str());43#else44// Prevent warnings for unused parameters...45(void)name;46#endif47}49/**50* The name of the thread. We use char array instead of std::string to avoid51* complications with running a destructor when the thread exits. Avoid adding52* other thread_local variables.53* @see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=27870154*/55static thread_local char g_thread_name[128]{'\0'};56std::string util::ThreadGetInternalName() { return g_thread_name; }57//! Set the in-memory internal name for this thread. Does not affect the process58//! name.59static void SetInternalName(const std::string& name)60{61const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())};62std::memcpy(g_thread_name, name.data(), copy_bytes);63g_thread_name[copy_bytes] = '\0';64}66void util::ThreadRename(const std::string& name)67{68Assume(name.size() <= 13); // Linux keeps 15 bytes69SetThreadName(("b-" + name).c_str());70SetInternalName(name);71}73void util::ThreadSetInternalName(const std::string& name)74{75SetInternalName(name);76}