diff --git a/src/libs/extensionsystem/pluginmanager.cpp b/src/libs/extensionsystem/pluginmanager.cpp index 01811c33c54..5937a6d639d 100644 --- a/src/libs/extensionsystem/pluginmanager.cpp +++ b/src/libs/extensionsystem/pluginmanager.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #ifdef WITH_TESTS #include @@ -851,7 +852,7 @@ bool PluginManager::finishScenario() // Waits until the running scenario is fully initialized void PluginManager::waitForScenarioFullyInitialized() { - if (QThread::currentThread() == qApp->thread()) { + if (isMainThread()) { qWarning("The waitForScenarioFullyInitialized() function can't be called from main thread."); return; } @@ -1378,7 +1379,7 @@ void PluginManagerPrivate::shutdown() #ifdef WITH_TESTS if (PluginManager::isScenarioRunning("TestModelManagerInterface")) { qDebug() << "Point 2: Expect the next call to Point 3 triggers a crash"; - QThread::currentThread()->sleep(5); + QThread::sleep(5); } #endif if (!allObjects.isEmpty()) { diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp index c39c48415dc..fc31c30e1b7 100644 --- a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp +++ b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp @@ -7,7 +7,6 @@ #include "qmljsinterpreter.h" #include "qmljsmodelmanagerinterface.h" #include "qmljsplugindumper.h" -#include "qmljstypedescriptionreader.h" #include "qmljsdialect.h" #include "qmljsviewercontext.h" #include "qmljsutils.h" @@ -989,7 +988,7 @@ void ModelManagerInterface::parseLoop(QSet &scannedPaths, ExtensionSystem::PluginManager::waitForScenarioFullyInitialized(); if (ExtensionSystem::PluginManager::finishScenario()) { qDebug() << "Point 1: Shutdown triggered"; - QThread::currentThread()->sleep(2); + QThread::sleep(2); qDebug() << "Point 3: If Point 2 was already reached, expect a crash now"; } } diff --git a/src/libs/utils/processreaper.cpp b/src/libs/utils/processreaper.cpp index 8f247ba3094..2638b09b1ed 100644 --- a/src/libs/utils/processreaper.cpp +++ b/src/libs/utils/processreaper.cpp @@ -4,6 +4,7 @@ #include "processreaper.h" #include "processutils.h" #include "qtcassert.h" +#include "threadutils.h" #include #include @@ -224,7 +225,7 @@ ProcessReaper::ProcessReaper() ProcessReaper::~ProcessReaper() { - QTC_CHECK(QThread::currentThread() == qApp->thread()); + QTC_CHECK(isMainThread()); QMutexLocker locker(&s_instanceMutex); instance()->m_private->waitForFinished(); m_thread.quit(); diff --git a/src/libs/utils/qtcprocess.cpp b/src/libs/utils/qtcprocess.cpp index 7ddce617637..f0f6170f882 100644 --- a/src/libs/utils/qtcprocess.cpp +++ b/src/libs/utils/qtcprocess.cpp @@ -12,6 +12,7 @@ #include "processreaper.h" #include "processutils.h" #include "terminalprocess_p.h" +#include "threadutils.h" #include #include @@ -62,25 +63,25 @@ public: timer.start(); auto cleanup = qScopeGuard([this, &timer] { const qint64 currentNsecs = timer.nsecsElapsed(); - const bool isMainThread = QThread::currentThread() == qApp->thread(); + const bool mainThread = isMainThread(); const int hitThisAll = m_hitThisAll.fetch_add(1) + 1; const int hitAllAll = m_hitAllAll.fetch_add(1) + 1; - const int hitThisMain = isMainThread + const int hitThisMain = mainThread ? m_hitThisMain.fetch_add(1) + 1 : m_hitThisMain.load(); - const int hitAllMain = isMainThread + const int hitAllMain = mainThread ? m_hitAllMain.fetch_add(1) + 1 : m_hitAllMain.load(); const qint64 totalThisAll = toMs(m_totalThisAll.fetch_add(currentNsecs) + currentNsecs); const qint64 totalAllAll = toMs(m_totalAllAll.fetch_add(currentNsecs) + currentNsecs); - const qint64 totalThisMain = toMs(isMainThread + const qint64 totalThisMain = toMs(mainThread ? m_totalThisMain.fetch_add(currentNsecs) + currentNsecs : m_totalThisMain.load()); - const qint64 totalAllMain = toMs(isMainThread + const qint64 totalAllMain = toMs(mainThread ? m_totalAllMain.fetch_add(currentNsecs) + currentNsecs : m_totalAllMain.load()); printMeasurement(QLatin1String(m_functionName), hitThisAll, toMs(currentNsecs), - totalThisAll, hitAllAll, totalAllAll, isMainThread, + totalThisAll, hitAllAll, totalAllAll, mainThread, hitThisMain, totalThisMain, hitAllMain, totalAllMain); }); return std::invoke(std::forward(function), std::forward(args)...); @@ -1212,7 +1213,7 @@ void QtcProcess::setRemoteProcessHooks(const DeviceProcessHooks &hooks) static bool askToKill(const QString &command) { #ifdef QT_GUI_LIB - if (QThread::currentThread() != QCoreApplication::instance()->thread()) + if (!isMainThread()) return true; const QString title = QtcProcess::tr("Process Not Responding"); QString msg = command.isEmpty() ? @@ -1775,13 +1776,6 @@ void QtcProcess::setWriteData(const QByteArray &writeData) d->m_setup.m_writeData = writeData; } -#ifdef QT_GUI_LIB -static bool isGuiThread() -{ - return QThread::currentThread() == QCoreApplication::instance()->thread(); -} -#endif - void QtcProcess::runBlocking(EventLoopMode eventLoopMode) { // Attach a dynamic property with info about blocking type @@ -1802,7 +1796,7 @@ void QtcProcess::runBlocking(EventLoopMode eventLoopMode) timer.setInterval(1000); timer.start(); #ifdef QT_GUI_LIB - if (isGuiThread()) + if (isMainThread()) QApplication::setOverrideCursor(Qt::WaitCursor); #endif QEventLoop eventLoop(this); @@ -1812,7 +1806,7 @@ void QtcProcess::runBlocking(EventLoopMode eventLoopMode) d->m_eventLoop = nullptr; timer.stop(); #ifdef QT_GUI_LIB - if (isGuiThread()) + if (isMainThread()) QApplication::restoreOverrideCursor(); #endif } diff --git a/src/libs/utils/singleton.cpp b/src/libs/utils/singleton.cpp index 474b6bd0e81..cbf7aa21789 100644 --- a/src/libs/utils/singleton.cpp +++ b/src/libs/utils/singleton.cpp @@ -3,10 +3,9 @@ #include "qtcassert.h" #include "singleton.h" +#include "threadutils.h" -#include #include -#include #include @@ -45,7 +44,7 @@ SingletonStaticData &Singleton::staticData(std::type_index index) // only. void Singleton::deleteAll() { - QTC_ASSERT(QThread::currentThread() == qApp->thread(), return); + QTC_ASSERT(isMainThread(), return); QList oldList; { QMutexLocker locker(&s_mutex); diff --git a/src/plugins/cppeditor/stringtable.cpp b/src/plugins/cppeditor/stringtable.cpp index 737c3dd3d0a..6f0ef89b18a 100644 --- a/src/plugins/cppeditor/stringtable.cpp +++ b/src/plugins/cppeditor/stringtable.cpp @@ -119,7 +119,7 @@ void StringTablePrivate::GC(QFutureInterface &futureInterface) #ifdef WITH_TESTS if (ExtensionSystem::PluginManager::isScenarioRunning("TestStringTable")) { if (ExtensionSystem::PluginManager::finishScenario()) - QThread::currentThread()->sleep(5); + QThread::sleep(5); } #endif diff --git a/src/plugins/projectexplorer/projectnodes.cpp b/src/plugins/projectexplorer/projectnodes.cpp index 68d482264af..27223f066ed 100644 --- a/src/plugins/projectexplorer/projectnodes.cpp +++ b/src/plugins/projectexplorer/projectnodes.cpp @@ -3,11 +3,9 @@ #include "projectnodes.h" -#include "buildconfiguration.h" #include "buildsystem.h" #include "project.h" #include "projectexplorerconstants.h" -#include "projecttree.h" #include "target.h" #include @@ -21,14 +19,12 @@ #include #include #include +#include #include #include #include #include -#include -#include -#include #include @@ -445,7 +441,7 @@ QString FolderNode::displayName() const */ QIcon FolderNode::icon() const { - QTC_CHECK(QThread::currentThread() == QCoreApplication::instance()->thread()); + QTC_CHECK(isMainThread()); // Instantiating the Icon provider is expensive. if (auto strPtr = std::get_if(&m_icon)) { @@ -1063,7 +1059,7 @@ DirectoryIcon::DirectoryIcon(const QString &overlay) */ QIcon DirectoryIcon::icon() const { - QTC_CHECK(QThread::currentThread() == QCoreApplication::instance()->thread()); + QTC_CHECK(isMainThread()); const auto it = m_cache.find(m_overlay); if (it != m_cache.end()) return it.value(); diff --git a/src/plugins/projectexplorer/taskhub.cpp b/src/plugins/projectexplorer/taskhub.cpp index e05d7322428..3bb63b42b8c 100644 --- a/src/plugins/projectexplorer/taskhub.cpp +++ b/src/plugins/projectexplorer/taskhub.cpp @@ -9,10 +9,10 @@ #include #include #include +#include #include #include -#include using namespace Utils; @@ -131,7 +131,7 @@ void TaskHub::addTask(Task::TaskType type, const QString &description, Utils::Id void TaskHub::addTask(Task task) { - if (QThread::currentThread() != qApp->thread()) { + if (!isMainThread()) { QMetaObject::invokeMethod(qApp, [task = std::move(task)] { TaskHub::addTask(task); }); diff --git a/src/plugins/projectexplorer/windebuginterface.cpp b/src/plugins/projectexplorer/windebuginterface.cpp index a27d3500959..8952f532468 100644 --- a/src/plugins/projectexplorer/windebuginterface.cpp +++ b/src/plugins/projectexplorer/windebuginterface.cpp @@ -6,6 +6,7 @@ #ifdef Q_OS_WIN #include +#include #include #include @@ -146,7 +147,7 @@ void WinDebugInterface::emitReadySignal() void WinDebugInterface::dispatchDebugOutput() { // Called in the thread this object was created in, not in the WinDebugInterfaceThread. - QTC_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread(), return); + QTC_ASSERT(Utils::isMainThread(), return); static size_t maxMessagesToSend = 100; std::vector> output; diff --git a/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp b/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp index dfc5667199b..9ccabce38f5 100644 --- a/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp +++ b/src/plugins/qmldesigner/designercore/instances/nodeinstanceview.cpp @@ -29,7 +29,6 @@ #include "nodeabstractproperty.h" #include "nodeinstanceserverproxy.h" #include "nodelistproperty.h" -#include "nodeproperty.h" #include "pixmapchangedcommand.h" #include "puppettocreatorcommand.h" #include "qml3dnode.h" @@ -84,19 +83,18 @@ #include #include #include +#include #include -#include -#include -#include -#include -#include #include #include +#include +#include +#include #include -#include -#include +#include +#include enum { debug = false @@ -261,7 +259,7 @@ void NodeInstanceView::modelAttached(Model *model) // If model gets attached on non-main thread of the application, do not attempt to monitor // file changes. Such models are typically short lived for specific purpose, and timers // will not work at all, if the thread is not based on QThread. - if (QThread::currentThread() == qApp->thread()) { + if (Utils::isMainThread()) { m_generateQsbFilesTimer.stop(); m_qsbTargets.clear(); updateQsbPathToFilterMap(); diff --git a/src/plugins/resourceeditor/resourceeditorplugin.cpp b/src/plugins/resourceeditor/resourceeditorplugin.cpp index ddfaa7d27a4..e96bf888cbe 100644 --- a/src/plugins/resourceeditor/resourceeditorplugin.cpp +++ b/src/plugins/resourceeditor/resourceeditorplugin.cpp @@ -28,10 +28,8 @@ #include #include #include -#include #include -#include #include #include #include diff --git a/src/tools/processlauncher/CMakeLists.txt b/src/tools/processlauncher/CMakeLists.txt index 25b3b461ee2..6efcafbc005 100644 --- a/src/tools/processlauncher/CMakeLists.txt +++ b/src/tools/processlauncher/CMakeLists.txt @@ -21,6 +21,8 @@ add_qtc_executable(qtcreator_processlauncher ${UTILSDIR}/qtcassert.h ${UTILSDIR}/singleton.cpp ${UTILSDIR}/singleton.h + ${UTILSDIR}/threadutils.cpp + ${UTILSDIR}/threadutils.h ) if (MSVC) diff --git a/src/tools/processlauncher/processlauncher.qbs b/src/tools/processlauncher/processlauncher.qbs index f56ff489cb5..757d50f9bb6 100644 --- a/src/tools/processlauncher/processlauncher.qbs +++ b/src/tools/processlauncher/processlauncher.qbs @@ -40,6 +40,8 @@ QtcTool { "qtcassert.h", "singleton.cpp", "singleton.h", + "threadutils.cpp", + "threadutils.h", ] } }