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 // The string is assumed to be null terminated
66 pthread_setname_np(name.data()); // NOLINT(bugprone-suspicious-stringview-data-usage)
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()); // NOLINT(bugprone-suspicious-stringview-data-usage)
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 " << name.size()
97 << ") exceeds maximum of " << maxThreadNameLength << " characters on Linux.\n";
98 }
99#endif
100}
101
102} // namespace beast::detail
103#endif // BOOST_OS_LINUX
104
105namespace beast {
106
107namespace detail {
109} // namespace detail
110
116
117void
119{
120 detail::threadName = name;
121 detail::setCurrentThreadNameImpl(name);
122}
123
124} // 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)