Remove the dead code for blocking methods from QtConcurrent

After 79fd1cb2c6 the methods for running
QtConcurrent algorithms in the blocking mode aren't used anymore. Since
ThreadEngineBase and ThreadEngineStarter classes aren't meant to be used
externally, it should be fine to remove startBlocking() methods now.

Removed the unused code and adjusted the tests accordingly.

Change-Id: Ifb13820ce207869d6f720bcb5be8d35bb355fe33
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
This commit is contained in:
Sona Kurazyan 2021-06-10 17:38:27 +02:00
parent 7dd4c93748
commit 1bf75f2a66
4 changed files with 29 additions and 174 deletions

View File

@ -176,39 +176,6 @@ void ThreadEngineBase::startSingleThreaded()
finish(); finish();
} }
void ThreadEngineBase::startBlocking()
{
start();
barrier.acquire();
startThreads();
bool throttled = false;
#ifndef QT_NO_EXCEPTIONS
try {
#endif
while (threadFunction() == ThrottleThread) {
if (threadThrottleExit()) {
throttled = true;
break;
}
}
#ifndef QT_NO_EXCEPTIONS
} catch (QException &e) {
handleException(e);
} catch (...) {
handleException(QUnhandledException(std::current_exception()));
}
#endif
if (throttled == false) {
barrier.release();
}
barrier.wait();
finish();
exceptionStore.throwPossibleException();
}
void ThreadEngineBase::startThread() void ThreadEngineBase::startThread()
{ {
startThreadInternal(); startThreadInternal();

View File

@ -91,7 +91,6 @@ public:
ThreadEngineBase(QThreadPool *pool); ThreadEngineBase(QThreadPool *pool);
virtual ~ThreadEngineBase(); virtual ~ThreadEngineBase();
void startSingleThreaded(); void startSingleThreaded();
void startBlocking();
void startThread(); void startThread();
bool isCanceled(); bool isCanceled();
void waitForResume(); void waitForResume();
@ -153,15 +152,6 @@ public:
return result(); return result();
} }
// Runs the user algorithm using multiple threads.
// This function blocks until the algorithm is finished,
// and then returns the result.
T *startBlocking()
{
ThreadEngineBase::startBlocking();
return result();
}
// Runs the user algorithm using multiple threads. // Runs the user algorithm using multiple threads.
// Does not block, returns a future. // Does not block, returns a future.
QFuture<T> startAsynchronously() QFuture<T> startAsynchronously()
@ -242,13 +232,6 @@ class ThreadEngineStarter : public ThreadEngineStarterBase<T>
public: public:
ThreadEngineStarter(TypedThreadEngine *eng) ThreadEngineStarter(TypedThreadEngine *eng)
: Base(eng) { } : Base(eng) { }
T startBlocking()
{
T t = *this->threadEngine->startBlocking();
delete this->threadEngine;
return t;
}
}; };
// Full template specialization where T is void. // Full template specialization where T is void.
@ -258,12 +241,6 @@ class ThreadEngineStarter<void> : public ThreadEngineStarterBase<void>
public: public:
ThreadEngineStarter(ThreadEngine<void> *_threadEngine) ThreadEngineStarter(ThreadEngine<void> *_threadEngine)
: ThreadEngineStarterBase<void>(_threadEngine) {} : ThreadEngineStarterBase<void>(_threadEngine) {}
void startBlocking()
{
this->threadEngine->startBlocking();
delete this->threadEngine;
}
}; };
//! [qtconcurrentthreadengine-1] //! [qtconcurrentthreadengine-1]

View File

