Преглед изворни кода

Fix Thread::setThreadName. (#358)

Branimir Karadžić пре 1 месец
родитељ
комит
ce31b14454
2 измењених фајлова са 31 додато и 41 уклоњено
  1. 4 4
      include/bx/thread.h
  2. 27 37
      src/thread.cpp

+ 4 - 4
include/bx/thread.h

@@ -6,8 +6,8 @@
 #ifndef BX_THREAD_H_HEADER_GUARD
 #define BX_THREAD_H_HEADER_GUARD
 
-#include "allocator.h"
 #include "mpscqueue.h"
+#include "string.h"
 
 #if BX_CONFIG_SUPPORTS_THREADING
 
@@ -39,7 +39,7 @@ namespace bx
 		/// @param[in] _name Thread name used by debugger.
 		/// @returns True if thread is created, otherwise returns false.
 		///
-		bool init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const char* _name = NULL);
+		bool init(ThreadFn _fn, void* _userData = NULL, uint32_t _stackSize = 0, const StringView& _name = "");
 
 		///
 		void shutdown();
@@ -51,7 +51,7 @@ namespace bx
 		int32_t getExitCode() const;
 
 		///
-		void setThreadName(const char* _name);
+		void setThreadName(const StringView& _name);
 
 		///
 		void push(void* _ptr);
@@ -72,7 +72,7 @@ namespace bx
 		uint32_t  m_stackSize;
 		int32_t   m_exitCode;
 		bool      m_running;
-		char      m_name[64];
+		FixedString64 m_name;
 	};
 
 	///

+ 27 - 37
src/thread.cpp

@@ -109,23 +109,14 @@ namespace bx
 		}
 	}
 
-	bool Thread::init(ThreadFn _fn, void* _userData, uint32_t _stackSize, const char* _name)
+	bool Thread::init(ThreadFn _fn, void* _userData, uint32_t _stackSize, const StringView& _name)
 	{
 		BX_ASSERT(!m_running, "Already running!");
 
-		m_fn = _fn;
-		m_userData = _userData;
+		m_fn        = _fn;
+		m_userData  = _userData;
 		m_stackSize = _stackSize;
-
-		if (NULL != _name)
-		{
-			BX_WARN(strLen(_name) < int32_t(BX_COUNTOF(m_name) )-1, "Truncating thread name.");
-			strCopy(m_name, BX_COUNTOF(m_name), _name);
-		}
-		else
-		{
-			m_name[0] = '\0';
-		}
+		m_name      = _name;
 
 		ThreadInternal* ti = (ThreadInternal*)m_internal;
 #if BX_CRT_NONE
@@ -223,40 +214,40 @@ namespace bx
 		return m_exitCode;
 	}
 
-	void Thread::setThreadName(const char* _name)
+	void Thread::setThreadName(const StringView& _name)
 	{
-		if (NULL == _name
-		||  0 == strLen(_name) )
+		if (_name.isEmpty() )
 		{
 			return;
 		}
 
+		m_name = _name;
+
 		ThreadInternal* ti = (ThreadInternal*)m_internal;
 		BX_UNUSED(ti);
-#if BX_CRT_NONE
-		BX_UNUSED(_name);
-#elif  BX_PLATFORM_OSX \
-	|| BX_PLATFORM_IOS   \
+#if  BX_PLATFORM_OSX   \
+	|| BX_PLATFORM_IOS \
 	|| BX_PLATFORM_VISIONOS
-		pthread_setname_np(_name);
+		pthread_setname_np(m_name.getCPtr() );
 #elif BX_CRT_GLIBC
-		pthread_setname_np(ti->m_handle, _name);
+		pthread_setname_np(ti->m_handle, m_name.getCPtr() );
 #elif BX_PLATFORM_LINUX
-		prctl(PR_SET_NAME,_name, 0, 0, 0);
+		prctl(PR_SET_NAME, m_name.getCPtr(), 0, 0, 0);
 #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 = dlsym<SetThreadDescriptionProc>((void*)GetModuleHandleA("Kernel32.dll"), "SetThreadDescription");
+		SetThreadDescriptionProc SetThreadDescription = dlsym<SetThreadDescriptionProc>( (void*)GetModuleHandleA("Kernel32.dll"), "SetThreadDescription");
 
 		if (NULL != SetThreadDescription)
 		{
-			uint32_t length = (uint32_t)strLen(_name)+1;
-			uint32_t size = length*sizeof(wchar_t);
-			wchar_t* name = (wchar_t*)BX_STACK_ALLOC(size);
-			mbstowcs(name, _name, size-2);
-			name[size-2] = 0;
+			const uint32_t length = m_name.getLength();
+			const uint32_t max    = (length+1)*sizeof(wchar_t);
+			wchar_t* name = (wchar_t*)BX_STACK_ALLOC(max);
+			mbstowcs(name, m_name.getCPtr(), length);
+			name[length] = 0;
 			SetThreadDescription(ti->m_handle, name);
 		}
+		else
+		{
 #	if BX_COMPILER_MSVC
 #		pragma pack(push, 8)
 			struct ThreadName
@@ -269,24 +260,23 @@ namespace bx
 #		pragma pack(pop)
 			ThreadName tn;
 			tn.type  = 0x1000;
-			tn.name  = _name;
+			tn.name  = m_name.getCPtr();
 			tn.id    = ti->m_threadId;
 			tn.flags = 0;
 
 			__try
 			{
 				RaiseException(0x406d1388
-						, 0
-						, sizeof(tn)/4
-						, reinterpret_cast<ULONG_PTR*>(&tn)
-						);
+					, 0
+					, sizeof(tn)/4
+					, reinterpret_cast<ULONG_PTR*>(&tn)
+					);
 			}
 			__except(EXCEPTION_EXECUTE_HANDLER)
 			{
 			}
 #	endif // BX_COMPILER_MSVC
-#else
-		BX_UNUSED(_name);
+		}
 #endif // BX_PLATFORM_
 	}