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