From cde5ce9ec6fe7eb634702578f50d2738bce3ac49 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 14 Nov 2024 15:18:37 -0700 Subject: [PATCH] QThreadData: make current() inline with a noexcept likely portion This helps compilers with codegen. Change-Id: I096db2bc39b21ee7605efffdaa257c6c658f38b4 Reviewed-by: Fabian Kosmale --- src/corelib/thread/qthread.cpp | 14 +++++++++----- src/corelib/thread/qthread_p.h | 11 ++++++++++- src/corelib/thread/qthread_unix.cpp | 9 ++++++--- src/corelib/thread/qthread_win.cpp | 9 ++++++--- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index f804ab8b9d3..64a6645db21 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -1119,12 +1119,16 @@ void QThread::setTerminationEnabled(bool) // No threads: so we can just use static variables Q_CONSTINIT static QThreadData *data = nullptr; -QThreadData *QThreadData::current() +QThreadData *QThreadData::currentThreadData() noexcept { - if (!data) { - data = new QThreadData; - data->thread = new QAdoptedThread(data); - } + return data; +} + +QThreadData *QThreadData::createCurrentThreadData() +{ + Q_ASSERT(!currentThreadData()); + data = new QThreadData; + data->thread = new QAdoptedThread(data); return data; } diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index 51299ee53b9..5f64293e467 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -308,7 +308,12 @@ public: } ~QThreadData(); - static Q_AUTOTEST_EXPORT QThreadData *current(); + static QThreadData *current() + { + if (QThreadData *data = currentThreadData()) Q_LIKELY_BRANCH + return data; + return createCurrentThreadData(); + } static void clearCurrentThreadData(); static QThreadData *get2(QThread *thread) { Q_ASSERT_X(thread != nullptr, "QThread", "internal error"); return thread->d_func()->data; } @@ -363,6 +368,10 @@ public: bool canWait = true; bool isAdopted = false; bool requiresCoreApplication = true; + +private: + static Q_AUTOTEST_EXPORT QThreadData *currentThreadData() noexcept Q_DECL_PURE_FUNCTION; + static Q_AUTOTEST_EXPORT QThreadData *createCurrentThreadData(); }; class QScopedScopeLevelCounter diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 832a21db48c..926b7aea400 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -225,11 +225,14 @@ void QThreadData::clearCurrentThreadData() set_thread_data(nullptr); } -QThreadData *QThreadData::current() +QThreadData *QThreadData::currentThreadData() noexcept { - if (QThreadData *data = get_thread_data(); Q_LIKELY(data)) - return data; + return get_thread_data(); +} +QThreadData *QThreadData::createCurrentThreadData() +{ + Q_ASSERT(!currentThreadData()); std::unique_ptr data = std::make_unique(); // This needs to be called prior to new QAdoptedThread() to avoid diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 947c1a4e475..8b902ec2cd6 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -96,11 +96,14 @@ void QThreadData::clearCurrentThreadData() set_thread_data(nullptr); } -QThreadData *QThreadData::current() +QThreadData *QThreadData::currentThreadData() noexcept { - if (QThreadData *data = get_thread_data(); Q_LIKELY(data)) - return data; + return get_thread_data(); +} +QThreadData *QThreadData::createCurrentThreadData() +{ + Q_ASSERT(!currentThreadData()); std::unique_ptr data = std::make_unique(); // This needs to be called prior to new QAdoptedThread() to avoid