QThread: merge some Unix/Windows/no-thread code for QAdoptedThread
It was duplicated in all three files. The remaining code is very similar, but I don't think we want to merge it any further. For one thing, we must set the thread-local variable before QAdoptedThread calls the QObject constructor. Change-Id: Iac9f7f7528085a1137d7fffdecf080a2b6e1aefe Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
d25ff2c7e2
commit
f2b5c779cd
@ -13,6 +13,8 @@
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
/*
|
||||
QPostEventList
|
||||
*/
|
||||
@ -113,6 +115,20 @@ QAbstractEventDispatcher *QThreadData::createEventDispatcher()
|
||||
QAdoptedThread::QAdoptedThread(QThreadData *data)
|
||||
: QThread(*new QThreadPrivate(data))
|
||||
{
|
||||
// avoid a cyclic reference count: QThreadData owns this QAdoptedThread
|
||||
// object but QObject's constructor increased the count
|
||||
data->deref();
|
||||
|
||||
data->isAdopted = true;
|
||||
Qt::HANDLE id = QThread::currentThreadId();
|
||||
data->threadId.storeRelaxed(id);
|
||||
if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
|
||||
// we are the main thread
|
||||
QCoreApplicationPrivate::theMainThread.storeRelease(this);
|
||||
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(id);
|
||||
setObjectName(u"Qt mainThread"_s);
|
||||
}
|
||||
|
||||
// thread should be running and not finished for the lifetime
|
||||
// of the application (even if QCoreApplication goes away)
|
||||
#if QT_CONFIG(thread)
|
||||
@ -1130,15 +1146,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
|
||||
if (!data && createIfNecessary) {
|
||||
data = new QThreadData;
|
||||
data->thread = new QAdoptedThread(data);
|
||||
data->threadId.storeRelaxed(Qt::HANDLE(data->thread.loadAcquire()));
|
||||
data->deref();
|
||||
data->isAdopted = true;
|
||||
if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
|
||||
auto *mainThread = data->thread.loadRelaxed();
|
||||
mainThread->setObjectName("Qt mainThread");
|
||||
QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
|
||||
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed());
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
@ -363,7 +363,7 @@ class QAdoptedThread : public QThread
|
||||
Q_DECLARE_PRIVATE(QThread)
|
||||
|
||||
public:
|
||||
QAdoptedThread(QThreadData *data = nullptr);
|
||||
QAdoptedThread(QThreadData *data);
|
||||
~QAdoptedThread();
|
||||
void init();
|
||||
|
||||
|
@ -245,15 +245,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
|
||||
data = nullptr;
|
||||
QT_RETHROW;
|
||||
}
|
||||
data->deref();
|
||||
data->isAdopted = true;
|
||||
data->threadId.storeRelaxed(QThread::currentThreadId());
|
||||
if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
|
||||
auto *mainThread = data->thread.loadRelaxed();
|
||||
mainThread->setObjectName("Qt mainThread");
|
||||
QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
|
||||
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed());
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
@ -107,16 +107,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
|
||||
threadData = 0;
|
||||
QT_RETHROW;
|
||||
}
|
||||
threadData->deref();
|
||||
threadData->isAdopted = true;
|
||||
threadData->threadId.storeRelaxed(reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId())));
|
||||
|
||||
if (!QCoreApplicationPrivate::theMainThreadId) {
|
||||
auto *mainThread = threadData->thread.loadRelaxed();
|
||||
mainThread->setObjectName("Qt mainThread");
|
||||
QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
|
||||
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(threadData->threadId.loadRelaxed());
|
||||
}
|
||||
}
|
||||
return threadData;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user