@ -126,7 +126,8 @@ public:
void tst_QtConcurrentIterateKernel::instantiate() void tst_QtConcurrentIterateKernel::instantiate()
{ {
startThreadEngine(new PrintFor(0, 40)).startBlocking(); auto future = startThreadEngine(new PrintFor(0, 40)).startAsynchronously();
future.waitForFinished();
QCOMPARE(iterations.loadRelaxed(), 40); QCOMPARE(iterations.loadRelaxed(), 40);
} }
@ -165,8 +166,10 @@ void tst_QtConcurrentIterateKernel::stresstest()
const int times = 50; const int times = 50;
for (int i = 0; i < times; ++i) { for (int i = 0; i < times; ++i) {
counter.storeRelaxed(0); counter.storeRelaxed(0);
CountFor f(0, iterations); // ThreadEngine will delete f when it finishes
f.startBlocking(); auto f = new CountFor(0, iterations);
auto future = f->startAsynchronously();
future.waitForFinished();
QCOMPARE(counter.loadRelaxed(), iterations); QCOMPARE(counter.loadRelaxed(), iterations);
} }
} }
@ -174,8 +177,12 @@ void tst_QtConcurrentIterateKernel::stresstest()
void tst_QtConcurrentIterateKernel::noIterations() void tst_QtConcurrentIterateKernel::noIterations()
{ {
const int times = 20000; const int times = 20000;
for (int i = 0; i < times; ++i) for (int i = 0; i < times; ++i) {
startThreadEngine(new IterateKernel<TestIterator, void>(QThreadPool::globalInstance(), 0, 0)).startBlocking(); auto future = startThreadEngine(new IterateKernel<TestIterator, void>(
QThreadPool::globalInstance(), 0, 0))
.startAsynchronously();
future.waitForFinished();
}
} }
QMutex threadsMutex; QMutex threadsMutex;
@ -230,8 +237,10 @@ void tst_QtConcurrentIterateKernel::throttling()
threads.clear(); threads.clear();
ThrottleFor f(0, totalIterations); // ThreadEngine will delete f when it finishes
f.startBlocking(); auto f = new ThrottleFor(0, totalIterations);
auto future = f->startAsynchronously();
future.waitForFinished();
QCOMPARE(iterations.loadRelaxed(), totalIterations); QCOMPARE(iterations.loadRelaxed(), totalIterations);

View File

