From 7b38f466e0e78c3c7f73877c633bf9c847f6513b Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Fri, 9 Jul 2021 09:41:49 +0200 Subject: QProperty: Only try to avoid TLS access if currentThreadId is faster We will not gain anything if we have to do multiple function calls to obtain the thread id. Therefore we introduce a macro to signal that we have a fast implementation of currentThreadId, and only use the function if it is defined. Change-Id: I3347489ea91992896bb753b796ae26e391c2c99c Reviewed-by: Lars Knoll (cherry picked from commit 5889985c8c8e8cc676de4480ad95979287860b96) --- src/corelib/kernel/qproperty.cpp | 10 +++++++++- src/corelib/thread/qthread.h | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index 82ab3b9b59..3123858d83 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -523,8 +523,12 @@ void QPropertyBindingData::notifyObservers(QUntypedPropertyData *propertyDataPtr if (QPropertyObserverPointer observer = d.firstObserver()) { auto status = storage ? storage->bindingStatus : nullptr; QPropertyDelayedNotifications *delay; +#ifndef QT_HAS_FAST_CURRENT_THREAD_ID + status = &bindingStatus; +#else if (!status || status->threadId != QThread::currentThreadId()) status = &bindingStatus; +#endif delay = status->groupUpdateData; if (delay) { delay->addProperty(this, propertyDataPtr); @@ -2171,12 +2175,16 @@ void QBindingStorage::registerDependency_helper(const QUntypedPropertyData *data Q_ASSERT(bindingStatus); // Use ::bindingStatus to get the binding from TLS. This is required, so that reads from // another thread do not register as dependencies - const bool threadMatches = (QThread::currentThreadId() == bindingStatus->threadId); QtPrivate::BindingEvaluationState *currentBinding; +#ifdef QT_HAS_FAST_CURRENT_THREAD_ID + const bool threadMatches = (QThread::currentThreadId() == bindingStatus->threadId); if (Q_LIKELY(threadMatches)) currentBinding = bindingStatus->currentlyEvaluatingBinding; else currentBinding = QT_PREPEND_NAMESPACE(bindingStatus).currentlyEvaluatingBinding; +#else + currentBinding = QT_PREPEND_NAMESPACE(bindingStatus).currentlyEvaluatingBinding; +#endif QUntypedPropertyData *dd = const_cast(data); if (!currentBinding) return; diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index 51cd3eadcc..e48354dedb 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -186,6 +186,8 @@ QThread *QThread::create(Function &&f, Args &&... args) */ inline Qt::HANDLE QThread::currentThreadId() noexcept { + // define is undefed if we have to fall back to currentThreadIdImpl +#define QT_HAS_FAST_CURRENT_THREAD_ID Qt::HANDLE tid; // typedef to void* static_assert(sizeof(tid) == sizeof(void*)); // See https://akkadia.org/drepper/tls.pdf for x86 ABI @@ -219,6 +221,7 @@ inline Qt::HANDLE QThread::currentThreadId() noexcept // Then read the thread ID tid = *reinterpret_cast(tib + 0x24); #else +#undef QT_HAS_FAST_CURRENT_THREAD_ID tid = currentThreadIdImpl(); #endif return tid; -- cgit v1.2.3