Utilities
Utilities header only library
Threading.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_THREADING_H
7 #define UTILS_THREADING_H
8 
9 #include <string>
10 
11 #ifdef _WIN32
12 #include <Windows.h>
13 #endif // _WIN32
14 
15 
16 namespace utils
17 {
18 
19 #if defined _WIN32 && defined _MSC_VER
20 
21 #include <windows.h>
22 
23  const DWORD MS_VC_EXCEPTION = 0x406D1388;
24 
25 #pragma pack(push,8)
26  typedef struct tagTHREADNAME_INFO
27  {
28  DWORD dwType; // Must be 0x1000.
29  LPCSTR szName; // Pointer to name (in user addr space).
30  DWORD dwThreadID; // Thread ID (-1=caller thread).
31  DWORD dwFlags; // Reserved for future use, must be zero.
32  } THREADNAME_INFO;
33 #pragma pack(pop)
34 
39  inline void SetThreadName(DWORD dwThreadID, const char *threadName)
40  {
41  THREADNAME_INFO info;
42  info.dwType = 0x1000;
43  info.szName = threadName;
44  info.dwThreadID = dwThreadID;
45  info.dwFlags = 0;
46 #pragma warning(push)
47 #pragma warning(disable: 6320 6322)
48  __try
49  {
50  RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)& info);
51  }
52  __except (EXCEPTION_EXECUTE_HANDLER)
53  {
54  }
55 #pragma warning(pop)
56  }
57 
62  inline void SetThisThreadName(const std::string &threadName)
63  {
64  SetThreadName(GetCurrentThreadId(), threadName.c_str());
65  }
66 
67 #elif __linux__
68  inline void SetThreadName(long, const char *)
70  {
71  // can not name a thread on Linux
72  }
73 
75  inline void SetThisThreadName(const std::string &)
76  {
77  // can not name a thread on Linux
78  }
79 
80 #else
81 #error "platform not supported"
82 #endif
83 
84 }
85 
86 #endif /* !UTILS_THREADING_H */
General namespace for utilities.
Definition: BulletConversions.h:9