Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
24static 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__))
30#elif defined(MAC_OSX)
32#else
33 // Prevent warnings for unused parameters...
34 (void)name;
35#endif
36}
37
38static thread_local std::string g_thread_name;
39const std::string &util::ThreadGetInternalName() {
40 return g_thread_name;
41}
44static void SetInternalName(std::string name) {
45 g_thread_name = std::move(name);
46}
47
48void util::ThreadRename(std::string &&name) {
49 SetThreadName(("b-" + name).c_str());
50 SetInternalName(std::move(name));
51}
52
53void util::ThreadSetInternalName(std::string &&name) {
54 SetInternalName(std::move(name));
55}
void ThreadSetInternalName(std::string &&)
Set the internal (in-memory) name of the current thread only.
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * name
Definition rest.cpp:47
static thread_local std::string g_thread_name
static void SetInternalName(std::string name)
Set the in-memory internal name for this thread.
static void SetThreadName(const char *name)
Set the thread's name at the process level.