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