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(
46 0x406d1388, 0, sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni);
47 }
48 __except (EXCEPTION_CONTINUE_EXECUTION)
49 {
50 }
51#pragma warning(pop)
52#endif
53}
54
55} // namespace beast::detail
56#endif // BOOST_OS_WINDOWS
57
58#if BOOST_OS_MACOS
59#include <pthread.h>
60
61namespace beast::detail {
62
63inline void
64setCurrentThreadNameImpl(std::string_view name)
65{
66 pthread_setname_np(name.data());
67}
68
69} // namespace beast::detail
70#endif // BOOST_OS_MACOS
71
72#if BOOST_OS_LINUX
73#include <pthread.h>
74
75#include <iostream>
76
77namespace beast::detail {
78
79inline void
80setCurrentThreadNameImpl(std::string_view name)
81{
82 // truncate and set the thread name.
83 char boundedName[maxThreadNameLength + 1];
85 boundedName,
86 sizeof(boundedName),
87 "%.*s",
88 static_cast<int>(maxThreadNameLength),
89 name.data());
90
91 pthread_setname_np(pthread_self(), boundedName);
92
93#ifdef TRUNCATED_THREAD_NAME_LOGS
94 if (name.size() > maxThreadNameLength)
95 {
96 std::cerr << "WARNING: Thread name \"" << name << "\" (length "
97 << name.size() << ") exceeds maximum of "
98 << maxThreadNameLength << " characters on Linux.\n";
99
100 XRPL_ASSERT(
101 false,
102 "beast::detail::setCurrentThreadNameImpl : Thread name exceeds "
103 "maximum length for Linux");
104 }
105#endif
106}
107
108} // namespace beast::detail
109#endif // BOOST_OS_LINUX
110
111namespace beast {
112
113namespace detail {
115} // namespace detail
116
122
123void
125{
126 detail::threadName = name;
127 detail::setCurrentThreadNameImpl(name);
128}
129
130} // 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)