MRPT  2.0.4
thread_name.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 //
12 #include <mrpt/config.h>
13 #include <mrpt/core/exceptions.h>
15 
16 #if defined(MRPT_OS_WINDOWS)
17 #include <windows.h>
18 
19 #include <cwchar>
20 #include <vector>
21 
22 #elif defined(MRPT_OS_LINUX)
23 #include <sys/prctl.h>
24 
25 static void SetThreadName(std::thread& thread, const char* threadName)
26 {
27  auto handle = thread.native_handle();
28  pthread_setname_np(handle, threadName);
29 }
30 
31 static std::string GetThreadName(std::thread& thread)
32 {
33  auto handle = thread.native_handle();
34  char buf[1000];
35  buf[0] = '\0';
36  pthread_getname_np(handle, buf, sizeof(buf));
37  return std::string(buf);
38 }
39 
40 static void SetThreadName(const char* threadName)
41 {
42  prctl(PR_SET_NAME, threadName, 0L, 0L, 0L);
43 }
44 static std::string GetThreadName()
45 {
46  char buf[100] = {0};
47  prctl(PR_GET_NAME, buf, 0L, 0L, 0L);
48  return std::string(buf);
49 }
50 #endif
51 
52 void mrpt::system::thread_name(const std::string& name)
53 {
54 #if defined(MRPT_OS_WINDOWS)
55  wchar_t wName[50];
56  std::mbstowcs(wName, name.c_str(), sizeof(wName) / sizeof(wName[0]));
57  SetThreadDescription(GetCurrentThread(), wName);
58 #elif defined(MRPT_OS_LINUX)
59  SetThreadName(name.c_str());
60 #endif
61 }
62 
63 void mrpt::system::thread_name(const std::string& name, std::thread& theThread)
64 {
65 #if defined(MRPT_OS_WINDOWS)
66  wchar_t wName[50];
67  std::mbstowcs(wName, name.c_str(), sizeof(wName) / sizeof(wName[0]));
68  SetThreadDescription(theThread.native_handle(), wName);
69 #elif defined(MRPT_OS_LINUX)
70  SetThreadName(theThread, name.c_str());
71 #endif
72 }
73 
74 #if defined(MRPT_OS_WINDOWS)
75 static std::string w2cstr(wchar_t** wstrnc)
76 {
77  const wchar_t** wstr = const_cast<const wchar_t**>(wstrnc);
78 
79  std::mbstate_t state = std::mbstate_t();
80  std::size_t len = 1 + std::wcsrtombs(nullptr, wstr, 0, &state);
81  std::vector<char> mbstr(len);
82  std::wcsrtombs(&mbstr[0], wstr, mbstr.size(), &state);
83  return std::string(mbstr.data());
84 }
85 #endif
86 
88 {
89 #if defined(MRPT_OS_WINDOWS)
90  std::string ret = "NoName";
91  PWSTR str;
92  HRESULT hr = GetThreadDescription(GetCurrentThread(), &str);
93  if (SUCCEEDED(hr))
94  {
95  ret = w2cstr(reinterpret_cast<wchar_t**>(&str));
96  LocalFree(str);
97  }
98  return ret;
99 #elif defined(MRPT_OS_LINUX)
100  return GetThreadName();
101 #else
102  return std::string("");
103 #endif
104 }
105 
106 std::string mrpt::system::thread_name(std::thread& theThread)
107 {
108 #if defined(MRPT_OS_WINDOWS)
109  std::string ret = "NoName";
110  PWSTR str;
111  HRESULT hr = GetThreadDescription(theThread.native_handle(), &str);
112  if (SUCCEEDED(hr))
113  {
114  ret = w2cstr(reinterpret_cast<wchar_t**>(&str));
115  LocalFree(str);
116  }
117  return ret;
118 #elif defined(MRPT_OS_LINUX)
119  return GetThreadName(theThread);
120 #else
121  return std::string("");
122 #endif
123 }
void thread_name(const std::string &name, std::thread &theThread)
Sets the name of the given thread; useful for debuggers.
Definition: thread_name.cpp:63



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020