From d137ccbfc8e869277d96759a114cfe83f5f83657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Tue, 13 Apr 2021 10:47:12 +0200 Subject: 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::thread::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 --- thirdparty/etcpak/System.cpp | 3 - thirdparty/etcpak/TaskDispatch.cpp | 15 +++-- thirdparty/etcpak/patches/pthread-setname.patch | 66 ++++++++++++++++++++++ .../etcpak/patches/windows-mingw-bswap.patch | 50 ++++++++++++++++ .../etcpak/patches/windows-mingw-fixes.patch | 63 --------------------- 5 files changed, 127 insertions(+), 70 deletions(-) create mode 100644 thirdparty/etcpak/patches/pthread-setname.patch create mode 100644 thirdparty/etcpak/patches/windows-mingw-bswap.patch delete mode 100644 thirdparty/etcpak/patches/windows-mingw-fixes.patch (limited to 'thirdparty') diff --git a/thirdparty/etcpak/System.cpp b/thirdparty/etcpak/System.cpp index a09b289cb2..041f2676e8 100644 --- a/thirdparty/etcpak/System.cpp +++ b/thirdparty/etcpak/System.cpp @@ -2,7 +2,6 @@ #ifdef _WIN32 # include #else -# include # include #endif @@ -62,7 +61,5 @@ void System::SetThreadName( std::thread& thread, const char* name ) __except(EXCEPTION_EXECUTE_HANDLER) { } -#elif !defined(__APPLE__) - pthread_setname_np( thread.native_handle(), name ); #endif } diff --git a/thirdparty/etcpak/TaskDispatch.cpp b/thirdparty/etcpak/TaskDispatch.cpp index 7287da4de2..b1ba17953b 100644 --- a/thirdparty/etcpak/TaskDispatch.cpp +++ b/thirdparty/etcpak/TaskDispatch.cpp @@ -1,5 +1,8 @@ #include #include +#ifndef _MSC_VER +#include +#endif #include "Debug.hpp" #include "System.hpp" @@ -22,15 +25,19 @@ TaskDispatch::TaskDispatch( size_t workers ) { char tmp[16]; sprintf( tmp, "Worker %zu", i ); -#ifdef __APPLE__ +#ifdef _MSC_VER + auto worker = std::thread( [this]{ Worker(); } ); + System::SetThreadName( worker, tmp ); +#else // Using pthread. auto worker = std::thread( [this, tmp]{ +#ifdef __APPLE__ pthread_setname_np( tmp ); +#else // Linux or MinGW. + pthread_setname_np( pthread_self(), tmp ); +#endif Worker(); } ); -#else - auto worker = std::thread( [this]{ Worker(); } ); #endif - System::SetThreadName( worker, tmp ); m_workers.emplace_back( std::move( worker ) ); } diff --git a/thirdparty/etcpak/patches/pthread-setname.patch b/thirdparty/etcpak/patches/pthread-setname.patch new file mode 100644 index 0000000000..e2b009a1b3 --- /dev/null +++ b/thirdparty/etcpak/patches/pthread-setname.patch @@ -0,0 +1,66 @@ +diff --git a/thirdparty/etcpak/System.cpp b/thirdparty/etcpak/System.cpp +index 1383d0ecd0..041f2676e8 100644 +--- a/thirdparty/etcpak/System.cpp ++++ b/thirdparty/etcpak/System.cpp +@@ -2,7 +2,6 @@ + #ifdef _WIN32 + # include + #else +-# include + # include + #endif + +@@ -35,7 +34,7 @@ unsigned int System::CPUCores() + + void System::SetThreadName( std::thread& thread, const char* name ) + { +-#ifdef _WIN32 ++#ifdef _MSC_VER + const DWORD MS_VC_EXCEPTION=0x406D1388; + + # pragma pack( push, 8 ) +@@ -62,7 +61,5 @@ void System::SetThreadName( std::thread& thread, const char* name ) + __except(EXCEPTION_EXECUTE_HANDLER) + { + } +-#elif !defined(__APPLE__) +- pthread_setname_np( thread.native_handle(), name ); + #endif + } +diff --git a/thirdparty/etcpak/TaskDispatch.cpp b/thirdparty/etcpak/TaskDispatch.cpp +index 7287da4de2..b1ba17953b 100644 +--- a/thirdparty/etcpak/TaskDispatch.cpp ++++ b/thirdparty/etcpak/TaskDispatch.cpp +@@ -1,5 +1,8 @@ + #include + #include ++#ifndef _MSC_VER ++#include ++#endif + + #include "Debug.hpp" + #include "System.hpp" +@@ -22,15 +25,19 @@ TaskDispatch::TaskDispatch( size_t workers ) + { + char tmp[16]; + sprintf( tmp, "Worker %zu", i ); +-#ifdef __APPLE__ ++#ifdef _MSC_VER ++ auto worker = std::thread( [this]{ Worker(); } ); ++ System::SetThreadName( worker, tmp ); ++#else // Using pthread. + auto worker = std::thread( [this, tmp]{ ++#ifdef __APPLE__ + pthread_setname_np( tmp ); ++#else // Linux or MinGW. ++ pthread_setname_np( pthread_self(), tmp ); ++#endif + Worker(); + } ); +-#else +- auto worker = std::thread( [this]{ Worker(); } ); + #endif +- System::SetThreadName( worker, tmp ); + m_workers.emplace_back( std::move( worker ) ); + } + diff --git a/thirdparty/etcpak/patches/windows-mingw-bswap.patch b/thirdparty/etcpak/patches/windows-mingw-bswap.patch new file mode 100644 index 0000000000..c09192f573 --- /dev/null +++ b/thirdparty/etcpak/patches/windows-mingw-bswap.patch @@ -0,0 +1,50 @@ +diff --git a/thirdparty/etcpak/BlockData.cpp b/thirdparty/etcpak/BlockData.cpp +index a2cd032c5b..bd738085f3 100644 +--- a/thirdparty/etcpak/BlockData.cpp ++++ b/thirdparty/etcpak/BlockData.cpp +@@ -15,7 +15,7 @@ + # include + #endif + +-#ifdef __SSE4_1__ ++#if defined __SSE4_1__ || defined __AVX2__ || defined _MSC_VER + # ifdef _MSC_VER + # include + # include +@@ -24,12 +24,6 @@ + # else + # include + # endif +-#else +-# ifndef _MSC_VER +-# include +-# define _bswap(x) bswap_32(x) +-# define _bswap64(x) bswap_64(x) +-# endif + #endif + + #ifndef _bswap +diff --git a/thirdparty/etcpak/ProcessRGB.cpp b/thirdparty/etcpak/ProcessRGB.cpp +index 220d5c55e2..9dc5a78b67 100644 +--- a/thirdparty/etcpak/ProcessRGB.cpp ++++ b/thirdparty/etcpak/ProcessRGB.cpp +@@ -1,5 +1,6 @@ + #include + #include ++#include + + #ifdef __ARM_NEON + # include +@@ -21,12 +22,6 @@ + # else + # include + # endif +-#else +-# ifndef _MSC_VER +-# include +-# define _bswap(x) bswap_32(x) +-# define _bswap64(x) bswap_64(x) +-# endif + #endif + + #ifndef _bswap diff --git a/thirdparty/etcpak/patches/windows-mingw-fixes.patch b/thirdparty/etcpak/patches/windows-mingw-fixes.patch deleted file mode 100644 index 1da60e4a4f..0000000000 --- a/thirdparty/etcpak/patches/windows-mingw-fixes.patch +++ /dev/null @@ -1,63 +0,0 @@ -diff --git a/thirdparty/etcpak/BlockData.cpp b/thirdparty/etcpak/BlockData.cpp -index a2cd032c5b..bd738085f3 100644 ---- a/thirdparty/etcpak/BlockData.cpp -+++ b/thirdparty/etcpak/BlockData.cpp -@@ -15,7 +15,7 @@ - # include - #endif - --#ifdef __SSE4_1__ -+#if defined __SSE4_1__ || defined __AVX2__ || defined _MSC_VER - # ifdef _MSC_VER - # include - # include -@@ -24,12 +24,6 @@ - # else - # include - # endif --#else --# ifndef _MSC_VER --# include --# define _bswap(x) bswap_32(x) --# define _bswap64(x) bswap_64(x) --# endif - #endif - - #ifndef _bswap -diff --git a/thirdparty/etcpak/ProcessRGB.cpp b/thirdparty/etcpak/ProcessRGB.cpp -index 220d5c55e2..9dc5a78b67 100644 ---- a/thirdparty/etcpak/ProcessRGB.cpp -+++ b/thirdparty/etcpak/ProcessRGB.cpp -@@ -1,5 +1,6 @@ - #include - #include -+#include - - #ifdef __ARM_NEON - # include -@@ -21,12 +22,6 @@ - # else - # include - # endif --#else --# ifndef _MSC_VER --# include --# define _bswap(x) bswap_32(x) --# define _bswap64(x) bswap_64(x) --# endif - #endif - - #ifndef _bswap -diff --git a/thirdparty/etcpak/System.cpp b/thirdparty/etcpak/System.cpp -index 1383d0ecd0..a09b289cb2 100644 ---- a/thirdparty/etcpak/System.cpp -+++ b/thirdparty/etcpak/System.cpp -@@ -35,7 +35,7 @@ unsigned int System::CPUCores() - - void System::SetThreadName( std::thread& thread, const char* name ) - { --#ifdef _WIN32 -+#ifdef _MSC_VER - const DWORD MS_VC_EXCEPTION=0x406D1388; - - # pragma pack( push, 8 ) -- cgit v1.2.3