rippled
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 <ripple/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 
33 namespace beast::detail {
34 
35 inline void
36 setCurrentThreadNameImpl(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 
81 namespace beast::detail {
82 
83 inline void
84 setCurrentThreadNameImpl(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 
95 namespace beast::detail {
96 
97 inline void
98 setCurrentThreadNameImpl(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 
106 namespace beast {
107 
108 namespace detail {
109 thread_local std::string threadName;
110 } // namespace detail
111 
114 {
115  return detail::threadName;
116 }
117 
118 void
120 {
121  detail::threadName = name;
122  detail::setCurrentThreadNameImpl(name);
123 }
124 
125 } // namespace beast
std::string
STL class.
std::string_view
STL class.
beast::detail::threadName
thread_local std::string threadName
Definition: CurrentThreadName.cpp:109
beast::detail
Definition: abstract_clock.h:79
beast::getCurrentThreadName
std::string getCurrentThreadName()
Returns the name of the caller thread.
Definition: CurrentThreadName.cpp:113
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Definition: CurrentThreadName.cpp:119
std::string::data
T data(T... args)
beast
Definition: base_uint.h:641