Core: Fix compilation with MinGW MCF threads

This commit is contained in:
SirLynix 2023-07-22 12:25:40 +02:00
parent caaaf606d3
commit 6370e0d16d
3 changed files with 40 additions and 9 deletions

View File

@ -16,15 +16,31 @@
namespace Nz 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<PlatformImpl::ThreadHandle>(_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<PlatformImpl::ThreadHandle>(thread.native_handle());
#endif
}
}
std::string GetCurrentThreadName() std::string GetCurrentThreadName()
{ {
NAZARA_USE_ANONYMOUS_NAMESPACE
return PlatformImpl::GetCurrentThreadName(); return PlatformImpl::GetCurrentThreadName();
} }
std::string GetThreadName(std::thread& thread) std::string GetThreadName(std::thread& thread)
{ {
// std::thread::native_handle returns a void* with MSVC instead of a HANDLE // std::thread::native_handle returns a void* with MSVC instead of a HANDLE
return PlatformImpl::GetThreadName(static_cast<PlatformImpl::ThreadHandle>(thread.native_handle())); return PlatformImpl::GetThreadName(GetHandle(thread));
} }
void SetCurrentThreadName(const char* name) void SetCurrentThreadName(const char* name)
@ -34,7 +50,9 @@ namespace Nz
void SetThreadName(std::thread& thread, const char* name) 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 // std::thread::native_handle returns a void* with MSVC instead of a HANDLE
PlatformImpl::SetThreadName(static_cast<PlatformImpl::ThreadHandle>(thread.native_handle()), name); PlatformImpl::SetThreadName(GetHandle(thread), name);
} }
} }

View File

@ -9,7 +9,7 @@
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
#ifndef NAZARA_COMPILER_MINGW #ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX
namespace NAZARA_ANONYMOUS_NAMESPACE namespace NAZARA_ANONYMOUS_NAMESPACE
{ {
// Windows 10, version 1607 added GetThreadDescription and SetThreadDescription in order to name a thread // Windows 10, version 1607 added GetThreadDescription and SetThreadDescription in order to name a thread
@ -32,7 +32,7 @@ namespace Nz::PlatformImpl
ThreadHandle GetCurrentThreadHandle() ThreadHandle GetCurrentThreadHandle()
{ {
#ifndef NAZARA_COMPILER_MINGW #ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX
return ::GetCurrentThread(); return ::GetCurrentThread();
#else #else
return ::pthread_self(); return ::pthread_self();
@ -46,7 +46,7 @@ namespace Nz::PlatformImpl
std::string GetThreadName(ThreadHandle threadHandle) std::string GetThreadName(ThreadHandle threadHandle)
{ {
#ifndef NAZARA_COMPILER_MINGW #ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX
NAZARA_USE_ANONYMOUS_NAMESPACE NAZARA_USE_ANONYMOUS_NAMESPACE
// Use GetThreadDescription if available // Use GetThreadDescription if available
@ -70,7 +70,7 @@ namespace Nz::PlatformImpl
#endif #endif
} }
#ifndef NAZARA_COMPILER_MINGW #ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX
void RaiseThreadNameException(DWORD threadId, const char* threadName) void RaiseThreadNameException(DWORD threadId, const char* threadName)
{ {
#ifdef NAZARA_COMPILER_MSVC #ifdef NAZARA_COMPILER_MSVC
@ -111,7 +111,7 @@ namespace Nz::PlatformImpl
void SetThreadName(ThreadHandle threadHandle, const char* threadName) void SetThreadName(ThreadHandle threadHandle, const char* threadName)
{ {
#ifndef NAZARA_COMPILER_MINGW #ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX
NAZARA_USE_ANONYMOUS_NAMESPACE NAZARA_USE_ANONYMOUS_NAMESPACE
// Try to use SetThreadDescription if available // Try to use SetThreadDescription if available
@ -126,6 +126,6 @@ namespace Nz::PlatformImpl
} }
} }
#ifndef NAZARA_COMPILER_MINGW #ifndef NAZARA_COMPILER_MINGW_THREADS_POSIX
#include <Nazara/Core/AntiWindows.hpp> #include <Nazara/Core/AntiWindows.hpp>
#endif #endif

View File

@ -10,7 +10,20 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
#include <string> #include <string>
// Try to identify MinGW thread flavor
#ifdef NAZARA_COMPILER_MINGW #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 <pthread.h> #include <pthread.h>
#else #else
#include <Windows.h> #include <Windows.h>
@ -18,7 +31,7 @@
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
#ifdef NAZARA_COMPILER_MINGW #ifdef NAZARA_COMPILER_MINGW_THREADS_POSIX
using ThreadHandle = pthread_t; using ThreadHandle = pthread_t;
#else #else
using ThreadHandle = HANDLE; using ThreadHandle = HANDLE;