Bitcoin Core 31.99.0
P2P Digital Currency
threadnames.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-present 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#include <util/threadnames.h>
6
7#include <algorithm>
8#include <cstring>
9#include <string>
10
11#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
12#include <pthread.h>
13#include <pthread_np.h>
14#endif
15
16#if __has_include(<sys/prctl.h>)
17#include <sys/prctl.h>
18#endif
19
22static void SetThreadName(const char* name)
23{
24#if defined(PR_SET_NAME)
25 // Only the first 15 characters are used (16 - NUL terminator)
26 ::prctl(PR_SET_NAME, name, 0, 0, 0);
27#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
28 pthread_set_name_np(pthread_self(), name);
29#elif defined(__APPLE__)
30 pthread_setname_np(name);
31#else
32 // Prevent warnings for unused parameters...
33 (void)name;
34#endif
35}
36
43static thread_local char g_thread_name[128]{'\0'};
47static void SetInternalName(const std::string& name)
48{
49 const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())};
50 std::memcpy(g_thread_name, name.data(), copy_bytes);
51 g_thread_name[copy_bytes] = '\0';
52}
53
54void util::ThreadRename(const std::string& name)
55{
56 SetThreadName(("b-" + name).c_str());
58}
59
60void util::ThreadSetInternalName(const std::string& name)
61{
63}
void ThreadRename(const std::string &)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:54
std::string ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:44
void ThreadSetInternalName(const std::string &)
Set the internal (in-memory) name of the current thread only.
Definition: threadnames.cpp:60
const char * name
Definition: rest.cpp:49
static thread_local char g_thread_name[128]
The name of the thread.
Definition: threadnames.cpp:43
static void SetInternalName(const std::string &name)
Set the in-memory internal name for this thread.
Definition: threadnames.cpp:47
static void SetThreadName(const char *name)
Set the thread's name at the process level.
Definition: threadnames.cpp:22