rippled
Loading...
Searching...
No Matches
CurrentThreadName.cpp
1#include <xrpl/beast/core/CurrentThreadName.h>
2
3#include <boost/predef.h>
4
5#include <string>
6#include <string_view>
7
8//------------------------------------------------------------------------------
9
10#if BOOST_OS_WINDOWS
11#include <process.h>
12#include <windows.h>
13
14namespace beast::detail {
15
16inline void
17setCurrentThreadNameImpl(std::string_view name)
18{
19#if DEBUG && BOOST_COMP_MSVC
20 // This technique is documented by Microsoft and works for all versions
21 // of Windows and Visual Studio provided that the process is being run
22 // under the Visual Studio debugger. For more details, see:
23 // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
24
25#pragma pack(push, 8)
26 struct THREADNAME_INFO
27 {
28 DWORD dwType;
29 LPCSTR szName;
30 DWORD dwThreadID;
31 DWORD dwFlags;
32 };
33#pragma pack(pop)
34
35 THREADNAME_INFO ni;
36
37 ni.dwType = 0x1000;
38 ni.szName = name.data();
39 ni.dwThreadID = GetCurrentThreadId();
40 ni.dwFlags = 0;
41
42#pragma warning(push)
43#pragma warning(disable : 6320 6322)
44 __try
45 {
46 RaiseException(
47 0x406d1388, 0, sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni);
48 }
49 __except (EXCEPTION_CONTINUE_EXECUTION)
50 {
51 }
52#pragma warning(pop)
53#endif
54}
55
56} // namespace beast::detail
57#endif // BOOST_OS_WINDOWS
58
59#if BOOST_OS_MACOS
60#include <pthread.h>
61
62namespace beast::detail {
63
64inline void
65setCurrentThreadNameImpl(std::string_view name)
66{
67 pthread_setname_np(name.data());
68}
69
70} // namespace beast::detail
71#endif // BOOST_OS_MACOS
72
73#if BOOST_OS_LINUX
74#include <pthread.h>
75
76namespace beast::detail {
77
78inline void
79setCurrentThreadNameImpl(std::string_view name)
80{
81 pthread_setname_np(pthread_self(), name.data());
82}
83
84} // namespace beast::detail
85#endif // BOOST_OS_LINUX
86
87namespace beast {
88
89namespace detail {
91} // namespace detail
92
98
99void
101{
102 detail::threadName = name;
103 detail::setCurrentThreadNameImpl(name);
104}
105
106} // namespace beast
T data(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.