Core/Thread: Add posibility of setting thread name
This commit is contained in:
@@ -6,13 +6,27 @@
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Functor.hpp>
|
||||
#include <process.h>
|
||||
#include <windows.h>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
#pragma pack(push,8)
|
||||
struct THREADNAME_INFO
|
||||
{
|
||||
DWORD dwType;
|
||||
LPCSTR szName;
|
||||
DWORD dwThreadID;
|
||||
DWORD dwFlags;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
}
|
||||
|
||||
ThreadImpl::ThreadImpl(Functor* functor)
|
||||
{
|
||||
m_handle = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &ThreadImpl::ThreadProc, functor, 0, nullptr));
|
||||
m_handle = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &ThreadImpl::ThreadProc, functor, 0, &m_threadId));
|
||||
if (!m_handle)
|
||||
NazaraInternalError("Failed to create thread: " + Error::GetLastSystemError());
|
||||
}
|
||||
@@ -29,6 +43,44 @@ namespace Nz
|
||||
CloseHandle(m_handle);
|
||||
}
|
||||
|
||||
void ThreadImpl::SetName(const Nz::String& name)
|
||||
{
|
||||
SetThreadName(m_threadId, name.GetConstBuffer());
|
||||
}
|
||||
|
||||
void ThreadImpl::SetCurrentName(const Nz::String& name)
|
||||
{
|
||||
SetThreadName(::GetCurrentThreadId(), name.GetConstBuffer());
|
||||
}
|
||||
|
||||
void ThreadImpl::Sleep(UInt32 time)
|
||||
{
|
||||
::Sleep(time);
|
||||
}
|
||||
|
||||
void ThreadImpl::SetThreadName(DWORD threadId, const char* threadName)
|
||||
{
|
||||
// https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
|
||||
constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
|
||||
|
||||
THREADNAME_INFO info;
|
||||
info.dwType = 0x1000;
|
||||
info.szName = threadName;
|
||||
info.dwThreadID = threadId;
|
||||
info.dwFlags = 0;
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 6320 6322)
|
||||
__try
|
||||
{
|
||||
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
|
||||
}
|
||||
__except (EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
}
|
||||
#pragma warning(pop)
|
||||
}
|
||||
|
||||
unsigned int __stdcall ThreadImpl::ThreadProc(void* userdata)
|
||||
{
|
||||
Functor* func = static_cast<Functor*>(userdata);
|
||||
@@ -42,9 +94,4 @@ namespace Nz
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ThreadImpl::Sleep(UInt32 time)
|
||||
{
|
||||
::Sleep(time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#define NAZARA_THREADIMPL_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <windows.h>
|
||||
|
||||
namespace Nz
|
||||
@@ -23,12 +24,16 @@ namespace Nz
|
||||
|
||||
void Detach();
|
||||
void Join();
|
||||
void SetName(const Nz::String& name);
|
||||
|
||||
static void SetCurrentName(const Nz::String& name);
|
||||
static void Sleep(UInt32 time);
|
||||
|
||||
private:
|
||||
static void SetThreadName(DWORD threadId, const char* threadName);
|
||||
static unsigned int __stdcall ThreadProc(void* userdata);
|
||||
|
||||
DWORD m_threadId;
|
||||
HANDLE m_handle;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user