diff --git a/src/Nazara/Core/ThreadExt.cpp b/src/Nazara/Core/ThreadExt.cpp index 1773b432f..c29a33d65 100644 --- a/src/Nazara/Core/ThreadExt.cpp +++ b/src/Nazara/Core/ThreadExt.cpp @@ -16,15 +16,31 @@ namespace Nz { + namespace NAZARA_ANONYMOUS_NAMESPACE + { + PlatformImpl::ThreadHandle GetHandle(std::thread& thread) + { +#ifdef NAZARA_COMPILER_MINGW_THREADS_MCF + // MCF flavor (returns HANDLE by a void*) + return static_cast(_MCF_thread_get_handle(thread.native_handle())); +#else + // Cast because of MSVC standard library that returns a void* instead of a HANDLE + return static_cast(thread.native_handle()); +#endif + } + } + std::string GetCurrentThreadName() { + NAZARA_USE_ANONYMOUS_NAMESPACE + return PlatformImpl::GetCurrentThreadName(); } std::string GetThreadName(std::thread& thread) { // std::thread::native_handle returns a void* with MSVC instead of a HANDLE - return PlatformImpl::GetThreadName(static_cast(thread.native_handle())); + return PlatformImpl::GetThreadName(GetHandle(thread)); } void SetCurrentThreadName(const char* name) @@ -34,7 +50,9 @@ namespace Nz void SetThreadName(std::thread& thread, const char* name) { + NAZARA_USE_ANONYMOUS_NAMESPACE + // std::thread::native_handle returns a void* with MSVC instead of a HANDLE - PlatformImpl::SetThreadName(static_cast(thread.native_handle()), name); + PlatformImpl::SetThreadName(GetHandle(thread), name); } } diff --git a/src/Nazara/Core/Win32/ThreadImpl.cpp b/src/Nazara/Core/Win32/ThreadImpl.cpp index 8efb4b35d..da2ea9fd4 100644 --- a/src/Nazara/Core/Win32/ThreadImpl.cpp +++ b/src/Nazara/Core/Win32/ThreadImpl.cpp @@ -9,7 +9,7 @@ namespace Nz::PlatformImpl { -#ifndef NAZARA_COMPILER_MINGW +#ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX namespace NAZARA_ANONYMOUS_NAMESPACE { // Windows 10, version 1607 added GetThreadDescription and SetThreadDescription in order to name a thread @@ -32,7 +32,7 @@ namespace Nz::PlatformImpl ThreadHandle GetCurrentThreadHandle() { -#ifndef NAZARA_COMPILER_MINGW +#ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX return ::GetCurrentThread(); #else return ::pthread_self(); @@ -46,7 +46,7 @@ namespace Nz::PlatformImpl std::string GetThreadName(ThreadHandle threadHandle) { -#ifndef NAZARA_COMPILER_MINGW +#ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX NAZARA_USE_ANONYMOUS_NAMESPACE // Use GetThreadDescription if available @@ -70,7 +70,7 @@ namespace Nz::PlatformImpl #endif } -#ifndef NAZARA_COMPILER_MINGW +#ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX void RaiseThreadNameException(DWORD threadId, const char* threadName) { #ifdef NAZARA_COMPILER_MSVC @@ -111,7 +111,7 @@ namespace Nz::PlatformImpl void SetThreadName(ThreadHandle threadHandle, const char* threadName) { -#ifndef NAZARA_COMPILER_MINGW +#ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX NAZARA_USE_ANONYMOUS_NAMESPACE // Try to use SetThreadDescription if available @@ -126,6 +126,6 @@ namespace Nz::PlatformImpl } } -#ifndef NAZARA_COMPILER_MINGW +#ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX #include #endif diff --git a/src/Nazara/Core/Win32/ThreadImpl.hpp b/src/Nazara/Core/Win32/ThreadImpl.hpp index 252f36311..0afd14929 100644 --- a/src/Nazara/Core/Win32/ThreadImpl.hpp +++ b/src/Nazara/Core/Win32/ThreadImpl.hpp @@ -10,7 +10,20 @@ #include #include +// Try to identify MinGW thread flavor #ifdef NAZARA_COMPILER_MINGW + +#if defined(__USING_MCFGTHREAD__) +#define NAZARA_COMPILER_MINGW_THREADS_MCF +#elif defined(_REENTRANT) +#define NAZARA_COMPILER_MINGW_THREADS_POSIX +#else +#define NAZARA_COMPILER_MINGW_THREADS_WIN32 +#endif + +#endif + +#ifdef NAZARA_COMPILER_MINGW_THREADS_POSIX #include #else #include @@ -18,7 +31,7 @@ namespace Nz::PlatformImpl { -#ifdef NAZARA_COMPILER_MINGW +#ifdef NAZARA_COMPILER_MINGW_THREADS_POSIX using ThreadHandle = pthread_t; #else using ThreadHandle = HANDLE;