Bitcoin ABC  0.24.7
P2P Digital Currency
threadnames.cpp
Go to the documentation of this file.
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 <util/threadnames.h>
10 
11 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
12 #include <pthread.h>
13 #include <pthread_np.h>
14 #endif
15 
16 #include <thread>
17 
18 #ifdef HAVE_SYS_PRCTL_H
19 #include <sys/prctl.h> // For prctl, PR_SET_NAME, PR_GET_NAME
20 #endif
21 
24 static void SetThreadName(const char *name) {
25 #if defined(PR_SET_NAME)
26  // Only the first 15 characters are used (16 - NUL terminator)
27  ::prctl(PR_SET_NAME, name, 0, 0, 0);
28 #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
29  pthread_set_name_np(pthread_self(), name);
30 #elif defined(MAC_OSX)
31  pthread_setname_np(name);
32 #else
33  // Prevent warnings for unused parameters...
34  (void)name;
35 #endif
36 }
37 
38 static thread_local std::string g_thread_name;
39 const std::string &util::ThreadGetInternalName() {
40  return g_thread_name;
41 }
44 static void SetInternalName(std::string name) {
45  g_thread_name = std::move(name);
46 }
47 
48 void util::ThreadRename(std::string &&name) {
49  SetThreadName(("b-" + name).c_str());
50  SetInternalName(std::move(name));
51 }
52 
53 void util::ThreadSetInternalName(std::string &&name) {
54  SetInternalName(std::move(name));
55 }
SetThreadName
static void SetThreadName(const char *name)
Set the thread's name at the process level.
Definition: threadnames.cpp:24
util::ThreadGetInternalName
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:39
util::ThreadRename
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:48
SetInternalName
static void SetInternalName(std::string name)
Set the in-memory internal name for this thread.
Definition: threadnames.cpp:44
name
const char * name
Definition: rest.cpp:43
g_thread_name
static thread_local std::string g_thread_name
Definition: threadnames.cpp:38
threadnames.h
util::ThreadSetInternalName
void ThreadSetInternalName(std::string &&)
Set the internal (in-memory) name of the current thread only.
Definition: threadnames.cpp:53