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];
83 std::snprintf(boundedName, sizeof(boundedName), "%.*s", static_cast<int>(maxThreadNameLength), name.data());
84
85 pthread_setname_np(pthread_self(), boundedName);
86
87#ifdef TRUNCATED_THREAD_NAME_LOGS
88 if (name.size() > maxThreadNameLength)
89 {
90 std::cerr << "WARNING: Thread name \"" << name << "\" (length " << name.size() << ") exceeds maximum of "
91 << maxThreadNameLength << " characters on Linux.\n";
92
93 XRPL_ASSERT(
94 false,
95 "beast::detail::setCurrentThreadNameImpl : Thread name exceeds "
96 "maximum length for Linux");
97 }
98#endif
99}
100
101} // namespace beast::detail
102#endif // BOOST_OS_LINUX
103
104namespace beast {
105
106namespace detail {
108} // namespace detail
109
115
116void
118{
119 detail::threadName = name;
120 detail::setCurrentThreadNameImpl(name);
121}
122
123} // 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)