Commit 4d028021 authored by Harald Hvaal's avatar Harald Hvaal
Browse files

Added moveToThread to threading support to avoid random crashes. Added...

Added moveToThread to threading support to avoid random crashes. Added preprocessor flags for disabling/enabling threading of jobs.

svn path=/trunk/KDE/kdeutils/ark/; revision=939256
parent 4ca542b4
......@@ -30,13 +30,15 @@
#include <KLocale>
#include <QDir>
#include <QTimer>
#include <QApplication>
#define KERFUFFLE_NOJOBTHREADING
namespace Kerfuffle
{
Job::Job(ReadOnlyArchiveInterface *interface, QObject *parent)
: KJob(parent),
: KJob(NULL),
m_interface(interface)
{
static bool onlyOnce = false;
......@@ -44,12 +46,23 @@ namespace Kerfuffle
qRegisterMetaType<QPair<QString, QString> >("QPair<QString,QString>");
onlyOnce = true;
}
setCapabilities(KJob::Killable | KJob::Suspendable);
}
void Job::start()
{
#ifdef KERFUFFLE_NOJOBTHREADING
QTimer::singleShot(0, this, SLOT(doWork()));
#else
ThreadExecution *thread = new ThreadExecution(this);
//we want the event handling to happen in the work thread to avoid
//unsafe data access due to events while work is being done
moveToThread(thread);
thread->start();
#endif
}
void Job::onError( const QString & message, const QString & details )
......@@ -94,10 +107,14 @@ namespace Kerfuffle
m_interface->registerObserver( this );
bool result = m_interface->list();
m_interface->removeObserver( this );
setError(!result);
emitResult();
kDebug( 1601 ) << "Finished";
#ifndef KERFUFFLE_NOJOBTHREADING
moveToThread(QApplication::instance()->thread());
#endif
}
void ListJob::onNewEntry(const ArchiveEntry& entry)
......@@ -158,6 +175,9 @@ namespace Kerfuffle
m_interface->removeObserver( this );
emitResult();
#ifndef KERFUFFLE_NOJOBTHREADING
moveToThread(QApplication::instance()->thread());
#endif
}
void ExtractJob::fillInDefaultValues(ExtractionOptions& options)
......@@ -186,6 +206,9 @@ namespace Kerfuffle
m_writeInterface->removeObserver( this );
emitResult();
#ifndef KERFUFFLE_NOJOBTHREADING
moveToThread(QApplication::instance()->thread());
#endif
}
......@@ -209,6 +232,9 @@ namespace Kerfuffle
m_writeInterface->removeObserver( this );
emitResult();
#ifndef KERFUFFLE_NOJOBTHREADING
moveToThread(QApplication::instance()->thread());
#endif
}
} // namespace Kerfuffle
......@@ -24,6 +24,9 @@
*/
#include "threading.h"
#include <QApplication>
#include <QTimer>
#include <kdebug.h>
namespace Kerfuffle
{
......@@ -35,8 +38,26 @@ namespace Kerfuffle
void ThreadExecution::run()
{
//perform the work in this thread!
m_job->doWork();
kDebug(1601) << "Run";
//schedule to perform the work in this thread
QTimer::singleShot(0, m_job, SLOT(doWork()));
//and when finished, quit the event loop
connect(m_job, SIGNAL(finished(KJob*)),
this, SLOT(cleanUp()));
//start the event loop
exec();
kDebug(1601) << "Finished exec";
}
void ThreadExecution::cleanUp()
{
kDebug(1601);
//exit the event loop
quit();
}
}
......@@ -26,8 +26,6 @@
#ifndef _THREADING_H_
#define _THREADING_H_
#include <ThreadWeaver/Job>
#include <ThreadWeaver/Weaver>
#include "jobs.h"
#include <QThread>
......@@ -36,6 +34,11 @@ namespace Kerfuffle
class ThreadExecution : public QThread
{
Q_OBJECT
private slots:
void cleanUp();
public:
ThreadExecution(Kerfuffle::Job *job);
......
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