QThreadData: make current() inline with a noexcept likely portion

This helps compilers with codegen.

Change-Id: I096db2bc39b21ee7605efffdaa257c6c658f38b4
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2024-11-14 15:18:37 -07:00
parent 38f77d09e8
commit cde5ce9ec6
4 changed files with 31 additions and 12 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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<QThreadData>();
// This needs to be called prior to new QAdoptedThread() to avoid

View File

@ -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<QThreadData>();
// This needs to be called prior to new QAdoptedThread() to avoid