From 22c66e12e4c6841d51f505073a01e60917cf3174 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 1 Mar 2021 11:51:21 +0100 Subject: Provide an inline implementation of currentThreadId() on Windows As this method is rather critical for performance of some central parts of Qt, it really should be inline whereever possible. This commit adds an inline implementation for Windows 32 and 64 bit. Amends 5e9b2ade678f37e43bfc2e3484f54cbbb5844d2e Change-Id: Iea51ef905b1cb7f91ca64b718d79bdc4f5c02c3a Reviewed-by: Volker Hilsheimer Reviewed-by: Lars Knoll Reviewed-by: Thiago Macieira --- src/corelib/thread/qthread.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index ab32891a59..e5db44638d 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -49,6 +49,10 @@ # include // for std::async # include // for std::invoke; no guard needed as it's a C++98 header #endif +// internal compiler error with mingw 8.1 +#if defined(Q_CC_MSVC) && defined(Q_PROCESSOR_X86) +#include +#endif QT_BEGIN_NAMESPACE @@ -194,6 +198,27 @@ inline Qt::HANDLE QThread::currentThreadId() noexcept #elif defined(Q_PROCESSOR_X86_64) && (defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)) && !defined(Q_OS_ANDROID) // x86_64 Linux, BSD uses FS __asm__("movq %%fs:0, %0" : "=r" (tid) : : ); +#elif defined(Q_PROCESSOR_X86_64) && defined(Q_OS_WIN) + // See https://en.wikipedia.org/wiki/Win32_Thread_Information_Block + // First get the pointer to the TIB + quint8 *tib; +# if defined(Q_CC_MINGW) // internal compiler error when using the intrinsics + __asm__("movq %%gs:0x30, %0" : "=r" (tib) : :); +# else + tib = reinterpret_cast(__readgsqword(0x30)); +# endif + // Then read the thread ID + tid = *reinterpret_cast(tib + 0x48); +#elif defined(Q_PROCESSOR_X86_32) && defined(Q_OS_WIN) + // First get the pointer to the TIB + quint8 *tib; +# if defined(Q_CC_MINGW) // internal compiler error when using the intrinsics + __asm__("movl %%fs:0x18, %0" : "=r" (tib) : :); +# else + tib = reinterpret_cast(__readfsdword(0x18)); +# endif + // Then read the thread ID + tid = *reinterpret_cast(tib + 0x24); #else tid = currentThreadIdImpl(); #endif -- cgit v1.2.3