Commit ff1f8114 authored by Luca Giambonini's avatar Luca Giambonini

let QThreadPool manage the QRunnable process

a QRunnable is already a thread, we don't need to cast an other thread inside the class.
QThreadPool would be happy to delete the pointer when needed, and automatically.

this commit partially revert c6cc00cc
http://doc.qt.io/qt-4.8/qthreadpool.html#details
parent ca96381a
......@@ -227,7 +227,7 @@ void OperationRunnerPrivate::__k__doValidate()
QObject::connect(valThread.data(), SIGNAL(ready()), q, SLOT(__k__connectToValidationStatus()), Qt::DirectConnection);
valThread.data()->run();
QThreadPool::globalInstance()->start(valThread);
}
void OperationRunnerPrivate::__k__connectToValidationStatus()
......@@ -261,7 +261,7 @@ void OperationRunner::run()
QObject::connect(d->runThread.data(), SIGNAL(ready()), this, SLOT(__k__connectToRunnerStatus()), Qt::DirectConnection);
d->runThread.data()->run();
QThreadPool::globalInstance()->start(d->runThread);
}
void OperationRunnerPrivate::__k__connectToRunnerStatus()
......
......@@ -121,14 +121,6 @@ void RunnerWorker::processNextPhase()
m_currentPhase = Operation::Phase5;
break;
case Operation::Phase5:
//Now we need to move all ops back to the main app thread
for (auto it = m_operations.constBegin(), end = m_operations.constEnd(); it != end; ++it) {
const auto ops = it.value();
for ( auto it2 = ops.constBegin(), end2 = ops.constEnd(); it2 != end2; ++it2 ) {
Operation* op = *it2;
op->moveToThread(QCoreApplication::instance()->thread());
}
}
// If we're here, we're successfully over.
emit runFinished(true);
return;
......@@ -213,9 +205,9 @@ void RunnerWorker::run()
RunnerRunnable::RunnerRunnable(const QHash<Operation::Phase, QList< Operation* > > &ops, ProcessingOptions processingOptions, QObject* parent)
: QObject(parent)
, QRunnable()
, m_operations(ops)
, m_worker(nullptr)
, m_thread(new QThread())
{
s_options = processingOptions;
}
......@@ -224,7 +216,6 @@ RunnerRunnable::~RunnerRunnable()
{
if (m_worker)
m_worker->deleteLater();
delete m_thread;
}
void RunnerRunnable::requestCancel()
......@@ -234,21 +225,10 @@ void RunnerRunnable::requestCancel()
void RunnerRunnable::run()
{
//Don't parent, otherwise we break thread-affinity!!!
// with QRunnable we are running this operations inside a thread
m_worker = new RunnerWorker(m_operations);
m_worker->moveToThread(m_thread);
for (auto it = m_operations.constBegin(), end = m_operations.constEnd(); it != end; ++it) {
const auto ops = it.value();
for ( auto it2 = ops.constBegin(), end2 = ops.constEnd(); it2 != end2; ++it2 ) {
Operation* op = *it2;
op->moveToThread(m_thread);
}
}
emit ready();
QTimer::singleShot(0, m_worker, &RunnerWorker::run);
m_thread->start();
m_worker->run();
}
RunnerWorker * RunnerRunnable::worker()
......
......@@ -14,12 +14,28 @@
#include <QThread>
#include <QHash>
#include <QPointer>
#include <QRunnable>
#include <akabeierror.h>
#include <akabeioperation.h>
#include <akabeioperationrunner_p.h>
namespace Akabei {
/**
* \class RunnerWorker akabeirunnerrunnable_p.h "akabeirunnerrunnable_p.h"
*
* \brief This class run the transactions.
*
* This class run the queued transaction previously verified with the ValidatorRunnable class.
*
* The operations are divided in 5 phases, and they are run one ofter the other.
* If one operation in the phase can not be executed in parallel (concurrent) then
* the transaction will be run sequentially. (parallel is set by default)
*
* This class is not thread-safe.
*/
class RunnerWorker : public QObject
{
Q_OBJECT
......@@ -53,7 +69,31 @@ class RunnerWorker : public QObject
friend class RunnerRunnable;
};
class RunnerRunnable : public QObject
/**
* \class RunnerRunnable akabeirunnerrunnable_p.h "akabeirunnerrunnable_p.h"
*
* \brief This class manage the RunnerWorker process.
*
* This class is based on QRunnable, and manage the RunnerWorker process transactions.
*
* \code
* class RunnerRunnable : public QRunnable
* {
* void run()
* {
* qDebug() << "Hello world from thread" << QThread::currentThread();
* }
* }
*
* RunnerRunnable *validator = new RunnerRunnable();
* // QThreadPool takes ownership and deletes 'validator' automatically
* QThreadPool::globalInstance()->start(validator);
* \endcode
*
* This class is thread-safe.
*/
class RunnerRunnable : public QObject, public QRunnable
{
Q_OBJECT
public:
......@@ -71,7 +111,6 @@ class RunnerRunnable : public QObject
private:
QHash<Operation::Phase, QList< Operation* > > m_operations;
RunnerWorker * m_worker;
QThread * m_thread;
};
}
......
......@@ -141,13 +141,6 @@ void ValidatorWorker::processNextPhase()
m_currentPhase = Operation::Phase5;
break;
case Operation::Phase5:
//Now we need to move all ops back to the main app thread
for (auto it = m_operations.constBegin(), end = m_operations.constEnd(); it != end; ++it ) {
const auto ops = it.value();
foreach (Operation * op, ops)
op->moveToThread(QCoreApplication::instance()->thread());
}
// If we're here, we're successfully over.
emit validationFinished(true, m_operations);
return;
......@@ -442,7 +435,6 @@ ValidatorRunnable::ValidatorRunnable(const QHash<Operation::Phase, QList< Operat
, QRunnable()
, m_operations(ops)
, m_worker(nullptr)
, m_thread(new QThread())
{
s_options = processingOptions;
}
......@@ -451,7 +443,6 @@ ValidatorRunnable::~ValidatorRunnable()
{
if (m_worker)
m_worker->deleteLater();
delete m_thread;
}
ValidatorWorker* ValidatorRunnable::worker()
......@@ -466,18 +457,10 @@ void ValidatorRunnable::requestCancel()
void ValidatorRunnable::run()
{
//Don't use a parent here, otherwise we break thread affinity!!!
// with QRunnable we are running this operations inside a thread
m_worker = new ValidatorWorker(m_operations);
m_worker->moveToThread(m_thread);
for (auto it = m_operations.constBegin(), end = m_operations.constEnd(); it != end; ++it ) {
const auto ops = it.value();
foreach (Operation * op, ops)
op->moveToThread(m_thread);
}
emit ready();
// m_worker->run();
QTimer::singleShot(0, m_worker, &ValidatorWorker::run);
m_thread->start();
m_worker->run();
}
}
......
......@@ -22,6 +22,31 @@
namespace Akabei {
class OperationPrivate;
/**
* \class ValidatorWorker akabeivalidatorrunnable_p.h "akabeivalidatorrunnable_p.h"
*
* \brief This class decides if a transaction is valid or not.
*
* This class decides if a transaction is valid or not.
* It is the most complex part in the whole AkabeiCore, and the most delicate as well, since
* this decides if your system will be fucked up by a wrong transaction or be spared.
* Be careful.
*
* The validation process is divided in 5 phases, each of one contains
* different operations. The ValidatorWorker, by means of processNextPhase(),
* go through all the phases to identify if one of the included operation
* is not valid.
*
* The validation steps can be divided in:
* 1. concurrent validation
* 2. two operations trying to add the same target
* 3. dependency and version check
* 4. filesystem conflicts, and pkg file conflicts
* 5. dependencies break
*
* This class is not thread-safe.
*/
class ValidatorWorker : public QObject
{
Q_OBJECT
......@@ -52,13 +77,31 @@ class ValidatorWorker : public QObject
friend class ValidatorRunnable;
};
/*
Hey, brave man looking at this code, I salute you.
Before starting, keep this in mind: what you see here is what decides if a transaction is valid or not.
It is the most complex part in the whole AkabeiCore, and the most delicate as well, since
this decides if your system will be fucked up by a wrong transaction or be spared.
Be careful.
*/
/**
* \class ValidatorRunnable akabeivalidatorrunnable_p.h "akabeivalidatorrunnable_p.h"
*
* \brief This class manage the ValidatorWorker process.
*
* This class is based on QRunnable, and manage the ValidatorWorker process transactions.
*
* \code
* class ValidatorRunnable : public QRunnable
* {
* void run()
* {
* qDebug() << "Hello world from thread" << QThread::currentThread();
* }
* }
*
* ValidatorRunnable *validator = new ValidatorRunnable();
* // QThreadPool takes ownership and deletes 'validator' automatically
* QThreadPool::globalInstance()->start(validator);
* \endcode
*
* This class is thread-safe.
*/
class ValidatorRunnable : public QObject, public QRunnable
{
Q_OBJECT
......@@ -77,7 +120,6 @@ class ValidatorRunnable : public QObject, public QRunnable
private:
OpsHash m_operations;
ValidatorWorker * m_worker;
QThread * m_thread;
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment