Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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#include <util/check.h>
7
8#include <algorithm>
9#include <cstring>
10#include <string>
11
12#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
13#include <pthread.h>
14#include <pthread_np.h>
15#endif
16
17#if __has_include(<sys/prctl.h>)
18#include <sys/prctl.h>
19#endif
20
21#ifdef WIN32
22#include <windows.h>
23#endif
24
27static void SetThreadName(const char* name)
28{
29#if defined(PR_SET_NAME)
30 // Only the first 15 characters are used (16 - NUL terminator)
31 ::prctl(PR_SET_NAME, name, 0, 0, 0);
32#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
34#elif defined(__APPLE__)
36#elif defined(WIN32)
37 // Thread names are ASCII-only, so widening each character is sufficient as
38 // a conversion to UTF-16.
39 const std::wstring wname{name, name + std::strlen(name)};
41#else
42 // Prevent warnings for unused parameters...
43 (void)name;
44#endif
45}
46
53static thread_local char g_thread_name[128]{'\0'};
57static void SetInternalName(const std::string& name)
58{
59 const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())};
60 std::memcpy(g_thread_name, name.data(), copy_bytes);
62}
63
64void util::ThreadRename(const std::string& name)
65{
66 Assume(name.size() <= 13); // Linux keeps 15 bytes
67 SetThreadName(("b-" + name).c_str());
69}
70
71void util::ThreadSetInternalName(const std::string& name)
72{
74}
#define Assume(val)
Assume is the identity function.
Definition check.h:131
void ThreadRename(const std::string &)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
std::string ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
void ThreadSetInternalName(const std::string &)
Set the internal (in-memory) name of the current thread only.
const char * name
Definition rest.cpp:71
static thread_local char g_thread_name[128]
The name of the thread.
static void SetInternalName(const 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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82