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 <windows.h>
31 #include <process.h>
32 
33 namespace beast::detail {
34 
35 inline void setCurrentThreadNameImpl (std::string_view name)
36 {
37 #if DEBUG && BOOST_COMP_MSVC
38  // This technique is documented by Microsoft and works for all versions
39  // of Windows and Visual Studio provided that the process is being run
40  // under the Visual Studio debugger. For more details, see:
41  // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
42 
43 #pragma pack(push,8)
44  struct THREADNAME_INFO
45  {
46  DWORD dwType;
47  LPCSTR szName;
48  DWORD dwThreadID;
49  DWORD dwFlags;
50  };
51 #pragma pack(pop)
52 
53  THREADNAME_INFO ni;
54 
55  ni.dwType = 0x1000;
56  ni.szName = name.data();
57  ni.dwThreadID = GetCurrentThreadId();
58  ni.dwFlags = 0;
59 
60 #pragma warning(push)
61 #pragma warning(disable: 6320 6322)
62  __try
63  {
64  RaiseException (0x406d1388, 0,
65  sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni);
66  }
67  __except (EXCEPTION_CONTINUE_EXECUTION)
68  {}
69 #pragma warning(pop)
70 #endif
71 }
72 
73 } // beast::detail
74 #endif // BOOST_OS_WINDOWS
75 
76 #if BOOST_OS_MACOS
77 #include <pthread.h>
78 
79 namespace beast::detail {
80 
81 inline void setCurrentThreadNameImpl (std::string_view name)
82 {
83  pthread_setname_np(name.data());
84 }
85 
86 } // beast::detail
87 #endif // BOOST_OS_MACOS
88 
89 #if BOOST_OS_LINUX
90 #include <pthread.h>
91 
92 namespace beast::detail {
93 
94 inline void setCurrentThreadNameImpl (std::string_view name)
95 {
96  pthread_setname_np(pthread_self(), name.data());
97 }
98 
99 } // beast::detail
100 #endif // BOOST_OS_LINUX
101 
102 namespace beast {
103 
104 namespace detail {
105 thread_local std::string threadName;
106 } // detail
107 
109 {
110  return detail::threadName;
111 }
112 
114 {
115  detail::threadName = name;
116  detail::setCurrentThreadNameImpl(name);
117 }
118 
119 } // beast
std::string
STL class.
std::string_view
STL class.
beast::detail::threadName
thread_local std::string threadName
Definition: CurrentThreadName.cpp:105
beast::detail
Definition: abstract_clock.h:78
beast::getCurrentThreadName
std::string getCurrentThreadName()
Returns the name of the caller thread.
Definition: CurrentThreadName.cpp:108
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Definition: CurrentThreadName.cpp:113
std::string_view::data
T data(T... args)
beast
Definition: base_uint.h:582