@ -66,16 +66,9 @@ public:
void tst_QtConcurrentThreadEngine::runDirectly() void tst_QtConcurrentThreadEngine::runDirectly()
{ {
{ PrintUser *engine = new PrintUser();
PrintUser engine; QFuture<void> f = engine->startAsynchronously();
engine.startSingleThreaded(); f.waitForFinished();
engine.startBlocking();
}
{
PrintUser *engine = new PrintUser();
QFuture<void> f = engine->startAsynchronously();
f.waitForFinished();
}
} }
class StringResultUser : public ThreadEngine<QString> class StringResultUser : public ThreadEngine<QString>
@ -108,8 +101,10 @@ public:
void tst_QtConcurrentThreadEngine::result() void tst_QtConcurrentThreadEngine::result()
{ {
StringResultUser engine; // ThreadEngine will delete 'engine' when it finishes
QCOMPARE(*engine.startBlocking(), QString("Foo")); auto engine = new StringResultUser();
auto future = engine->startAsynchronously();
QCOMPARE(future.result(), QString("Foo"));
} }
class VoidResultUser : public ThreadEngine<void> class VoidResultUser : public ThreadEngine<void>
@ -137,17 +132,9 @@ public:
void tst_QtConcurrentThreadEngine::runThroughStarter() void tst_QtConcurrentThreadEngine::runThroughStarter()
{ {
{ ThreadEngineStarter<QString> starter = startThreadEngine(new StringResultUser());
ThreadEngineStarter<QString> starter = startThreadEngine(new StringResultUser()); QFuture<QString> f = starter.startAsynchronously();
QFuture<QString> f = starter.startAsynchronously(); QCOMPARE(f.result(), QString("Foo"));
QCOMPARE(f.result(), QString("Foo"));
}
{
ThreadEngineStarter<QString> starter = startThreadEngine(new StringResultUser());
QString str = starter.startBlocking();
QCOMPARE(str, QString("Foo"));
}
} }
class CancelUser : public ThreadEngine<void> class CancelUser : public ThreadEngine<void>
@ -233,12 +220,6 @@ void tst_QtConcurrentThreadEngine::throttle()
f.waitForFinished(); f.waitForFinished();
QCOMPARE(count.loadRelaxed(), 0); QCOMPARE(count.loadRelaxed(), 0);
} }
for (int i = 0; i < repeats; ++i) {
ThrottleAlwaysUser t;
t.startBlocking();
QCOMPARE(count.loadRelaxed(), 0);
}
} }
QSet<QThread *> threads; QSet<QThread *> threads;
@ -278,17 +259,9 @@ void tst_QtConcurrentThreadEngine::threadCount()
const int repeats = 10; const int repeats = 10;
for (int i = 0; i < repeats; ++i) { for (int i = 0; i < repeats; ++i) {
ThreadCountUser t;
t.startBlocking();
int count = threads.count();
int count_expected = QThreadPool::globalInstance()->maxThreadCount() + 1; // +1 for the main thread.
if (count != count_expected)
QEXPECT_FAIL("", "QTBUG-23333", Abort);
QCOMPARE(count, count_expected);
(new ThreadCountUser())->startAsynchronously().waitForFinished(); (new ThreadCountUser())->startAsynchronously().waitForFinished();
count = threads.count(); const auto count = threads.count();
count_expected = QThreadPool::globalInstance()->maxThreadCount(); const auto count_expected = QThreadPool::globalInstance()->maxThreadCount();
if (count != count_expected) if (count != count_expected)
QEXPECT_FAIL("", "QTBUG-23333", Abort); QEXPECT_FAIL("", "QTBUG-23333", Abort);
QCOMPARE(count, count_expected); QCOMPARE(count, count_expected);
@ -296,15 +269,8 @@ void tst_QtConcurrentThreadEngine::threadCount()
// Set the finish flag immediately, this should give us one thread only. // Set the finish flag immediately, this should give us one thread only.
for (int i = 0; i < repeats; ++i) { for (int i = 0; i < repeats; ++i) {
ThreadCountUser t(true /*finishImmediately*/);
t.startBlocking();
int count = threads.count();
if (count != 1)
QEXPECT_FAIL("", "QTBUG-23333", Abort);
QCOMPARE(count, 1);
(new ThreadCountUser(true /*finishImmediately*/))->startAsynchronously().waitForFinished(); (new ThreadCountUser(true /*finishImmediately*/))->startAsynchronously().waitForFinished();
count = threads.count(); const auto count = threads.count();
if (count != 1) if (count != 1)
QEXPECT_FAIL("", "QTBUG-23333", Abort); QEXPECT_FAIL("", "QTBUG-23333", Abort);
QCOMPARE(count, 1); QCOMPARE(count, 1);
@ -450,7 +416,6 @@ public:
void tst_QtConcurrentThreadEngine::exceptions() void tst_QtConcurrentThreadEngine::exceptions()
{ {
// Asynchronous mode:
{ {
bool caught = false; bool caught = false;
try { try {
@ -463,32 +428,6 @@ void tst_QtConcurrentThreadEngine::exceptions()
QVERIFY2(caught, "did not get exception"); QVERIFY2(caught, "did not get exception");
} }
// Blocking mode:
// test throwing the exception from a worker thread.
{
bool caught = false;
try {
QtConcurrentExceptionThrower e(QThread::currentThread());
e.startBlocking();
} catch (const QException &) {
caught = true;
}
QVERIFY2(caught, "did not get exception");
}
// test throwing the exception from the main thread (different code path)
{
bool caught = false;
try {
QtConcurrentExceptionThrower e(nullptr);
e.startBlocking();
} catch (const QException &) {
caught = true;
}
QVERIFY2(caught, "did not get exception");
}
// Asynchronous mode:
{ {
bool caught = false; bool caught = false;
try { try {
@ -506,43 +445,6 @@ void tst_QtConcurrentThreadEngine::exceptions()
} }
QVERIFY2(caught, "did not get exception"); QVERIFY2(caught, "did not get exception");
} }
// Blocking mode:
// test throwing the exception from a worker thread.
{
bool caught = false;
try {
IntExceptionThrower e(QThread::currentThread());
e.startBlocking();
} catch (const QUnhandledException &ex) {
// Make sure the exception info is not lost
try {
if (ex.exception())
std::rethrow_exception(ex.exception());
} catch (int) {
caught = true;
}
}
QVERIFY2(caught, "did not get exception");
}
// test throwing the exception from the main thread (different code path)
{
bool caught = false;
try {
IntExceptionThrower e(nullptr);
e.startBlocking();
} catch (const QUnhandledException &ex) {
// Make sure the exception info is not lost
try {
if (ex.exception())
std::rethrow_exception(ex.exception());
} catch (int) {
caught = true;
}
}
QVERIFY2(caught, "did not get exception");
}
} }
#endif #endif