浏览代码

call SetThreadDescription() to set thread name if available (#200)

* Update thread.cpp

call SetThreadDescription() (if available) when setting the thread name, so that it is available in minidumps and to any process that has permission to call GetThreadDescription (as opposed to just being available inside the debugger)

* Update thread.cpp

whitespace fix

* Update thread.cpp

another bit of whitespace, I've become so blind to this

* Update thread.cpp

whoops - would help if it actually compiled

* Update thread.cpp

actually this one was unnecessary - already checked above as first #if condition

* Update thread.cpp

change to use GetModuleHandleA
Graeme Kelly 7 年之前
父节点
当前提交
81413839a8
共有 1 个文件被更改,包括 42 次插入25 次删除
  1. 42 25
      src/thread.cpp

+ 42 - 25
src/thread.cpp

@@ -8,6 +8,10 @@
 
 #if BX_CONFIG_SUPPORTS_THREADING
 
+#if BX_PLATFORM_WINDOWS && !BX_CRT_NONE
+#	include <bx/string.h>
+#endif
+
 #if BX_CRT_NONE
 #	include "crt0.h"
 #elif  BX_PLATFORM_ANDROID \
@@ -240,33 +244,46 @@ namespace bx
 #	else
 		pthread_set_name_np(ti->m_handle, _name);
 #	endif // defined(__NetBSD__)
-#elif BX_PLATFORM_WINDOWS && BX_COMPILER_MSVC
-#	pragma pack(push, 8)
-		struct ThreadName
-		{
-			DWORD  type;
-			LPCSTR name;
-			DWORD  id;
-			DWORD  flags;
-		};
-#	pragma pack(pop)
-		ThreadName tn;
-		tn.type  = 0x1000;
-		tn.name  = _name;
-		tn.id    = ti->m_threadId;
-		tn.flags = 0;
-
-		__try
-		{
-			RaiseException(0x406d1388
-					, 0
-					, sizeof(tn)/4
-					, reinterpret_cast<ULONG_PTR*>(&tn)
-					);
-		}
-		__except(EXCEPTION_EXECUTE_HANDLER)
+#elif BX_PLATFORM_WINDOWS
+		// Try to use the new thread naming API from Win10 Creators update onwards if we have it
+		typedef HRESULT (WINAPI *SetThreadDescriptionProc)(HANDLE, PCWSTR);
+		SetThreadDescriptionProc SetThreadDescription = (SetThreadDescriptionProc)(GetProcAddress(GetModuleHandleA("Kernel32.dll"), "SetThreadDescription"));
+		if (SetThreadDescription)
 		{
+			uint32_t length = (uint32_t)bx::strLen(_name)+1;
+			uint32_t size = length*sizeof(wchar_t);
+			wchar_t* name = (wchar_t*)alloca(size);
+			mbstowcs(name, _name, size-2);
+			SetThreadDescription(ti->m_handle, name);
 		}
+#	if BX_COMPILER_MSVC
+#		pragma pack(push, 8)
+			struct ThreadName
+			{
+				DWORD  type;
+				LPCSTR name;
+				DWORD  id;
+				DWORD  flags;
+			};
+#		pragma pack(pop)
+			ThreadName tn;
+			tn.type  = 0x1000;
+			tn.name  = _name;
+			tn.id    = ti->m_threadId;
+			tn.flags = 0;
+
+			__try
+			{
+				RaiseException(0x406d1388
+						, 0
+						, sizeof(tn)/4
+						, reinterpret_cast<ULONG_PTR*>(&tn)
+						);
+			}
+			__except(EXCEPTION_EXECUTE_HANDLER)
+			{
+			}
+#	endif // BX_COMPILER_MSVC
 #else
 		BX_UNUSED(_name);
 #endif // BX_PLATFORM_