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 5e9b2ade67

Change-Id: Iea51ef905b1cb7f91ca64b718d79bdc4f5c02c3a
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Lars Knoll 2021-03-01 11:51:21 +01:00 committed by Mårten Nordheim
parent 47abdbea81
commit 22c66e12e4
1 changed files with 25 additions and 0 deletions

View File

@ -49,6 +49,10 @@
# include <future> // for std::async
# include <functional> // 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 <intrin.h>
#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<quint8 *>(__readgsqword(0x30));
# endif
// Then read the thread ID
tid = *reinterpret_cast<Qt::HANDLE *>(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<quint8 *>(__readfsdword(0x18));
# endif
// Then read the thread ID
tid = *reinterpret_cast<Qt::HANDLE *>(tib + 0x24);
#else
tid = currentThreadIdImpl();
#endif