2012-04-25 00:21:01 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-04-25 01:22:30 +02:00
|
|
|
#ifndef CONCURRENTRUN_H
|
|
|
|
#define CONCURRENTRUN_H
|
2012-04-25 00:21:01 +02:00
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
#include <functional>
|
2012-07-16 00:06:55 +02:00
|
|
|
|
2012-04-25 00:21:01 +02:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QRunnable>
|
|
|
|
#include <QThreadPool>
|
|
|
|
|
|
|
|
/*
|
|
|
|
The aim of ThreadFunctor classes and ConcurrentRun::Run() functions is to
|
2012-04-25 00:29:19 +02:00
|
|
|
complete QtConcurrentRun, which lack support for using a particular
|
2012-04-25 00:21:01 +02:00
|
|
|
QThreadPool, as it always uses QThreadPool::globalInstance().
|
|
|
|
|
|
|
|
This is problematic when we do not want to share the same thread pool over
|
|
|
|
all the application, but want to keep the convenient QtConcurrent::run()
|
2012-04-25 00:29:19 +02:00
|
|
|
functor syntax.
|
2012-04-25 01:22:30 +02:00
|
|
|
With ConcurrentRun::Run(), time critical changes can be performed in their
|
|
|
|
own pool, which is not empty by other actions (as it happens when using
|
|
|
|
QtConcurrentRun::run()).
|
2012-04-25 00:21:01 +02:00
|
|
|
|
|
|
|
ThreadFunctor classes are used to store a functor and its arguments, and
|
|
|
|
Run() functions are used for convenience: to directly create a new
|
|
|
|
ThreadFunctor object and start it.
|
|
|
|
*/
|
|
|
|
|
2012-07-16 00:06:55 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Base abstract classes ThreadFunctorBase and ThreadFunctor (for void and
|
|
|
|
non-void result):
|
|
|
|
*/
|
2012-04-25 00:21:01 +02:00
|
|
|
template<typename ReturnType>
|
|
|
|
class ThreadFunctorBase : public QFutureInterface<ReturnType>, public QRunnable {
|
|
|
|
public:
|
|
|
|
ThreadFunctorBase() {}
|
|
|
|
|
|
|
|
QFuture<ReturnType> Start(QThreadPool* thread_pool) {
|
|
|
|
this->setRunnable(this);
|
|
|
|
this->reportStarted();
|
|
|
|
Q_ASSERT(thread_pool);
|
|
|
|
QFuture<ReturnType> future = this->future();
|
|
|
|
thread_pool->start(this, 0 /* priority: currently we do not support
|
|
|
|
changing the priority. Might be added later
|
|
|
|
if needed */);
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run() = 0;
|
2012-07-16 00:06:55 +02:00
|
|
|
};
|
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
template <typename ReturnType, typename... Args>
|
2012-07-16 00:06:55 +02:00
|
|
|
class ThreadFunctor : public ThreadFunctorBase<ReturnType> {
|
|
|
|
public:
|
2014-02-06 14:48:00 +01:00
|
|
|
ThreadFunctor(std::function<ReturnType (Args...)> function,
|
2012-11-21 17:17:37 +01:00
|
|
|
Args... args)
|
2014-02-06 14:48:00 +01:00
|
|
|
: function_(std::bind(function, args...)) {
|
2012-11-21 16:03:50 +01:00
|
|
|
}
|
2012-04-25 00:21:01 +02:00
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
virtual void run() {
|
|
|
|
this->reportResult(function_());
|
2012-04-25 00:21:01 +02:00
|
|
|
this->reportFinished();
|
|
|
|
}
|
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
private:
|
2014-02-06 14:48:00 +01:00
|
|
|
std::function<ReturnType()> function_;
|
2012-04-25 00:21:01 +02:00
|
|
|
};
|
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
// Partial specialisation for void return type.
|
|
|
|
template <typename... Args>
|
|
|
|
class ThreadFunctor <void, Args...> : public ThreadFunctorBase<void> {
|
2012-06-29 16:56:17 +02:00
|
|
|
public:
|
2014-02-06 14:48:00 +01:00
|
|
|
ThreadFunctor(std::function<void (Args...)> function,
|
2012-11-21 17:17:37 +01:00
|
|
|
Args... args)
|
2014-02-06 14:48:00 +01:00
|
|
|
: function_(std::bind(function, args...)) {
|
2012-07-16 00:06:55 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
virtual void run() {
|
2012-06-29 16:56:17 +02:00
|
|
|
function_();
|
2012-11-21 16:03:50 +01:00
|
|
|
this->reportFinished();
|
2012-07-16 00:06:55 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
private:
|
2014-02-06 14:48:00 +01:00
|
|
|
std::function<void()> function_;
|
2012-07-16 00:06:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Run functions
|
|
|
|
*/
|
2012-04-25 00:21:01 +02:00
|
|
|
namespace ConcurrentRun {
|
2012-07-16 00:06:55 +02:00
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
// Empty argument form.
|
|
|
|
template <typename ReturnType>
|
2012-07-16 00:06:55 +02:00
|
|
|
QFuture<ReturnType> Run(
|
2012-06-29 16:56:17 +02:00
|
|
|
QThreadPool* threadpool,
|
2014-02-06 14:48:00 +01:00
|
|
|
std::function<ReturnType ()> function) {
|
2012-11-21 16:03:50 +01:00
|
|
|
return (new ThreadFunctor<ReturnType>(function))->Start(threadpool);
|
2012-07-16 00:06:55 +02:00
|
|
|
}
|
2012-04-25 00:21:01 +02:00
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
// Function object with arguments form.
|
|
|
|
template <typename ReturnType, typename... Args>
|
2012-04-25 00:21:01 +02:00
|
|
|
QFuture<ReturnType> Run(
|
|
|
|
QThreadPool* threadpool,
|
2014-02-06 14:48:00 +01:00
|
|
|
std::function<ReturnType (Args...)> function,
|
2012-11-21 16:03:50 +01:00
|
|
|
const Args&... args) {
|
|
|
|
return (new ThreadFunctor<ReturnType, Args...>(
|
|
|
|
function, args...))->Start(threadpool);
|
2012-04-25 00:21:01 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 16:03:50 +01:00
|
|
|
// Support passing C function pointers instead of function objects.
|
|
|
|
template <typename ReturnType, typename... Args>
|
2012-04-25 00:21:01 +02:00
|
|
|
QFuture<ReturnType> Run(
|
|
|
|
QThreadPool* threadpool,
|
2012-11-21 16:03:50 +01:00
|
|
|
ReturnType (*function) (Args...),
|
|
|
|
const Args&... args) {
|
|
|
|
return Run(
|
2014-02-06 14:48:00 +01:00
|
|
|
threadpool, std::function<ReturnType (Args...)>(function), args...);
|
2012-07-16 00:06:55 +02:00
|
|
|
}
|
2012-04-25 00:21:01 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 01:22:30 +02:00
|
|
|
#endif // CONCURRENTRUN_H
|