Skip to content

Commit 603d5ca

Browse files
committed
Fix gui hangup
1 parent 4780cd2 commit 603d5ca

2 files changed

Lines changed: 62 additions & 33 deletions

File tree

gui/threadhandler.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
ThreadHandler::ThreadHandler(QObject *parent) :
4040
QObject(parent)
4141
{
42-
setThreadCount(1);
4342
}
4443

4544
ThreadHandler::~ThreadHandler()
@@ -54,7 +53,7 @@ void ThreadHandler::clearFiles()
5453
mAnalyseWholeProgram = false;
5554
mCtuInfo.clear();
5655
mAddonsAndTools.clear();
57-
mSuppressions.clear();
56+
mSuppressionsUI.clear();
5857
}
5958

6059
void ThreadHandler::setFiles(const QStringList &files)
@@ -83,6 +82,14 @@ void ThreadHandler::setCheckFiles(const QStringList& files)
8382
}
8483
}
8584

85+
void ThreadHandler::setupCheckThread(CheckThread &thread) const
86+
{
87+
thread.setAddonsAndTools(mCheckAddonsAndTools);
88+
thread.setSuppressions(mSuppressionsUI);
89+
thread.setClangIncludePaths(mClangIncludePaths);
90+
thread.setSettings(mCheckSettings, mCheckSuppressions);
91+
}
92+
8693
void ThreadHandler::check(const Settings &settings, const std::shared_ptr<Suppressions>& supprs)
8794
{
8895
if (mResults.getFileCount() == 0 || mRunningThreadCount > 0 || settings.jobs == 0) {
@@ -91,25 +98,25 @@ void ThreadHandler::check(const Settings &settings, const std::shared_ptr<Suppre
9198
return;
9299
}
93100

94-
setThreadCount(settings.jobs);
101+
mCheckSettings = settings;
102+
mCheckSuppressions = supprs;
103+
104+
createThreads(mCheckSettings.jobs);
95105

96106
mRunningThreadCount = mThreads.size();
97107
mRunningThreadCount = std::min(mResults.getFileCount(), mRunningThreadCount);
98108

99-
QStringList addonsAndTools = mAddonsAndTools;
100-
for (const std::string& addon: settings.addons) {
109+
mCheckAddonsAndTools = mAddonsAndTools;
110+
for (const std::string& addon: mCheckSettings.addons) {
101111
QString s = QString::fromStdString(addon);
102-
if (!addonsAndTools.contains(s))
103-
addonsAndTools << s;
112+
if (!mCheckAddonsAndTools.contains(s))
113+
mCheckAddonsAndTools << s;
104114
}
105115

106116
mCtuInfo.clear();
107117

108118
for (int i = 0; i < mRunningThreadCount; i++) {
109-
mThreads[i]->setAddonsAndTools(addonsAndTools);
110-
mThreads[i]->setSuppressions(mSuppressions);
111-
mThreads[i]->setClangIncludePaths(mClangIncludePaths);
112-
mThreads[i]->setSettings(settings, supprs);
119+
setupCheckThread(*mThreads[i]);
113120
mThreads[i]->start();
114121
}
115122

@@ -123,14 +130,12 @@ void ThreadHandler::check(const Settings &settings, const std::shared_ptr<Suppre
123130

124131
bool ThreadHandler::isChecking() const
125132
{
126-
return mRunningThreadCount > 0;
133+
return mRunningThreadCount > 0 || mAnalyseWholeProgram;
127134
}
128135

129-
void ThreadHandler::setThreadCount(const int count)
136+
void ThreadHandler::createThreads(const int count)
130137
{
131-
if (mRunningThreadCount > 0 ||
132-
count == mThreads.size() ||
133-
count <= 0) {
138+
if (mRunningThreadCount > 0 || count <= 0) {
134139
return;
135140
}
136141

@@ -140,9 +145,9 @@ void ThreadHandler::setThreadCount(const int count)
140145
for (int i = mThreads.size(); i < count; i++) {
141146
mThreads << new CheckThread(mResults);
142147
connect(mThreads.last(), &CheckThread::done,
143-
this, &ThreadHandler::threadDone);
148+
this, &ThreadHandler::threadDone, Qt::QueuedConnection);
144149
connect(mThreads.last(), &CheckThread::fileChecked,
145-
&mResults, &ThreadResult::fileChecked);
150+
&mResults, &ThreadResult::fileChecked, Qt::QueuedConnection);
146151
}
147152
}
148153

@@ -151,7 +156,7 @@ void ThreadHandler::removeThreads()
151156
{
152157
for (CheckThread* thread : mThreads) {
153158
if (thread->isRunning()) {
154-
thread->terminate();
159+
thread->stop();
155160
thread->wait();
156161
}
157162
disconnect(thread, &CheckThread::done,
@@ -162,19 +167,22 @@ void ThreadHandler::removeThreads()
162167
}
163168

164169
mThreads.clear();
165-
mAnalyseWholeProgram = false;
166170
}
167171

168172
void ThreadHandler::threadDone()
169173
{
170-
if (mRunningThreadCount == 1 && mAnalyseWholeProgram) {
174+
mRunningThreadCount--;
175+
176+
if (mRunningThreadCount == 0 && mAnalyseWholeProgram) {
177+
createThreads(1);
178+
mRunningThreadCount = 1;
179+
setupCheckThread(*mThreads[0]);
171180
mThreads[0]->analyseWholeProgram(mLastFiles, mCtuInfo);
172181
mAnalyseWholeProgram = false;
173182
mCtuInfo.clear();
174183
return;
175184
}
176185

177-
mRunningThreadCount--;
178186
if (mRunningThreadCount == 0) {
179187
emit done();
180188

@@ -185,6 +193,9 @@ void ThreadHandler::threadDone()
185193
mLastCheckTime = mCheckStartTime;
186194
mCheckStartTime = QDateTime();
187195
}
196+
197+
mCheckAddonsAndTools.clear();
198+
mCheckSuppressions.reset();
188199
}
189200
}
190201

@@ -215,7 +226,7 @@ void ThreadHandler::initialize(const ResultsView *view)
215226

216227
void ThreadHandler::loadSettings(const QSettings &settings)
217228
{
218-
setThreadCount(settings.value(SETTINGS_CHECK_THREADS, 1).toInt());
229+
createThreads(settings.value(SETTINGS_CHECK_THREADS, 1).toInt());
219230
}
220231

221232
void ThreadHandler::saveSettings(QSettings &settings) const

gui/threadhandler.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef THREADHANDLER_H
2121
#define THREADHANDLER_H
2222

23+
#include "settings.h"
2324
#include "suppressions.h"
2425
#include "threadresult.h"
2526

@@ -37,7 +38,6 @@
3738
class ResultsView;
3839
class CheckThread;
3940
class QSettings;
40-
class Settings;
4141
class ImportProject;
4242
class ErrorItem;
4343

@@ -55,18 +55,12 @@ class ThreadHandler : public QObject {
5555
explicit ThreadHandler(QObject *parent = nullptr);
5656
~ThreadHandler() override;
5757

58-
/**
59-
* @brief Set the number of threads to use
60-
* @param count The number of threads to use
61-
*/
62-
void setThreadCount(int count);
63-
6458
/**
6559
* @brief Initialize the threads (connect all signals to resultsview's slots)
6660
*
6761
* @param view View to show error results
6862
*/
69-
void initialize(const ResultsView *view);
63+
virtual void initialize(const ResultsView *view);
7064

7165
/**
7266
* @brief Load settings
@@ -85,7 +79,7 @@ class ThreadHandler : public QObject {
8579
}
8680

8781
void setSuppressions(const QList<SuppressionList::Suppression> &s) {
88-
mSuppressions = s;
82+
mSuppressionsUI = s;
8983
}
9084

9185
void setClangIncludePaths(const QStringList &s) {
@@ -235,12 +229,24 @@ protected slots:
235229
*/
236230
int mScanDuration{};
237231

232+
/**
233+
* @brief Create checker threads
234+
* @param count The number of threads to spawn
235+
*/
236+
void createThreads(int count);
237+
238238
/**
239239
* @brief Function to delete all threads
240240
*
241241
*/
242242
void removeThreads();
243243

244+
/*
245+
* @brief Apply check settings to a checker thread
246+
* @param thread The thread to setup
247+
*/
248+
void setupCheckThread(CheckThread &thread) const;
249+
244250
/**
245251
* @brief Thread results are stored here
246252
*
@@ -259,12 +265,24 @@ protected slots:
259265
*/
260266
int mRunningThreadCount{};
261267

268+
/**
269+
* @brief A whole program check is queued by check()
270+
*/
262271
bool mAnalyseWholeProgram{};
263272
std::string mCtuInfo;
264273

265274
QStringList mAddonsAndTools;
266-
QList<SuppressionList::Suppression> mSuppressions;
275+
QList<SuppressionList::Suppression> mSuppressionsUI;
267276
QStringList mClangIncludePaths;
277+
278+
/// @{
279+
/**
280+
* @brief Settings specific to the current analysis
281+
*/
282+
QStringList mCheckAddonsAndTools;
283+
Settings mCheckSettings;
284+
std::shared_ptr<Suppressions> mCheckSuppressions;
285+
/// @}
268286
private:
269287

270288
/**

0 commit comments

Comments
 (0)