diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-08-19 13:33:31 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-10-20 17:08:57 +0200 |
commit | 5e9b2ade678f37e43bfc2e3484f54cbbb5844d2e (patch) | |
tree | 6415a35c0a3bb9a2ca0f3e62e126986fb30c83a6 /src/corelib/thread/qthread.h | |
parent | ab5153aa0b09320dc5c96aa2b5a564ea1e6dbed3 (diff) |
Read a unique thread identifier from CPU registers
This is, depending on the implementation of pthread, significantly
cheaper than using a pthread library call.
Even if we don't know the assembler for an architecture, taking
the address of the thread_local variable is still faster.
As QThread::currentThreadId() is documented to be used internally
and not meant for application code, we don't have to care about
what exact value we return. Internally, we use it only to compare
thread IDs for equality, which this implementation is sufficient
for, even if a thread ID is re-used when one of the threads
terminate and a new thread starts (since the other thread is still
executing code). Besides, pthread_self documents [0] that a thread
ID may be reused, and that the returned pthread_t cannot be
portably compared using operator==(); using pthread_equal would
require adding a Qt thread-ID type that implements this correctly,
and would make things even slower.
[0] http://man7.org/linux/man-pages/man3/pthread_self.3.html
Change-Id: Id08e79b9b9c88976561f7cd36c66d43771fc4f24
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/thread/qthread.h')
-rw-r--r-- | src/corelib/thread/qthread.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index c7a6dc8f1a..8141f945b6 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -161,6 +161,7 @@ private: #if QT_CONFIG(cxx11_future) static QThread *createThreadImpl(std::future<void> &&future); #endif + static Qt::HANDLE currentThreadIdImpl() noexcept Q_DECL_PURE_FUNCTION; friend class QCoreApplication; friend class QThreadData; @@ -236,6 +237,38 @@ QThread *QThread::create(Function &&f) #endif // QT_CONFIG(cxx11_future) +/* + On architectures and platforms we know, interpret the thread control + block (TCB) as a unique identifier for a thread within a process. Otherwise, + fall back to a slower but safe implementation. + + As per the documentation of currentThreadId, we return an opaque handle + as a thread identifier, and application code is not supposed to use that + value for anything. In Qt we use the handle to check if threads are identical, + for which the TCB is sufficient. + + So we use the fastest possible way, rathern than spend time on returning + some pseudo-interoperable value. +*/ +inline Qt::HANDLE QThread::currentThreadId() noexcept +{ + Qt::HANDLE tid; // typedef to void* + Q_STATIC_ASSERT(sizeof(tid) == sizeof(void*)); + // See https://akkadia.org/drepper/tls.pdf for x86 ABI +#if defined(Q_PROCESSOR_X86_32) && defined(Q_OS_LINUX) // x86 32-bit always uses GS + __asm__("movl %%gs:0, %0" : "=r" (tid) : : ); +#elif defined(Q_PROCESSOR_X86_64) && defined(Q_OS_DARWIN64) + // 64bit macOS uses GS, see https://github.com/apple/darwin-xnu/blob/master/libsyscall/os/tsd.h + __asm__("movq %%gs:0, %0" : "=r" (tid) : : ); +#elif defined(Q_PROCESSOR_X86_64) && (defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)) + // x86_64 Linux, BSD uses FS + __asm__("movq %%fs:0, %0" : "=r" (tid) : : ); +#else + tid = currentThreadIdImpl(); +#endif + return tid; +} + QT_END_NAMESPACE #endif // QTHREAD_H |