|
|
|
|
|
|
| #ifndef QTCONCURRENT_THREADENGINE_H
|
| #define QTCONCURRENT_THREADENGINE_H
|
|
|
| #include <QtConcurrent/qtconcurrent_global.h>
|
|
|
| #if !defined(QT_NO_CONCURRENT) ||defined(Q_QDOC)
|
|
|
| #include <QtCore/qthreadpool.h>
|
| #include <QtCore/qfuture.h>
|
| #include <QtCore/qdebug.h>
|
| #include <QtCore/qexception.h>
|
| #include <QtCore/qwaitcondition.h>
|
| #include <QtCore/qatomic.h>
|
| #include <QtCore/qsemaphore.h>
|
|
|
| QT_BEGIN_NAMESPACE
|
|
|
|
|
| namespace QtConcurrent {
|
|
|
|
|
|
|
|
|
| class ThreadEngineBarrier
|
| {
|
| private:
|
|
|
|
|
|
|
|
|
| QAtomicInt count;
|
| QSemaphore semaphore;
|
| public:
|
| ThreadEngineBarrier();
|
| void acquire();
|
| int release();
|
| void wait();
|
| int currentCount();
|
| bool releaseUnlessLast();
|
| };
|
|
|
| enum ThreadFunctionResult { ThrottleThread, ThreadFinished };
|
|
|
|
|
|
|
|
|
|
|
| class Q_CONCURRENT_EXPORT ThreadEngineBase: public QRunnable
|
| {
|
| public:
|
|
|
| ThreadEngineBase(QThreadPool *pool);
|
| virtual ~ThreadEngineBase();
|
| void startSingleThreaded();
|
| void startThread();
|
| bool isCanceled();
|
| void waitForResume();
|
| bool isProgressReportingEnabled();
|
| void setProgressValue(int progress);
|
| void setProgressRange(int minimum, int maximum);
|
| void acquireBarrierSemaphore();
|
| void reportIfSuspensionDone() const;
|
|
|
| protected:
|
| virtual void start() {}
|
| virtual void finish() {}
|
| virtual ThreadFunctionResult threadFunction() { return ThreadFinished; }
|
| virtual bool shouldStartThread() { return !shouldThrottleThread(); }
|
| virtual bool shouldThrottleThread()
|
| {
|
| return futureInterface ? (futureInterface->isSuspending() || futureInterface->isSuspended())
|
| : false;
|
| }
|
|
|
| private:
|
| bool startThreadInternal();
|
| void startThreads();
|
| void threadExit();
|
| bool threadThrottleExit();
|
| void run() override;
|
| virtual void asynchronousFinish() = 0;
|
| #ifndef QT_NO_EXCEPTIONS
|
| void handleException(const QException &exception);
|
| #endif
|
| protected:
|
| QFutureInterfaceBase *futureInterface;
|
| QThreadPool *threadPool;
|
| ThreadEngineBarrier barrier;
|
| QtPrivate::ExceptionStore exceptionStore;
|
| QBasicMutex mutex;
|
| };
|
|
|
|
|
| template <typename T>
|
| class ThreadEngine : public ThreadEngineBase
|
| {
|
| public:
|
| typedef T ResultType;
|
|
|
| ThreadEngine(QThreadPool *pool) : ThreadEngineBase(pool) {}
|
|
|
| virtual T *result() { return nullptr; }
|
|
|
| QFutureInterface<T> *futureInterfaceTyped()
|
| {
|
| return static_cast<QFutureInterface<T> *>(futureInterface);
|
| }
|
|
|
|
|
| T *startSingleThreaded()
|
| {
|
| ThreadEngineBase::startSingleThreaded();
|
| return result();
|
| }
|
|
|
|
|
|
|
| QFuture<T> startAsynchronously()
|
| {
|
| futureInterface = new QFutureInterface<T>();
|
|
|
|
|
|
|
|
|
| futureInterface->reportStarted();
|
| QFuture<T> future = QFuture<T>(futureInterfaceTyped());
|
| start();
|
|
|
| acquireBarrierSemaphore();
|
| threadPool->start(this);
|
| return future;
|
| }
|
|
|
| void asynchronousFinish() override
|
| {
|
| finish();
|
| futureInterfaceTyped()->reportFinished(result());
|
| delete futureInterfaceTyped();
|
| delete this;
|
| }
|
|
|
|
|
| void reportResult(const T *_result, int index = -1)
|
| {
|
| if (futureInterface)
|
| futureInterfaceTyped()->reportResult(_result, index);
|
| }
|
|
|
| void reportResults(const QList<T> &_result, int index = -1, int count = -1)
|
| {
|
| if (futureInterface)
|
| futureInterfaceTyped()->reportResults(_result, index, count);
|
| }
|
| };
|
|
|
|
|
|
|
|
|
|
|
| template <typename T>
|
| class ThreadEngineStarterBase
|
| {
|
| public:
|
| ThreadEngineStarterBase(ThreadEngine<T> *_threadEngine)
|
| : threadEngine(_threadEngine) { }
|
|
|
| inline ThreadEngineStarterBase(const ThreadEngineStarterBase &other)
|
| : threadEngine(other.threadEngine) { }
|
|
|
| QFuture<T> startAsynchronously()
|
| {
|
| return threadEngine->startAsynchronously();
|
| }
|
|
|
| operator QFuture<T>()
|
| {
|
| return startAsynchronously();
|
| }
|
|
|
| protected:
|
| ThreadEngine<T> *threadEngine;
|
| };
|
|
|
|
|
|
|
|
|
|
|
| template <typename T>
|
| class ThreadEngineStarter : public ThreadEngineStarterBase<T>
|
| {
|
| typedef ThreadEngineStarterBase<T> Base;
|
| typedef ThreadEngine<T> TypedThreadEngine;
|
| public:
|
| ThreadEngineStarter(TypedThreadEngine *eng)
|
| : Base(eng) { }
|
| };
|
|
|
|
|
| template <>
|
| class ThreadEngineStarter<void> : public ThreadEngineStarterBase<void>
|
| {
|
| public:
|
| ThreadEngineStarter(ThreadEngine<void> *_threadEngine)
|
| : ThreadEngineStarterBase<void>(_threadEngine) {}
|
| };
|
|
|
|
|
| template <typename ThreadEngine>
|
| inline ThreadEngineStarter<typename ThreadEngine::ResultType> startThreadEngine(ThreadEngine *threadEngine)
|
| {
|
| return ThreadEngineStarter<typename ThreadEngine::ResultType>(threadEngine);
|
| }
|
|
|
| }
|
|
|
|
|
| QT_END_NAMESPACE
|
|
|
| #endif
|
|
|
| #endif
|
|
|