xrpld
Loading...
Searching...
No Matches
CurrentThreadName.cpp
1#include <xrpl/beast/core/CurrentThreadName.h>
2
3#include <string>
4#include <string_view>
5
6//------------------------------------------------------------------------------
7
8#if BOOST_OS_WINDOWS
9#include <process.h>
10#include <windows.h>
11
12namespace beast::detail {
13
14inline void
15setCurrentThreadNameImpl(std::string_view name)
16{
17#if DEBUG && BOOST_COMP_MSVC
18 // This technique is documented by Microsoft and works for all versions
19 // of Windows and Visual Studio provided that the process is being run
20 // under the Visual Studio debugger. For more details, see:
21 // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
22
23#pragma pack(push, 8)
24 struct THREADNAME_INFO
25 {
26 DWORD dwType;
27 LPCSTR szName;
28 DWORD dwThreadID;
29 DWORD dwFlags;
30 };
31#pragma pack(pop)
32
33 THREADNAME_INFO ni;
34
35 ni.dwType = 0x1000;
36 ni.szName = name.data();
37 ni.dwThreadID = GetCurrentThreadId();
38 ni.dwFlags = 0;
39
40#pragma warning(push)
41#pragma warning(disable : 6320 6322)
42 __try
43 {
44 RaiseException(0x406d1388, 0, sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni);
45 }
46 __except (EXCEPTION_CONTINUE_EXECUTION)
47 {
48 }
49#pragma warning(pop)
50#endif
51}
52
53} // namespace beast::detail
54#endif // BOOST_OS_WINDOWS
55
56#if BOOST_OS_MACOS
57#include <pthread.h>
58
59namespace beast::detail {
60
61inline void
62setCurrentThreadNameImpl(std::string_view name)
63{
64 // The string is assumed to be null terminated
65 pthread_setname_np(name.data()); // NOLINT(bugprone-suspicious-stringview-data-usage)
66}
67
68} // namespace beast::detail
69#endif // BOOST_OS_MACOS
70
71#if BOOST_OS_LINUX
72#include <pthread.h>
73
74#include <iostream> // IWYU pragma: keep
75
76namespace beast::detail {
77
78inline void
79setCurrentThreadNameImpl(std::string_view name)
80{
81 // truncate and set the thread name.
82 char boundedName[kMaxThreadNameLength + 1];
83 auto const boundedSize =
84 name.size() < kMaxThreadNameLength ? name.size() : kMaxThreadNameLength;
85 name.copy(boundedName, boundedSize);
86 boundedName[boundedSize] = '\0';
87
88 pthread_setname_np(pthread_self(), boundedName);
89
90#ifdef TRUNCATED_THREAD_NAME_LOGS
91 if (name.size() > kMaxThreadNameLength)
92 {
93 std::cerr << "WARNING: Thread name \"" << name << "\" (length " << name.size()
94 << ") exceeds maximum of " << kMaxThreadNameLength << " characters on Linux.\n";
95 }
96#endif
97}
98
99} // namespace beast::detail
100#endif // BOOST_OS_LINUX
101
102namespace beast {
103
104namespace detail {
106} // namespace detail
107
113
114void
116{
117 detail::gThreadName = name;
118 detail::setCurrentThreadNameImpl(name);
119}
120
121} // namespace beast
T copy(T... args)
T data(T... args)
std::string gThreadName
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
std::string getCurrentThreadName()
Returns the name of the caller thread.
T size(T... args)