godot/thirdparty/etcpak/System.cpp
Rémi Verschelde d137ccbfc8
etcpak: Fix handling of pthread naming API for Linux and MinGW
For MinGW this is tricky to do as a two-step process like it was implemented,
as `std:🧵:native_handle()` is implementation-defined and depending on
the MinGW distribution, it may or may not be a pthread handle.

With mingw-gcc as packaged in Linux distros with pthread support it worked
fine, but with llvm-mingw it was problematic.

Setting the name in the thread directly as done for Apple platforms is simpler
and works fine.

Co-authored-by: Hein-Pieter van Braam-Stewart <hp@tmm.cx>
2021-04-13 21:04:09 +02:00

66 lines
1.4 KiB
C++

#include <algorithm>
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
#include "System.hpp"
unsigned int System::CPUCores()
{
static unsigned int cores = 0;
if( cores == 0 )
{
int tmp;
#ifdef _WIN32
SYSTEM_INFO info;
GetSystemInfo( &info );
tmp = (int)info.dwNumberOfProcessors;
#else
# ifndef _SC_NPROCESSORS_ONLN
# ifdef _SC_NPROC_ONLN
# define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
# elif defined _SC_CRAY_NCPU
# define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
# endif
# endif
tmp = (int)(long)sysconf( _SC_NPROCESSORS_ONLN );
#endif
cores = (unsigned int)std::max( tmp, 1 );
}
return cores;
}
void System::SetThreadName( std::thread& thread, const char* name )
{
#ifdef _MSC_VER
const DWORD MS_VC_EXCEPTION=0x406D1388;
# pragma pack( push, 8 )
struct THREADNAME_INFO
{
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
};
# pragma pack(pop)
DWORD ThreadId = GetThreadId( static_cast<HANDLE>( thread.native_handle() ) );
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = ThreadId;
info.dwFlags = 0;
__try
{
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
#endif
}