rippled
Loading...
Searching...
No Matches
CurrentThreadName.cpp
1#include <xrpl/beast/core/CurrentThreadName.h>
2#include <xrpl/beast/utility/instrumentation.h>
3
4#include <string>
5#include <string_view>
6
7//------------------------------------------------------------------------------
8
9#if BOOST_OS_WINDOWS
10#include <process.h>
11#include <windows.h>
12
13namespace beast::detail {
14
15inline void
16setCurrentThreadNameImpl(std::string_view name)
17{
18#if DEBUG && BOOST_COMP_MSVC
19 // This technique is documented by Microsoft and works for all versions
20 // of Windows and Visual Studio provided that the process is being run
21 // under the Visual Studio debugger. For more details, see:
22 // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
23
24#pragma pack(push, 8)
25 struct THREADNAME_INFO
26 {
27 DWORD dwType;
28 LPCSTR szName;
29 DWORD dwThreadID;
30 DWORD dwFlags;
31 };
32#pragma pack(pop)
33
34 THREADNAME_INFO ni;
35
36 ni.dwType = 0x1000;
37 ni.szName = name.data();
38 ni.dwThreadID = GetCurrentThreadId();
39 ni.dwFlags = 0;
40
41#pragma warning(push)
42#pragma warning(disable : 6320 6322)
43 __try
44 {
45 RaiseException(0x406d1388, 0, sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni);
46 }
47 __except (EXCEPTION_CONTINUE_EXECUTION)
48 {
49 }
50#pragma warning(pop)
51#endif
52}
53
54} // namespace beast::detail
55#endif // BOOST_OS_WINDOWS
56
57#if BOOST_OS_MACOS
58#include <pthread.h>
59
60namespace beast::detail {
61
62inline void
63setCurrentThreadNameImpl(std::string_view name)
64{
65 pthread_setname_np(name.data());
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>
75
76namespace beast::detail {
77
78inline void
79setCurrentThreadNameImpl(std::string_view name)
80{
81 // truncate and set the thread name.
82 char boundedName[maxThreadNameLength + 1];
84 boundedName,
85 sizeof(boundedName),
86 "%.*s",
87 static_cast<int>(maxThreadNameLength),
88 name.data());
89
90 pthread_setname_np(pthread_self(), boundedName);
91
92#ifdef TRUNCATED_THREAD_NAME_LOGS
93 if (name.size() > maxThreadNameLength)
94 {
95 std::cerr << "WARNING: Thread name \"" << name << "\" (length " << name.size()
96 << ") exceeds maximum of " << maxThreadNameLength << " characters on Linux.\n";
97
98 XRPL_ASSERT(
99 false,
100 "beast::detail::setCurrentThreadNameImpl : Thread name exceeds "
101 "maximum length for Linux");
102 }
103#endif
104}
105
106} // namespace beast::detail
107#endif // BOOST_OS_LINUX
108
109namespace beast {
110
111namespace detail {
113} // namespace detail
114
120
121void
123{
124 detail::threadName = name;
125 detail::setCurrentThreadNameImpl(name);
126}
127
128} // namespace beast
T data(T... args)
T snprintf(T... args)
thread_local std::string threadName
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)