1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 #pragma once 23 24 #ifndef __MT_THREAD__ 25 #define __MT_THREAD__ 26 27 #include <Platform/Common/MTThread.h> 28 29 namespace MT 30 { 31 32 class ThreadId 33 { 34 MW_DWORD id; 35 Atomic32<uint32> isInitialized; 36 37 public: 38 39 MT_NOCOPYABLE(ThreadId); 40 41 ThreadId() 42 { 43 isInitialized.Store(0); 44 } 45 46 void SetAsCurrentThread() 47 { 48 id = ::GetCurrentThreadId(); 49 isInitialized.Store(1); 50 } 51 52 bool IsCurrentThread() const 53 { 54 if (isInitialized.Load() == 0) 55 { 56 return false; 57 } 58 59 MW_DWORD callThread = ::GetCurrentThreadId(); 60 if (callThread == id) 61 { 62 return true; 63 } 64 return false; 65 } 66 }; 67 68 69 class Thread : public ThreadBase 70 { 71 MW_HANDLE thread; 72 73 static MW_DWORD __stdcall ThreadFuncInternal(void *pThread) 74 { 75 Thread* self = (Thread*)pThread; 76 self->func(self->funcData); 77 return 0; 78 } 79 80 81 82 static int GetPriority(ThreadPriority::Type priority) 83 { 84 switch(priority) 85 { 86 case ThreadPriority::DEFAULT: 87 return MW_THREAD_PRIORITY_HIGHEST; 88 case ThreadPriority::HIGH: 89 return MW_THREAD_PRIORITY_NORMAL; 90 case ThreadPriority::LOW: 91 return MW_THREAD_PRIORITY_LOWEST; 92 default: 93 MT_REPORT_ASSERT("Invalid thread priority"); 94 } 95 96 return MW_THREAD_PRIORITY_NORMAL; 97 } 98 99 100 public: 101 102 Thread() 103 : thread(nullptr) 104 { 105 } 106 107 ~Thread() 108 { 109 MT_ASSERT(thread == nullptr, "Thread is not stopped!"); 110 } 111 112 void Start(size_t stackSize, TThreadEntryPoint entryPoint, void *userData, uint32 cpuCore = MT_CPUCORE_ANY, ThreadPriority::Type priority = ThreadPriority::DEFAULT) 113 { 114 MT_ASSERT(thread == nullptr, "Thread already started"); 115 116 func = entryPoint; 117 funcData = userData; 118 thread = ::CreateThread( nullptr, stackSize, ThreadFuncInternal, this, MW_CREATE_SUSPENDED, nullptr ); 119 MT_ASSERT(thread != nullptr, "Can't create thread"); 120 121 if (cpuCore == MT_CPUCORE_ANY) 122 { 123 cpuCore = MW_MAXIMUM_PROCESSORS; 124 } 125 MT_VERIFY((cpuCore >= 0 && cpuCore < (uint32)GetNumberOfHardwareThreads()) || cpuCore == MW_MAXIMUM_PROCESSORS, "Invalid cpu core specified", cpuCore=MW_MAXIMUM_PROCESSORS); 126 MW_DWORD res = ::SetThreadIdealProcessor(thread, cpuCore); 127 MT_USED_IN_ASSERT(res); 128 MT_ASSERT(res != (MW_DWORD)-1, "SetThreadIdealProcessor failed!"); 129 130 int sched_priority = GetPriority(priority); 131 132 MW_BOOL result = ::SetThreadPriority(thread, sched_priority); 133 MT_USED_IN_ASSERT(result); 134 MT_ASSERT(result != 0, "SetThreadPriority failed!"); 135 136 res = ::ResumeThread(thread); 137 MT_USED_IN_ASSERT(res); 138 MT_ASSERT(res != (MW_DWORD)-1, "ResumeThread failed!"); 139 } 140 141 void Join() 142 { 143 if (thread == nullptr) 144 { 145 return; 146 } 147 148 ::WaitForSingleObject(thread, MW_INFINITE); 149 MW_BOOL res = CloseHandle(thread); 150 MT_USED_IN_ASSERT(res); 151 MT_ASSERT(res != 0, "Can't close thread handle"); 152 thread = nullptr; 153 } 154 155 #ifdef MT_INSTRUMENTED_BUILD 156 static void SetThreadName(const char* threadName) 157 { 158 const int MW_EXCEPTION_EXECUTE_HANDLER = 1; 159 const MW_DWORD MW_MSVC_EXCEPTION = 0x406D1388; 160 161 #pragma pack(push,8) 162 typedef struct tagTHREADNAME_INFO 163 { 164 MW_DWORD dwType; // Must be 0x1000. 165 const char* szName; // Pointer to name (in user addr space). 166 MW_DWORD dwThreadID; // Thread ID (-1=caller thread). 167 MW_DWORD dwFlags; // Reserved for future use, must be zero. 168 } THREADNAME_INFO; 169 #pragma pack(pop) 170 171 THREADNAME_INFO info; 172 info.dwType = 0x1000; 173 info.szName = threadName; 174 info.dwThreadID = ::GetCurrentThreadId(); 175 info.dwFlags = 0; 176 177 __try 178 { 179 RaiseException(MW_MSVC_EXCEPTION, 0, sizeof(info) / sizeof(void*), (MW_ULONG_PTR*)&info); 180 } 181 __except (MW_EXCEPTION_EXECUTE_HANDLER) 182 { 183 } 184 } 185 #endif 186 187 static void SetThreadSchedulingPolicy(uint32 cpuCore, ThreadPriority::Type priority = ThreadPriority::DEFAULT) 188 { 189 if (cpuCore == MT_CPUCORE_ANY) 190 { 191 cpuCore = MW_MAXIMUM_PROCESSORS; 192 } 193 MT_VERIFY((cpuCore >= 0 && cpuCore < (uint32)GetNumberOfHardwareThreads()) || cpuCore == MW_MAXIMUM_PROCESSORS, "Invalid cpu core specified", cpuCore=MW_MAXIMUM_PROCESSORS); 194 MW_DWORD res = ::SetThreadIdealProcessor( ::GetCurrentThread(), cpuCore); 195 MT_USED_IN_ASSERT(res); 196 MT_ASSERT(res != (MW_DWORD)-1, "SetThreadIdealProcessor failed!"); 197 198 int sched_priority = GetPriority(priority); 199 200 MW_BOOL result = ::SetThreadPriority( ::GetCurrentThread(), sched_priority ); 201 MT_USED_IN_ASSERT(result); 202 MT_ASSERT(result != 0, "SetThreadPriority failed!"); 203 } 204 205 static int GetNumberOfHardwareThreads() 206 { 207 MW_SYSTEM_INFO sysinfo; 208 ::GetSystemInfo( &sysinfo ); 209 return sysinfo.dwNumberOfProcessors; 210 } 211 212 static void Sleep(uint32 milliseconds) 213 { 214 ::Sleep(milliseconds); 215 } 216 }; 217 218 219 } 220 221 222 #endif 223