|
| 1 | +// Copyright (c) 2018 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#if defined(HAVE_CONFIG_H) |
| 6 | +#include <config/bitcoin-config.h> |
| 7 | +#endif |
| 8 | + |
| 9 | +#include <atomic> |
| 10 | +#include <thread> |
| 11 | + |
| 12 | +#include <threadutil.h> |
| 13 | + |
| 14 | +#ifdef HAVE_SYS_PRCTL_H |
| 15 | +#include <sys/prctl.h> // For prctl, PR_SET_NAME, PR_GET_NAME |
| 16 | +#endif |
| 17 | + |
| 18 | +//! Set the thread's name at the process level. Does not affect the |
| 19 | +//! internal name. |
| 20 | +static void SetThreadName(const char* name) |
| 21 | +{ |
| 22 | +#if defined(PR_SET_NAME) |
| 23 | + // Only the first 15 characters are used (16 - NUL terminator) |
| 24 | + ::prctl(PR_SET_NAME, name, 0, 0, 0); |
| 25 | +#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) |
| 26 | + pthread_set_name_np(pthread_self(), name); |
| 27 | +#elif defined(MAC_OSX) |
| 28 | + pthread_setname_np(name); |
| 29 | +#else |
| 30 | + // Prevent warnings for unused parameters... |
| 31 | + (void)name; |
| 32 | +#endif |
| 33 | +} |
| 34 | + |
| 35 | +// If we have thread_local, just keep thread ID and name in a thread_local |
| 36 | +// global. |
| 37 | +#if defined(HAVE_THREAD_LOCAL) |
| 38 | + |
| 39 | +static thread_local std::string g_thread_name; |
| 40 | +std::string thread_util::GetInternalName() { return g_thread_name; } |
| 41 | +//! Set the in-memory internal name for this thread. Does not affect the process |
| 42 | +//! name. |
| 43 | +static void SetInternalName(const std::string& name) { g_thread_name = name; } |
| 44 | + |
| 45 | +// Without thread_local available, don't handle internal name at all. |
| 46 | +#else |
| 47 | + |
| 48 | +std::string thread_util::GetInternalName() { return ""; } |
| 49 | +static void SetInternalName(const std::string& name) { } |
| 50 | +#endif |
| 51 | + |
| 52 | +void thread_util::Rename(const std::string& name) |
| 53 | +{ |
| 54 | + SetThreadName(("bitcoin-" + name).c_str()); |
| 55 | + SetInternalName(name); |
| 56 | +} |
0 commit comments