ezEngine  Milestone 7
OSThread_win.h
1 
2 #ifdef EZ_OSTHREAD_WIN_INL_H_INCLUDED
3  #error This file must not be included twice.
4 #endif
5 
6 #define EZ_OSTHREAD_WIN_INL_H_INCLUDED
7 
9 
10 // Deactivate Doxygen document generation for the following block.
12 
13 // Exception used to set a thread name
14 // See: http://blogs.msdn.com/b/stevejs/archive/2005/12/19/505815.aspx for more details
15 const DWORD MS_VC_EXCEPTION = 0x406D1388;
16 
17 // This structure describes a thread name set exception
18 #pragma pack(push,8)
19 typedef struct tagTHREADNAME_INFO
20 {
21  DWORD dwType; // Must be 0x1000.
22  LPCSTR szName; // Pointer to name (in user addr space).
23  DWORD dwThreadID; // Thread ID (-1=caller thread).
24  DWORD dwFlags; // Reserved for future use, must be zero.
25 } THREADNAME_INFO;
26 #pragma pack(pop)
27 
28 #define EZ_MSVC_WARNING_NUMBER 6312
29 #include <Foundation/Basics/Compiler/DisableWarning.h>
30 
31 void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
32 {
33  THREADNAME_INFO info;
34  info.dwType = 0x1000;
35  info.szName = szThreadName;
36  info.dwThreadID = dwThreadID;
37  info.dwFlags = 0;
38 
39  __try
40  {
41  RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (ULONG_PTR*)&info);
42  }
43  __except(EXCEPTION_CONTINUE_EXECUTION)
44  {
45  return; // makes the static code analysis happy
46  }
47 }
48 
49 #include <Foundation/Basics/Compiler/RestoreWarning.h>
50 
52 
53 
54 // Windows specific implementation of the thread class
55 
56 ezOSThread::ezOSThread(ezOSThreadEntryPoint pThreadEntryPoint, void* pUserData /*= nullptr*/, const char* szName /*= "ezThread"*/, ezUInt32 uiStackSize /*= 128 * 1024*/)
57 {
59 
60  m_Handle = CreateThread(nullptr, uiStackSize, pThreadEntryPoint, pUserData, CREATE_SUSPENDED, nullptr);
61  EZ_ASSERT_RELEASE(m_Handle != INVALID_HANDLE_VALUE, "Thread creation failed!");
62  EZ_ASSERT_RELEASE(m_Handle != nullptr, "Thread creation failed!"); // makes the static code analysis happy
63 
64  m_ThreadID = GetThreadId(m_Handle);
65 
66  m_EntryPoint = pThreadEntryPoint;
67  m_pUserData = pUserData;
68  m_szName = szName;
69  m_uiStackSize = uiStackSize;
70 
71  // If a name is given, assign it here
72  if (szName != nullptr)
73  {
74  SetThreadName(GetThreadId(m_Handle), szName);
75  }
76 
77 }
78 
80 {
81  CloseHandle(m_Handle);
82 
84 }
85 
87 void ezOSThread::Start()
88 {
89  ResumeThread(m_Handle);
90 }
91 
93 void ezOSThread::Join()
94 {
95  WaitForSingleObject(m_Handle, INFINITE);
96 }
97