Bitcoin Core 22.99.0
P2P Digital Currency
threadnames.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2019 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)
7#endif
8
9#include <thread>
10
11#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
12#include <pthread.h>
13#include <pthread_np.h>
14#endif
15
16#include <util/threadnames.h>
17
18#ifdef HAVE_SYS_PRCTL_H
19#include <sys/prctl.h> // For prctl, PR_SET_NAME, PR_GET_NAME
20#endif
21
24static void SetThreadName(const char* name)
25{
26#if defined(PR_SET_NAME)
27 // Only the first 15 characters are used (16 - NUL terminator)
28 ::prctl(PR_SET_NAME, name, 0, 0, 0);
29#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
30 pthread_set_name_np(pthread_self(), name);
31#elif defined(MAC_OSX)
32 pthread_setname_np(name);
33#else
34 // Prevent warnings for unused parameters...
35 (void)name;
36#endif
37}
38
39// If we have thread_local, just keep thread ID and name in a thread_local
40// global.
41#if defined(HAVE_THREAD_LOCAL)
42
43static thread_local std::string g_thread_name;
44const std::string& util::ThreadGetInternalName() { return g_thread_name; }
47static void SetInternalName(std::string name) { g_thread_name = std::move(name); }
48
49// Without thread_local available, don't handle internal name at all.
50#else
51
52static const std::string empty_string;
53const std::string& util::ThreadGetInternalName() { return empty_string; }
54static void SetInternalName(std::string name) { }
55#endif
56
57void util::ThreadRename(std::string&& name)
58{
59 SetThreadName(("b-" + name).c_str());
60 SetInternalName(std::move(name));
61}
62
64{
65 SetInternalName(std::move(name));
66}
void ThreadSetInternalName(std::string &&)
Set the internal (in-memory) name of the current thread only.
Definition: threadnames.cpp:63
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:53
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:57
const char * name
Definition: rest.cpp:43
static void SetInternalName(std::string name)
Definition: threadnames.cpp:54
static const std::string empty_string
Definition: threadnames.cpp:52
static void SetThreadName(const char *name)
Set the thread's name at the process level.
Definition: threadnames.cpp:24