xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
beast
core
CurrentThreadName.cpp
1
#include <xrpl/beast/core/CurrentThreadName.h>
2
3
#include <
string
>
4
#include <
string_view
>
5
6
//------------------------------------------------------------------------------
7
8
#if BOOST_OS_WINDOWS
9
#include <process.h>
10
#include <windows.h>
11
12
namespace
beast::detail
{
13
14
inline
void
15
setCurrentThreadNameImpl(std::string_view name)
16
{
17
#if DEBUG && BOOST_COMP_MSVC
18
// This technique is documented by Microsoft and works for all versions
19
// of Windows and Visual Studio provided that the process is being run
20
// under the Visual Studio debugger. For more details, see:
21
// https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
22
23
#pragma pack(push, 8)
24
struct
THREADNAME_INFO
25
{
26
DWORD dwType;
27
LPCSTR szName;
28
DWORD dwThreadID;
29
DWORD dwFlags;
30
};
31
#pragma pack(pop)
32
33
THREADNAME_INFO ni;
34
35
ni.dwType = 0x1000;
36
ni.szName = name.
data
();
37
ni.dwThreadID = GetCurrentThreadId();
38
ni.dwFlags = 0;
39
40
#pragma warning(push)
41
#pragma warning(disable : 6320 6322)
42
__try
43
{
44
RaiseException(0x406d1388, 0,
sizeof
(ni) /
sizeof
(ULONG_PTR), (ULONG_PTR*)&ni);
45
}
46
__except (EXCEPTION_CONTINUE_EXECUTION)
47
{
48
}
49
#pragma warning(pop)
50
#endif
51
}
52
53
}
// namespace beast::detail
54
#endif
// BOOST_OS_WINDOWS
55
56
#if BOOST_OS_MACOS
57
#include <pthread.h>
58
59
namespace
beast::detail
{
60
61
inline
void
62
setCurrentThreadNameImpl(std::string_view name)
63
{
64
// The string is assumed to be null terminated
65
pthread_setname_np(name.
data
());
// NOLINT(bugprone-suspicious-stringview-data-usage)
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
>
// IWYU pragma: keep
75
76
namespace
beast::detail
{
77
78
inline
void
79
setCurrentThreadNameImpl(std::string_view name)
80
{
81
// truncate and set the thread name.
82
char
boundedName[kMaxThreadNameLength + 1];
83
auto
const
boundedSize =
84
name.
size
() < kMaxThreadNameLength ? name.
size
() : kMaxThreadNameLength;
85
name.
copy
(boundedName, boundedSize);
86
boundedName[boundedSize] =
'\0'
;
87
88
pthread_setname_np(pthread_self(), boundedName);
89
90
#ifdef TRUNCATED_THREAD_NAME_LOGS
91
if
(name.
size
() > kMaxThreadNameLength)
92
{
93
std::cerr <<
"WARNING: Thread name \""
<< name <<
"\" (length "
<< name.
size
()
94
<<
") exceeds maximum of "
<< kMaxThreadNameLength <<
" characters on Linux.\n"
;
95
}
96
#endif
97
}
98
99
}
// namespace beast::detail
100
#endif
// BOOST_OS_LINUX
101
102
namespace
beast
{
103
104
namespace
detail
{
105
thread_local
std::string
gThreadName
;
106
}
// namespace detail
107
108
std::string
109
getCurrentThreadName
()
110
{
111
return
detail::gThreadName
;
112
}
113
114
void
115
setCurrentThreadName
(
std::string_view
name)
116
{
117
detail::gThreadName
= name;
118
detail::setCurrentThreadNameImpl(name);
119
}
120
121
}
// namespace beast
std::string
std::string_view
std::string_view::copy
T copy(T... args)
std::string_view::data
T data(T... args)
iostream
beast::detail
Definition
abstract_clock.h:59
beast::detail::gThreadName
std::string gThreadName
Definition
CurrentThreadName.cpp:105
beast
Definition
base_uint.h:697
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
Definition
CurrentThreadName.cpp:115
beast::getCurrentThreadName
std::string getCurrentThreadName()
Returns the name of the caller thread.
Definition
CurrentThreadName.cpp:109
std::string_view::size
T size(T... args)
string
string_view
Generated by
1.17.0