rippled
Loading...
Searching...
No Matches
CurrentThreadName.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Portions of this file are from JUCE.
7 Copyright (c) 2013 - Raw Material Software Ltd.
8 Please visit http://www.juce.com
9
10 Permission to use, copy, modify, and/or distribute this software for any
11 purpose with or without fee is hereby granted, provided that the above
12 copyright notice and this permission notice appear in all copies.
13
14 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21*/
22//==============================================================================
23
24#include <xrpl/beast/core/CurrentThreadName.h>
25#include <boost/predef.h>
26
27//------------------------------------------------------------------------------
28
29#if BOOST_OS_WINDOWS
30#include <process.h>
31#include <windows.h>
32
33namespace beast::detail {
34
35inline void
36setCurrentThreadNameImpl(std::string_view name)
37{
38#if DEBUG && BOOST_COMP_MSVC
39 // This technique is documented by Microsoft and works for all versions
40 // of Windows and Visual Studio provided that the process is being run
41 // under the Visual Studio debugger. For more details, see:
42 // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
43
44#pragma pack(push, 8)
45 struct THREADNAME_INFO
46 {
47 DWORD dwType;
48 LPCSTR szName;
49 DWORD dwThreadID;
50 DWORD dwFlags;
51 };
52#pragma pack(pop)
53
54 THREADNAME_INFO ni;
55
56 ni.dwType = 0x1000;
57 ni.szName = name.data();
58 ni.dwThreadID = GetCurrentThreadId();
59 ni.dwFlags = 0;
60
61#pragma warning(push)
62#pragma warning(disable : 6320 6322)
63 __try
64 {
65 RaiseException(
66 0x406d1388, 0, sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni);
67 }
68 __except (EXCEPTION_CONTINUE_EXECUTION)
69 {
70 }
71#pragma warning(pop)
72#endif
73}
74
75} // namespace beast::detail
76#endif // BOOST_OS_WINDOWS
77
78#if BOOST_OS_MACOS
79#include <pthread.h>
80
81namespace beast::detail {
82
83inline void
84setCurrentThreadNameImpl(std::string_view name)
85{
86 pthread_setname_np(name.data());
87}
88
89} // namespace beast::detail
90#endif // BOOST_OS_MACOS
91
92#if BOOST_OS_LINUX
93#include <pthread.h>
94
95namespace beast::detail {
96
97inline void
98setCurrentThreadNameImpl(std::string_view name)
99{
100 pthread_setname_np(pthread_self(), name.data());
101}
102
103} // namespace beast::detail
104#endif // BOOST_OS_LINUX
105
106namespace beast {
107
108namespace detail {
110} // namespace detail
111
114{
115 return detail::threadName;
116}
117
118void
120{
121 detail::threadName = name;
122 detail::setCurrentThreadNameImpl(name);
123}
124
125} // 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.