Fix thread support on MinGW

This commit is contained in:
SirLynix 2023-06-07 18:54:37 +02:00
parent 4c21821802
commit 51ac2b71df
3 changed files with 44 additions and 11 deletions

View File

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/ThreadImpl.hpp> #include <Nazara/Core/Posix/ThreadImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <array> #include <array>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
@ -34,7 +35,7 @@ namespace Nz::PlatformImpl
void SetCurrentThreadName(const char* threadName) void SetCurrentThreadName(const char* threadName)
{ {
#if defined(__linux__) #if defined(__linux__) || defined(NAZARA_COMPILER_MINGW)
::pthread_setname_np(::pthread_self(), threadName); ::pthread_setname_np(::pthread_self(), threadName);
#elif defined(__APPLE__) #elif defined(__APPLE__)
::pthread_setname_np(threadName); ::pthread_setname_np(threadName);
@ -47,7 +48,7 @@ namespace Nz::PlatformImpl
void SetThreadName(pthread_t threadHandle, const char* threadName) void SetThreadName(pthread_t threadHandle, const char* threadName)
{ {
#if defined(__linux__) #if defined(__linux__) || defined(NAZARA_COMPILER_MINGW)
::pthread_setname_np(threadHandle, threadName); ::pthread_setname_np(threadHandle, threadName);
#elif defined(__APPLE__) #elif defined(__APPLE__)
NazaraWarning("only current thread name can be set on MacOS X"); NazaraWarning("only current thread name can be set on MacOS X");

View File

@ -9,6 +9,7 @@
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
#ifndef NAZARA_COMPILER_MINGW
namespace NAZARA_ANONYMOUS_NAMESPACE namespace NAZARA_ANONYMOUS_NAMESPACE
{ {
// Windows 10, version 1607 added SetThreadDescription in order to name a thread // Windows 10, version 1607 added SetThreadDescription in order to name a thread
@ -24,21 +25,27 @@ namespace Nz::PlatformImpl
DWORD dwFlags; DWORD dwFlags;
}; };
#pragma pack(pop) #pragma pack(pop)
#endif #endif // NAZARA_COMPILER_MSVC
} }
#endif // !NAZARA_COMPILER_MINGW
HANDLE GetCurrentThreadHandle() ThreadHandle GetCurrentThreadHandle()
{ {
#ifndef NAZARA_COMPILER_MINGW
return ::GetCurrentThread(); return ::GetCurrentThread();
#else
return ::pthread_self();
#endif
} }
std::string GetCurrentThreadName() std::string GetCurrentThreadName()
{ {
return GetThreadName(::GetCurrentThread()); return GetThreadName(GetCurrentThreadHandle());
} }
std::string GetThreadName(HANDLE threadHandle) std::string GetThreadName(ThreadHandle threadHandle)
{ {
#ifndef NAZARA_COMPILER_MINGW
PWSTR namePtr; PWSTR namePtr;
HRESULT hr = ::GetThreadDescription(threadHandle, &namePtr); HRESULT hr = ::GetThreadDescription(threadHandle, &namePtr);
if (FAILED(hr)) if (FAILED(hr))
@ -47,8 +54,15 @@ namespace Nz::PlatformImpl
CallOnExit freeName([&] { LocalFree(namePtr); }); CallOnExit freeName([&] { LocalFree(namePtr); });
return FromWideString(namePtr); return FromWideString(namePtr);
#else
std::array<char, 16> name;
::pthread_getname_np(threadHandle, &name[0], name.size());
return std::string(&name[0]);
#endif
} }
#ifndef NAZARA_COMPILER_MINGW
void RaiseThreadNameException(DWORD threadId, const char* threadName) void RaiseThreadNameException(DWORD threadId, const char* threadName)
{ {
#ifdef NAZARA_COMPILER_MSVC #ifdef NAZARA_COMPILER_MSVC
@ -80,14 +94,16 @@ namespace Nz::PlatformImpl
NazaraWarning("ThreadName exception is only supported with MSVC"); NazaraWarning("ThreadName exception is only supported with MSVC");
#endif #endif
} }
#endif
void SetCurrentThreadName(const char* threadName) void SetCurrentThreadName(const char* threadName)
{ {
SetThreadName(::GetCurrentThread(), threadName); SetThreadName(GetCurrentThreadHandle(), threadName);
} }
void SetThreadName(HANDLE threadHandle, const char* threadName) void SetThreadName(ThreadHandle threadHandle, const char* threadName)
{ {
#ifndef NAZARA_COMPILER_MINGW
NAZARA_USE_ANONYMOUS_NAMESPACE NAZARA_USE_ANONYMOUS_NAMESPACE
// Try to use SetThreadDescription if available // Try to use SetThreadDescription if available
@ -96,7 +112,12 @@ namespace Nz::PlatformImpl
SetThreadDescription(threadHandle, ToWideString(threadName).data()); SetThreadDescription(threadHandle, ToWideString(threadName).data());
else else
RaiseThreadNameException(::GetThreadId(threadHandle), threadName); RaiseThreadNameException(::GetThreadId(threadHandle), threadName);
#else
::pthread_setname_np(threadHandle, threadName);
#endif
} }
} }
#ifndef NAZARA_COMPILER_MINGW
#include <Nazara/Core/AntiWindows.hpp> #include <Nazara/Core/AntiWindows.hpp>
#endif

View File

@ -9,18 +9,29 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
#include <string> #include <string>
#ifdef NAZARA_COMPILER_MINGW
#include <pthread.h>
#else
#include <Windows.h> #include <Windows.h>
#endif
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
#ifdef NAZARA_COMPILER_MINGW
using ThreadHandle = pthread_t;
#else
using ThreadHandle = HANDLE; using ThreadHandle = HANDLE;
#endif
HANDLE GetCurrentThreadHandle(); ThreadHandle GetCurrentThreadHandle();
std::string GetCurrentThreadName(); std::string GetCurrentThreadName();
std::string GetThreadName(HANDLE threadHandle); std::string GetThreadName(ThreadHandle threadHandle);
#ifndef NAZARA_COMPILER_MINGW
void RaiseThreadNameException(DWORD threadId, const char* threadName); void RaiseThreadNameException(DWORD threadId, const char* threadName);
#endif
void SetCurrentThreadName(const char* threadName); void SetCurrentThreadName(const char* threadName);
void SetThreadName(HANDLE threadHandle, const char* threadName); void SetThreadName(ThreadHandle threadHandle, const char* threadName);
} }
#endif // NAZARA_CORE_WIN32_THREADIMPL_HPP #endif // NAZARA_CORE_WIN32_THREADIMPL_HPP