QThread: use SetThreadDescription on Windows

Starting with Windows 10 1607 there's a modern API to set a thread's
name on Windows (replacing the RaiseException hack).

Change-Id: I45b7abef7b082b9f239b2ac948bb79cce44cdb5e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Giuseppe D'Angelo 2024-02-23 15:21:37 +01:00
parent 918ac58a89
commit 6848969cbc
1 changed files with 5 additions and 36 deletions

View File

@ -18,6 +18,7 @@
# define _MT
#endif // _MT
#include <process.h>
#include <processthreadsapi.h>
QT_BEGIN_NAMESPACE
@ -212,39 +213,6 @@ DWORD WINAPI qt_adopted_thread_watcher_function(LPVOID)
return 0;
}
#if !defined(QT_NO_DEBUG) && defined(Q_CC_MSVC)
#ifndef Q_OS_WIN64
# define ULONG_PTR DWORD
#endif
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in user addr space)
HANDLE dwThreadID; // thread ID (-1=caller thread)
DWORD dwFlags; // reserved for future use, must be zero
} THREADNAME_INFO;
void qt_set_thread_name(HANDLE threadId, LPCSTR threadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = threadId;
info.dwFlags = 0;
__try
{
RaiseException(0x406D1388, 0, sizeof(info)/sizeof(DWORD),
reinterpret_cast<const ULONG_PTR*>(&info));
}
__except (EXCEPTION_CONTINUE_EXECUTION)
{
}
}
#endif // !QT_NO_DEBUG && Q_CC_MSVC
/**************************************************************************
** QThreadPrivate
*************************************************************************/
@ -280,9 +248,10 @@ unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(voi
#if !defined(QT_NO_DEBUG) && defined(Q_CC_MSVC)
// sets the name of the current thread.
qt_set_thread_name(HANDLE(-1), thr->d_func()->objectName.isEmpty()
? thr->metaObject()->className()
: std::exchange(thr->d_func()->objectName, {}).toLocal8Bit().constData());
QString threadName = std::exchange(thr->d_func()->objectName, {});
if (Q_LIKELY(threadName.isEmpty()))
threadName = QString::fromUtf8(thr->metaObject()->className());
SetThreadDescription(GetCurrentThread(), reinterpret_cast<const wchar_t *>(threadName.utf16()));
#endif
emit thr->started(QThread::QPrivateSignal());