Use std:🧵:native_handle() only for MSVC, because it is unsafe to assume native_handle_type otherwise.

This commit is contained in:
levlam 2022-11-26 02:48:43 +03:00
parent 6679837ea1
commit ed1457b69b

View File

@ -12,7 +12,7 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/invoke.h" #include "td/utils/invoke.h"
#if TD_WINDOWS #if TD_WINDOWS && TD_MSVC
#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/NativeFd.h"
#endif #endif
#include "td/utils/port/detail/ThreadIdGuard.h" #include "td/utils/port/detail/ThreadIdGuard.h"
@ -25,7 +25,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#if TD_WINDOWS #if TD_WINDOWS && TD_MSVC
#define TD_HAVE_THREAD_AFFINITY 1 #define TD_HAVE_THREAD_AFFINITY 1
#endif #endif
@ -73,19 +73,17 @@ class ThreadStl {
return std::thread::hardware_concurrency(); return std::thread::hardware_concurrency();
} }
#if TD_WINDOWS #if TD_WINDOWS && TD_MSVC
using id = DWORD; using id = DWORD;
#else #else
using id = std::thread::id; using id = std::thread::id;
#endif #endif
id get_id() noexcept { id get_id() noexcept {
#if TD_WINDOWS #if TD_WINDOWS && TD_MSVC
#ifdef __MINGW32__ static_assert(std::is_same<decltype(thread_.native_handle()), HANDLE>::value,
return GetThreadId(reinterpret_cast<HANDLE>(thread_.native_handle())); "Expected HANDLE as native thread type");
#else
return GetThreadId(thread_.native_handle()); return GetThreadId(thread_.native_handle());
#endif
#else #else
return thread_.get_id(); return thread_.get_id();
#endif #endif
@ -140,7 +138,7 @@ class ThreadStl {
}; };
namespace this_thread_stl { namespace this_thread_stl {
#if TD_WINDOWS #if TD_WINDOWS && TD_MSVC
inline ThreadStl::id get_id() { inline ThreadStl::id get_id() {
return GetCurrentThreadId(); return GetCurrentThreadId();
} }