summaryrefslogtreecommitdiffstats
path: root/src/core/jobs
diff options
context:
space:
mode:
authorPaul Lemire <paul.lemire@kdab.com>2014-07-15 08:16:08 +0200
committerSean Harmer <sean.harmer@kdab.com>2014-07-17 14:13:11 +0200
commit19d1829684894f6497ae576b30305e63376ba9b7 (patch)
treefe226279f2a15d91da208594113f268997bcfb67 /src/core/jobs
parent1fbd080bcbebaa28325490ada028111903e42ec7 (diff)
QJob manager slightly refactored and d-pointered
Tried to remove the #include <job.h> from public headers as it seems to be causing compile errors with the latest Qt from dev Change-Id: I77d149772dbb135b573208c2bef5d3405f6cc40f Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/core/jobs')
-rw-r--r--src/core/jobs/jobs.pri3
-rw-r--r--src/core/jobs/qjobmanager.cpp105
-rw-r--r--src/core/jobs/qjobmanager.h33
-rw-r--r--src/core/jobs/qjobmanager_p.h71
4 files changed, 154 insertions, 58 deletions
diff --git a/src/core/jobs/jobs.pri b/src/core/jobs/jobs.pri
index a644f5e80..ade2880f2 100644
--- a/src/core/jobs/jobs.pri
+++ b/src/core/jobs/jobs.pri
@@ -9,6 +9,7 @@ HEADERS += \
$$PWD/qjobmanager.h \
$$PWD/qjobmanagerinterface.h \
$$PWD/qjobproviderinterface.h \
- $$PWD/weaverjob_p.h
+ $$PWD/weaverjob_p.h \
+ $$PWD/qjobmanager_p.h
INCLUDEPATH += $$PWD
diff --git a/src/core/jobs/qjobmanager.cpp b/src/core/jobs/qjobmanager.cpp
index f6f024f3b..01ebd0e0e 100644
--- a/src/core/jobs/qjobmanager.cpp
+++ b/src/core/jobs/qjobmanager.cpp
@@ -39,27 +39,88 @@
**
****************************************************************************/
-#include "qjobmanager.h"
-
-#include "weaverjob_p.h"
-
#include <threadweaver.h>
#include <dependencypolicy.h>
#include <thread.h>
+#include "qjobmanager.h"
+#include "job.h"
+#include "weaverjob_p.h"
+#include "qjobmanager_p.h"
+
#include <QAtomicInt>
#include <QDebug>
#include <QThread>
+
QT_BEGIN_NAMESPACE
namespace Qt3D {
+namespace {
+
+class SynchronizedJob : public ThreadWeaver::Job
+{
+public:
+ SynchronizedJob(QJobManagerInterface::JobFunction func, void *arg, QAtomicInt *atomicCount)
+ : m_func(func)
+ , m_arg(arg)
+ , m_atomicCount(atomicCount)
+ {}
+
+protected:
+ void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) Q_DECL_OVERRIDE;
+
+private:
+ QJobManagerInterface::JobFunction m_func;
+ void *m_arg;
+ QAtomicInt *m_atomicCount;
+};
+
+typedef QSharedPointer<SynchronizedJob> SynchronizedJobPtr;
+
+void SynchronizedJob::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
+{
+ Q_UNUSED(self);
+ Q_CHECK_PTR(m_func);
+ Q_CHECK_PTR(thread);
+
+ // Call the function
+ m_func(m_arg);
+
+ // Decrement the atomic counter to let others know we've done our bit
+ m_atomicCount->deref();
+
+ // Wait for the other worker threads to be done
+ while (m_atomicCount->load() > 0)
+ thread->yieldCurrentThread();
+}
+
+} // anonymous
+
+QJobManagerPrivate::QJobManagerPrivate(QJobManager *qq)
+ : QObjectPrivate()
+ , q_ptr(qq)
+ , m_weaver(Q_NULLPTR)
+{
+}
+
QJobManager::QJobManager(QObject *parent)
: QJobManagerInterface(parent)
- , m_weaver(new ThreadWeaver::Queue(this))
+ , d_ptr(new QJobManagerPrivate(this))
{
- m_weaver->setMaximumNumberOfThreads(QThread::idealThreadCount());
+ Q_D(QJobManager);
+ d->m_weaver = new ThreadWeaver::Queue(this);
+ d->m_weaver->setMaximumNumberOfThreads(QThread::idealThreadCount());
+}
+
+QJobManager::QJobManager(QJobManagerPrivate &dd, QObject *parent)
+ : QJobManagerInterface(parent)
+ , d_ptr(&dd)
+{
+ Q_D(QJobManager);
+ d->m_weaver = new ThreadWeaver::Queue(this);
+ d->m_weaver->setMaximumNumberOfThreads(QThread::idealThreadCount());
}
void QJobManager::initialize()
@@ -68,6 +129,7 @@ void QJobManager::initialize()
void QJobManager::enqueueJobs(const QVector<QJobPtr> &jobQueue)
{
+ Q_D(QJobManager);
// Convert QJobs to ThreadWeaver::Jobs
QHash<QJob *, QSharedPointer<WeaverJob> > jobsMap;
Q_FOREACH (const QJobPtr &job, jobQueue) {
@@ -93,43 +155,28 @@ void QJobManager::enqueueJobs(const QVector<QJobPtr> &jobQueue)
Q_FOREACH (const QJobPtr &job, jobQueue) {
QSharedPointer<WeaverJob> weaverJob = jobsMap.value(job.data());
- m_weaver->enqueue(weaverJob);
+ d->m_weaver->enqueue(weaverJob);
}
}
void QJobManager::waitForAllJobs()
{
- m_weaver->finish();
-}
-
-void QJobManager::SynchronizedJob::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
-{
- Q_UNUSED(self);
- Q_CHECK_PTR(m_func);
- Q_CHECK_PTR(thread);
-
- // Call the function
- m_func(m_arg);
-
- // Decrement the atomic counter to let others know we've done our bit
- m_atomicCount->deref();
-
- // Wait for the other worker threads to be done
- while (m_atomicCount->load() > 0)
- thread->yieldCurrentThread();
+ Q_D(QJobManager);
+ d->m_weaver->finish();
}
void QJobManager::waitForPerThreadFunction(JobFunction func, void *arg)
{
- const int threadCount = m_weaver->maximumNumberOfThreads();
+ Q_D(QJobManager);
+ const int threadCount = d->m_weaver->maximumNumberOfThreads();
QAtomicInt atomicCount(threadCount);
for (int i = 0; i < threadCount; ++i) {
- QJobManager::SynchronizedJobPtr syncJob(new QJobManager::SynchronizedJob(func, arg, &atomicCount));
- m_weaver->enqueue(syncJob);
+ SynchronizedJobPtr syncJob(new SynchronizedJob(func, arg, &atomicCount));
+ d->m_weaver->enqueue(syncJob);
}
- m_weaver->finish();
+ d->m_weaver->finish();
}
} // namespace Qt3D
diff --git a/src/core/jobs/qjobmanager.h b/src/core/jobs/qjobmanager.h
index a05f8b944..55878e7b5 100644
--- a/src/core/jobs/qjobmanager.h
+++ b/src/core/jobs/qjobmanager.h
@@ -47,18 +47,14 @@
#include <Qt3DCore/qjob.h>
-#include <job.h>
-
#include <QVector>
-namespace ThreadWeaver {
-class Queue;
-}
-
QT_BEGIN_NAMESPACE
namespace Qt3D {
+class QJobManagerPrivate;
+
class QJobManager : public QJobManagerInterface
{
Q_OBJECT
@@ -74,28 +70,9 @@ public:
void waitForPerThreadFunction(JobFunction func, void *arg) Q_DECL_OVERRIDE;
protected:
- class SynchronizedJob : public ThreadWeaver::Job
- {
- public:
- SynchronizedJob(JobFunction func, void *arg, QAtomicInt *atomicCount)
- : m_func(func)
- , m_arg(arg)
- , m_atomicCount(atomicCount)
- {}
-
- protected:
- void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) Q_DECL_OVERRIDE;
-
- private:
- JobFunction m_func;
- void *m_arg;
- QAtomicInt *m_atomicCount;
- };
-
- typedef QSharedPointer<SynchronizedJob> SynchronizedJobPtr;
-
-private:
- ThreadWeaver::Queue *m_weaver;
+ QJobManager(QJobManagerPrivate &dd, QObject *parent);
+ Q_DECLARE_PRIVATE(QJobManager)
+ QJobManagerPrivate *d_ptr;
};
} // namespace Qt3D
diff --git a/src/core/jobs/qjobmanager_p.h b/src/core/jobs/qjobmanager_p.h
new file mode 100644
index 000000000..588c33547
--- /dev/null
+++ b/src/core/jobs/qjobmanager_p.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QT3D_QJOBMANAGER_P_H
+#define QT3D_QJOBMANAGER_P_H
+
+#include <private/qobject_p.h>
+
+namespace ThreadWeaver {
+class Queue;
+}
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3D {
+
+class QJobManager;
+
+class QJobManagerPrivate : public QObjectPrivate
+{
+public:
+ QJobManagerPrivate(QJobManager *qq);
+
+ Q_DECLARE_PUBLIC(QJobManager)
+ QJobManager *q_ptr;
+ ThreadWeaver::Queue *m_weaver;
+};
+
+} // Qt3D
+
+QT_END_NAMESPACE
+
+#endif // QT3D_QJOBMANAGER_P_H