Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ThreadNaming.cpp
1 #include "ThreadNaming.hh"
2 
3 #include <Base/Debug/Error.hh>
4 #include <iostream>
5 #include <windows.h>
6 
7 /// author Jan Woetzel, 2005
8 
9 
10 #define MS_VC_EXCEPTION 0x406d1388
11 
12 // helper struct
13 typedef struct tagTHREADNAME_INFO
14 {
15  DWORD dwType; // must be 0x1000
16  LPCSTR szName; // pointer to name (in same addr space)
17  DWORD dwThreadID; // thread ID (-1 caller thread)
18  DWORD dwFlags; // reserved for future use, most be zero
19 } THREADNAME_INFO;
20 
21 
22 // JW public interface
23 void BIAS::SetThreadName(const unsigned long dwThreadID, const char* szThreadName )
24 {
25  if (szThreadName==NULL)
26  return; // invalid name
27 
28  // TODO: raise exception only when running in debugger (at runtime!)
29  // and additionally raise only in debug mode
30  // HACK
31  //BIASWARN("Thread naming temporarily disabled to avoid Exception thread on running without debug heap. dwThreadID="<<dwThreadID);
32  //return;
33 
34 #if !defined(COMPILE_DEBUG) || !defined(BIAS_DEBUG)
35  BIASWARN("Thread naming (temporarily) disabled to avoid throwing an exception when running without debug heap. dwThreadID="<<dwThreadID);
36  return; // do not raise exception if running without debug heap.
37 #else
38 
39  THREADNAME_INFO info;
40  info.dwType = 0x1000;
41  info.szName = szThreadName;
42  info.dwThreadID = dwThreadID;
43  info.dwFlags = 0;
44 
45  try {
46  // post an exception to Visual studio to change the thread name. (JW)
47  RaiseException(
48  MS_VC_EXCEPTION,
49  0,
50  sizeof(info) / sizeof(DWORD),
51 #ifdef _WIN64
52  (ULONG_PTR *)&info
53 #else
54  (DWORD *)&info
55 #endif //_WIN64
56  );
57  }
58  catch (...)
59  {
60  std::cerr<<__FUNCTION__<<"not working."<<std::endl;
61 # if defined(BIAS_DEBUG) && defined(COMPILE_DEBUG)
62  DebugBreak();
63 # endif
64  }
65 #endif
66 }
67