diff --git a/src/Nazara/Core/Posix/ThreadImpl.cpp b/src/Nazara/Core/Posix/ThreadImpl.cpp index ec457aee5..d7255cd25 100644 --- a/src/Nazara/Core/Posix/ThreadImpl.cpp +++ b/src/Nazara/Core/Posix/ThreadImpl.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include @@ -34,7 +35,7 @@ namespace Nz::PlatformImpl void SetCurrentThreadName(const char* threadName) { -#if defined(__linux__) +#if defined(__linux__) || defined(NAZARA_COMPILER_MINGW) ::pthread_setname_np(::pthread_self(), threadName); #elif defined(__APPLE__) ::pthread_setname_np(threadName); @@ -47,7 +48,7 @@ namespace Nz::PlatformImpl void SetThreadName(pthread_t threadHandle, const char* threadName) { -#if defined(__linux__) +#if defined(__linux__) || defined(NAZARA_COMPILER_MINGW) ::pthread_setname_np(threadHandle, threadName); #elif defined(__APPLE__) NazaraWarning("only current thread name can be set on MacOS X"); diff --git a/src/Nazara/Core/Win32/ThreadImpl.cpp b/src/Nazara/Core/Win32/ThreadImpl.cpp index b4363e3e1..1def05650 100644 --- a/src/Nazara/Core/Win32/ThreadImpl.cpp +++ b/src/Nazara/Core/Win32/ThreadImpl.cpp @@ -9,6 +9,7 @@ namespace Nz::PlatformImpl { +#ifndef NAZARA_COMPILER_MINGW namespace NAZARA_ANONYMOUS_NAMESPACE { // Windows 10, version 1607 added SetThreadDescription in order to name a thread @@ -24,21 +25,27 @@ namespace Nz::PlatformImpl DWORD dwFlags; }; #pragma pack(pop) -#endif +#endif // NAZARA_COMPILER_MSVC } +#endif // !NAZARA_COMPILER_MINGW - HANDLE GetCurrentThreadHandle() + ThreadHandle GetCurrentThreadHandle() { +#ifndef NAZARA_COMPILER_MINGW return ::GetCurrentThread(); +#else + return ::pthread_self(); +#endif } 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; HRESULT hr = ::GetThreadDescription(threadHandle, &namePtr); if (FAILED(hr)) @@ -47,8 +54,15 @@ namespace Nz::PlatformImpl CallOnExit freeName([&] { LocalFree(namePtr); }); return FromWideString(namePtr); +#else + std::array 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) { #ifdef NAZARA_COMPILER_MSVC @@ -80,14 +94,16 @@ namespace Nz::PlatformImpl NazaraWarning("ThreadName exception is only supported with MSVC"); #endif } +#endif 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 // Try to use SetThreadDescription if available @@ -96,7 +112,12 @@ namespace Nz::PlatformImpl SetThreadDescription(threadHandle, ToWideString(threadName).data()); else RaiseThreadNameException(::GetThreadId(threadHandle), threadName); +#else + ::pthread_setname_np(threadHandle, threadName); +#endif } } +#ifndef NAZARA_COMPILER_MINGW #include +#endif diff --git a/src/Nazara/Core/Win32/ThreadImpl.hpp b/src/Nazara/Core/Win32/ThreadImpl.hpp index 90963eecf..252f36311 100644 --- a/src/Nazara/Core/Win32/ThreadImpl.hpp +++ b/src/Nazara/Core/Win32/ThreadImpl.hpp @@ -9,18 +9,29 @@ #include #include + +#ifdef NAZARA_COMPILER_MINGW +#include +#else #include +#endif namespace Nz::PlatformImpl { +#ifdef NAZARA_COMPILER_MINGW + using ThreadHandle = pthread_t; +#else using ThreadHandle = HANDLE; +#endif - HANDLE GetCurrentThreadHandle(); + ThreadHandle GetCurrentThreadHandle(); std::string GetCurrentThreadName(); - std::string GetThreadName(HANDLE threadHandle); + std::string GetThreadName(ThreadHandle threadHandle); +#ifndef NAZARA_COMPILER_MINGW void RaiseThreadNameException(DWORD threadId, const char* threadName); +#endif 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