[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kdelibs/frameworks] tier1/threadweaver/src/Weaver: Add convenience API for queueing and creating jo
From: Mirko Boehm (Endocode) <mirko () endocode ! com>
Date: 2013-10-31 22:58:28
Message-ID: E1Vc1C8-0002CX-31 () scm ! kde ! org
[Download RAW message or body]
Git commit da4da04f5a2c341cbe246a96b8f21fa178c98152 by Mirko Boehm (Endocode).
Committed on 31/10/2013 at 21:18.
Pushed by mirko into branch 'frameworks'.
Add convenience API for queueing and creating job objects.
Move Weaver class into it īs own file. Make ThreadWeaver.h the catch-all
header to include the most important ThreadWeaver components.
M +4 -2 tier1/threadweaver/src/Weaver/CMakeLists.txt
A +101 -0 tier1/threadweaver/src/Weaver/Queueing.h [License: UNKNOWN] *
M +0 -190 tier1/threadweaver/src/Weaver/ThreadWeaver.cpp
M +7 -86 tier1/threadweaver/src/Weaver/ThreadWeaver.h
C +2 -29 tier1/threadweaver/src/Weaver/Weaver.cpp [from: \
tier1/threadweaver/src/Weaver/ThreadWeaver.cpp - 082% similarity] C +5 -3 \
tier1/threadweaver/src/Weaver/Weaver.h [from: \
tier1/threadweaver/src/Weaver/ThreadWeaver.h - 097% similarity] [License: \
UNKNOWN] *
The files marked with a * at the end have a non valid license. Please read: \
http://techbase.kde.org/Policies/Licensing_Policy and use the headers which are \
listed at that page.
http://commits.kde.org/kdelibs/da4da04f5a2c341cbe246a96b8f21fa178c98152
diff --git a/tier1/threadweaver/src/Weaver/CMakeLists.txt \
b/tier1/threadweaver/src/Weaver/CMakeLists.txt index cb37999..ce20785 100644
--- a/tier1/threadweaver/src/Weaver/CMakeLists.txt
+++ b/tier1/threadweaver/src/Weaver/CMakeLists.txt
@@ -3,7 +3,7 @@
set(ThreadWeaver_LIB_SRCS
Queue.cpp
QueueAPI.cpp
- ThreadWeaver.cpp
+ Weaver.cpp
WeaverImpl.cpp
DebuggingAids.cpp
Thread.cpp
@@ -46,8 +46,10 @@ set_target_properties(ThreadWeaver PROPERTIES
install(TARGETS ThreadWeaver EXPORT ThreadWeaverTargets \
${INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES
- WeaverInterface.h
ThreadWeaver.h
+ Queueing.h
+ WeaverInterface.h
+ Weaver.h
DebuggingAids.h
Thread.h
JobInterface.h
diff --git a/tier1/threadweaver/src/Weaver/Queueing.h \
b/tier1/threadweaver/src/Weaver/Queueing.h new file mode 100644
index 0000000..919d4f6
--- /dev/null
+++ b/tier1/threadweaver/src/Weaver/Queueing.h
@@ -0,0 +1,101 @@
+#ifndef THREADWEAVER_QUEUEING_H
+#define THREADWEAVER_QUEUEING_H
+
+#include "JobPointer.h"
+#include "ManagedJobPointer.h"
+#include "JobInterface.h"
+#include "JobCollection.h"
+#include "Lambda.h"
+#include "QObjectDecorator.h"
+#include "Weaver.h"
+
+namespace ThreadWeaver {
+namespace Queueing {
+
+// make a job that calls a functor, anything that responds to operator[]
+template<typename T>
+JobPointer make_job(T t) {
+ JobPointer ret(new Lambda<T>(t));
+ return ret;
+}
+
+// make a job pointer holding a pointer to a Job(Interface)
+template<>
+inline JobPointer make_job<JobInterface*>(JobInterface* job) {
+ return JobPointer(job);
+}
+
+// enqueue any functor type to the specified queue:
+template<typename T>
+JobPointer enqueue(Weaver* weaver, T t) {
+ JobPointer ret = make_job(t);
+ weaver->enqueue(ret);
+ return ret;
+}
+
+// specialise for QObjectDecorator:
+template<>
+inline JobPointer enqueue<QObjectDecorator*>(Weaver* weaver, QObjectDecorator* q) {
+ JobPointer ret(q);
+ weaver->enqueue(ret);
+ return ret;
+}
+
+// specialise for JobPointer:
+template<>
+inline JobPointer enqueue<JobPointer>(Weaver* weaver, JobPointer job) {
+ weaver->enqueue(job);
+ return job;
+}
+
+//// specialise for JobInterface:
+//template<>
+//JobPointer enqueue<JobInterface*>(Weaver* weaver, JobInterface* job) {
+// return enqueue(weaver, make_job(job));
+//}
+
+//// specialise for Collection:
+//template<>
+//JobPointer enqueue<JobCollection*>(Weaver* weaver, JobCollection* job) {
+// return enqueue(weaver, make_job(job));
+//}
+
+//// specialise for Sequence:
+//template<>
+//JobPointer enqueue<JobSequence*>(Weaver* weaver, JobSequence* job) {
+// return enqueue(weaver, make_job(job));
+//}
+
+// convenience overload: enqueue the functor to the global queue:
+template<typename T>
+JobPointer enqueue(T t) {
+ return enqueue(Weaver::instance(), t);
+}
+
+// enqueue a raw pointer with no memory management
+template<typename T>
+JobPointer enqueue_raw(Weaver* weaver, T* t) {
+ ManagedJobPointer<T> ret(t);
+ weaver->enqueue(ret);
+ return ret;
+}
+
+// create a QObjectDecorator decorating the job
+inline QObjectDecorator* decorate_q(JobInterface* job) {
+ return new QObjectDecorator(job);
+}
+
+}
+}
+
+inline ThreadWeaver::JobCollection& operator<<(ThreadWeaver::JobCollection& \
collection, ThreadWeaver::JobInterface* job) { + \
collection.addJob(ThreadWeaver::Queueing::make_job(job)); + return collection;
+}
+
+inline ThreadWeaver::JobCollection& operator<<(ThreadWeaver::JobCollection& \
collection, const ThreadWeaver::JobPointer& job) { + collection.addJob(job);
+ return collection;
+}
+
+#endif // THREADWEAVER_QUEUEING_H
diff --git a/tier1/threadweaver/src/Weaver/ThreadWeaver.cpp \
b/tier1/threadweaver/src/Weaver/ThreadWeaver.cpp index 6704a59..65442d0 100644
--- a/tier1/threadweaver/src/Weaver/ThreadWeaver.cpp
+++ b/tier1/threadweaver/src/Weaver/ThreadWeaver.cpp
@@ -27,193 +27,3 @@ http://creative-destruction.me $
#include "ThreadWeaver.h"
-#include <QtCore/QCoreApplication>
-#include <QtCore/QMutex>
-
-#include "WeaverImpl.h"
-#include "WeaverObserver.h"
-
-using namespace ThreadWeaver;
-
-class Weaver::Private
-{
-public:
- Private ()
- : implementation(0)
- {}
-
- Queue* implementation;
-};
-
-Weaver::Weaver(QObject* parent)
- : Queue(parent)
- , d(new Private)
-{
- d->implementation = makeWeaverImpl();
- //FIXME move to makeWeaverImpl(), so that implementations can be replaced
- connect(d->implementation, SIGNAL (finished()), SIGNAL (finished()));
- connect(d->implementation, SIGNAL (suspended()), SIGNAL (suspended()));
- connect(d->implementation, SIGNAL (jobDone(ThreadWeaver::JobPointer)),
- SIGNAL(jobDone(ThreadWeaver::JobPointer)));
-}
-
-Weaver::~Weaver()
-{
- if (d->implementation->state()->stateId()!=Destructed) {
- d->implementation->shutDown();
- }
- delete d->implementation;
- delete d;
-}
-
-Queue *Weaver::makeWeaverImpl()
-{
- Q_ASSERT_X(qApp!=0, Q_FUNC_INFO, "Cannot create global ThreadWeaver instance \
before QApplication!");
- Queue *queue = new WeaverImpl(this);
- return queue;
-}
-
-void Weaver::shutDown()
-{
- d->implementation->shutDown();
-}
-
-const State* Weaver::state() const
-{
- return d->implementation->state();
-}
-
-void Weaver::registerObserver ( WeaverObserver *ext )
-{
- d->implementation->registerObserver ( ext );
-}
-
-namespace {
-
-class StaticThreadWeaverInstanceGuard : public QObject {
- Q_OBJECT
-public:
- explicit StaticThreadWeaverInstanceGuard(QAtomicPointer<Weaver>& instance, \
QCoreApplication* app)
- : QObject(app)
- , instance_(instance)
- {
- Q_ASSERT_X(app!=0, Q_FUNC_INFO, "Calling ThreadWeaver::Weaver::instance() \
requires a QCoreApplication!");
- QObject* impl = instance.load()->findChild<Queue*>();
- Q_ASSERT(impl);
- impl->setObjectName(tr("GlobalQueue"));
- qAddPostRoutine(shutDownGlobalQueue);
- }
-
- ~StaticThreadWeaverInstanceGuard() {
- instance_.fetchAndStoreOrdered(0);
- }
-private:
- static void shutDownGlobalQueue() {
- Weaver::instance()->shutDown();
- Q_ASSERT(Weaver::instance()->state()->stateId() == Destructed);
- }
-
- QAtomicPointer<Weaver>& instance_;
-};
-
-}
-
-/** @brief The application-global Weaver instance.
- * This instance will only be created if this method is actually called in the \
lifetime of the application.
- * The method will create the Weaver instance on first call. The Q(Core)Application \
object must exist at that time.
- * The instance will be deleted when Q(Core)Application is destructed. After that, \
the instance() method returns zero. */
-Weaver* Weaver::instance()
-{
- static QAtomicPointer<Weaver> s_instance(new Weaver(qApp));
- //Order is of importance here:
- //When s_instanceGuard is destructed (first, before s_instance), it sets the \
value of s_instance to zero. Next, qApp will delete
- //the object s_instance pointed to.
- static StaticThreadWeaverInstanceGuard* s_instanceGuard = new \
StaticThreadWeaverInstanceGuard(s_instance, qApp);
- Q_UNUSED(s_instanceGuard);
- Q_ASSERT_X(s_instance.load() == 0 || s_instance.load()->thread() == \
QCoreApplication::instance()->thread(),
- Q_FUNC_INFO,
- "The global ThreadWeaver queue needs to be instantiated (accessed \
first) from the main thread!");
- return s_instance.loadAcquire();
-}
-
-void Weaver::enqueue(const JobPointer &job)
-{
- d->implementation->enqueue(job);
-}
-
-void Weaver::enqueueRaw(JobInterface *job)
-{
- d->implementation->enqueueRaw(job);
-}
-
-bool Weaver::dequeue(const JobPointer& job)
-{
- return d->implementation->dequeue(job);
-}
-
-bool Weaver::dequeueRaw(JobInterface* job)
-{
- return d->implementation->dequeueRaw(job);
-}
-
-void Weaver::dequeue ()
-{
- return d->implementation->dequeue();
-}
-
-void Weaver::finish ()
-{
- return d->implementation->finish ();
-}
-
-void Weaver::suspend ()
-{
- return d->implementation->suspend();
-}
-
-void Weaver::resume ()
-{
- return d->implementation->resume();
-}
-
-bool Weaver::isEmpty() const
-{
- return d->implementation->isEmpty();
-}
-
-bool Weaver::isIdle() const
-{
- return d->implementation->isIdle();
-}
-
-int Weaver::queueLength() const
-{
- return d->implementation->queueLength();
-}
-
-void Weaver::setMaximumNumberOfThreads( int cap )
-{
- d->implementation->setMaximumNumberOfThreads( cap );
-}
-
-int Weaver::currentNumberOfThreads() const
-{
- return d->implementation->currentNumberOfThreads();
-}
-
-int Weaver::maximumNumberOfThreads() const
-{
- return d->implementation->maximumNumberOfThreads();
-}
-
-void Weaver::requestAbort()
-{
- d->implementation->requestAbort();
-}
-
-void Weaver::reschedule()
-{
- d->implementation->reschedule();
-}
-
-#include "ThreadWeaver.moc"
diff --git a/tier1/threadweaver/src/Weaver/ThreadWeaver.h \
b/tier1/threadweaver/src/Weaver/ThreadWeaver.h index c6d5d7f..72c965f 100644
--- a/tier1/threadweaver/src/Weaver/ThreadWeaver.h
+++ b/tier1/threadweaver/src/Weaver/ThreadWeaver.h
@@ -29,91 +29,12 @@
#ifndef THREADWEAVER_H
#define THREADWEAVER_H
-#include <QtCore/QObject>
-
-#include "Queue.h"
-
-namespace ThreadWeaver {
-
-class Job;
-class State;
-class WeaverObserver;
-
-/** The Weaver class provides the public implementation of the WeaverInterface.
-
- Weaver provides a static instance that can be used to perform jobs in
- threads without managing a weaver object. The static instance will
- only be created when it is first accessed. Also, Weaver objects will
- create the threads only when the first jobs are queued. Therefore, the
- creation of a Weaver object is a rather cheap operation.
-
- The WeaverImpl class provides two parts of API - one for the threads
- that are handled by it, and one for the ThreadWeaver users
- (application developers). To separate those two different API parts,
- Weaver only provides the interface supposed to be used by developers
- of multithreaded applications.
-
- Weaver creates and destroys WeaverImpl objects. It hides the
- implementation details of the WeaverImpl class. It is strongly
- discouraged to use the WeaverImpl class in programs, as its API will
- be changed without notice.
- Also, Weaver provides a factory method for this purpose that can be \
overloaded to create
- derived WeaverImpl objects.
-
- */
-// Note: All member documentation is in the WeaverInterface class.
-class THREADWEAVER_EXPORT Weaver : public Queue
-{
- Q_OBJECT
-public:
- /** Construct a Weaver object. */
- explicit Weaver ( QObject* parent=0 );
-
- /** Destruct a Weaver object. */
- virtual ~Weaver ();
-
- const State* state() const;
-
- void setMaximumNumberOfThreads(int cap) Q_DECL_OVERRIDE;
- int maximumNumberOfThreads() const Q_DECL_OVERRIDE;
- int currentNumberOfThreads() const Q_DECL_OVERRIDE;
-
-
- void registerObserver ( WeaverObserver* );
-
- /** Return the global Weaver instance.
- In some cases, a global Weaver object per application is
- sufficient for the applications purpose. If this is the case,
- query instance() to get a pointer to a global instance.
- If instance is never called, a global Weaver object will not be
- created.
- */
- static ThreadWeaver::Weaver* instance();
- void enqueue(const JobPointer&) Q_DECL_OVERRIDE;
- void enqueueRaw(JobInterface* job) Q_DECL_OVERRIDE;
- bool dequeue(const JobPointer&) Q_DECL_OVERRIDE;
- bool dequeueRaw(JobInterface* job) Q_DECL_OVERRIDE;
- void dequeue() Q_DECL_OVERRIDE;
- void finish() Q_DECL_OVERRIDE;
- void suspend() Q_DECL_OVERRIDE;
- void resume() Q_DECL_OVERRIDE;
- bool isEmpty() const Q_DECL_OVERRIDE;
- bool isIdle() const Q_DECL_OVERRIDE;
- int queueLength () const Q_DECL_OVERRIDE;
- void requestAbort() Q_DECL_OVERRIDE;
- void reschedule() Q_DECL_OVERRIDE;
- void shutDown() Q_DECL_OVERRIDE;
-
-protected:
- /** The factory method to create the actual Weaver implementation.
- Overload this method to use a different or adapted implementation.
- */
- virtual Queue* makeWeaverImpl ();
-
-private:
- class Private;
- Private* const d;
-};
-}
+#include "Weaver.h"
+#include "Queueing.h"
+#include "JobInterface.h"
+#include "JobPointer.h"
+#include "Job.h"
+#include "JobCollection.h"
+#include "JobSequence.h"
#endif // THREADWEAVER_H
diff --git a/tier1/threadweaver/src/Weaver/ThreadWeaver.cpp \
b/tier1/threadweaver/src/Weaver/Weaver.cpp similarity index 82%
copy from tier1/threadweaver/src/Weaver/ThreadWeaver.cpp
copy to tier1/threadweaver/src/Weaver/Weaver.cpp
index 6704a59..8b0db15 100644
--- a/tier1/threadweaver/src/Weaver/ThreadWeaver.cpp
+++ b/tier1/threadweaver/src/Weaver/Weaver.cpp
@@ -1,31 +1,4 @@
-/* -*- C++ -*-
-
-This file implements the Weaver class.
-
-$ Author: Mirko Boehm $
-$ Copyright: (C) 2005-2013 Mirko Boehm $
-$ Contact: mirko@kde.org
-http://www.kde.org
-http://creative-destruction.me $
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public
- License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Library General Public License for more details.
-
- You should have received a copy of the GNU Library General Public License
- along with this library; see the file COPYING.LIB. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
-
-*/
-
-#include "ThreadWeaver.h"
+#include "Weaver.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QMutex>
@@ -216,4 +189,4 @@ void Weaver::reschedule()
d->implementation->reschedule();
}
-#include "ThreadWeaver.moc"
+#include "Weaver.moc"
diff --git a/tier1/threadweaver/src/Weaver/ThreadWeaver.h \
b/tier1/threadweaver/src/Weaver/Weaver.h similarity index 97%
copy from tier1/threadweaver/src/Weaver/ThreadWeaver.h
copy to tier1/threadweaver/src/Weaver/Weaver.h
index c6d5d7f..96c93c3 100644
--- a/tier1/threadweaver/src/Weaver/ThreadWeaver.h
+++ b/tier1/threadweaver/src/Weaver/Weaver.h
@@ -26,8 +26,9 @@
Boston, MA 02110-1301, USA.
*/
-#ifndef THREADWEAVER_H
-#define THREADWEAVER_H
+
+#ifndef THREADWEAVER_WEAVER_H
+#define THREADWEAVER_WEAVER_H
#include <QtCore/QObject>
@@ -114,6 +115,7 @@ private:
class Private;
Private* const d;
};
+
}
-#endif // THREADWEAVER_H
+#endif // THREADWEAVER_WEAVER_H
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic