1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #ifndef QTCONCURRENT_THREADENGINE_H
43 #define QTCONCURRENT_THREADENGINE_H
45 #include <QtCore/qglobal.h>
47 #ifndef QT_NO_CONCURRENT
49 #include <QtCore/qthreadpool.h>
50 #include <QtCore/qfuture.h>
51 #include <QtCore/qdebug.h>
52 #include <QtCore/qtconcurrentexception.h>
53 #include <QtCore/qwaitcondition.h>
54 #include <QtCore/qatomic.h>
55 #include <QtCore/qsemaphore.h>
64 namespace QtConcurrent
{
66 // The ThreadEngineBarrier counts worker threads, and allows one
67 // thread to wait for all others to finish. Tested for its use in
68 // QtConcurrent, requires more testing for use as a general class.
69 class ThreadEngineBarrier
72 // The thread count is maintained as an integer in the count atomic
73 // variable. The count can be either positive or negative - a negative
74 // count signals that a thread is waiting on the barrier.
76 // BC note: inlined code from Qt < 4.6 will expect to find the QMutex
77 // and QAtomicInt here. ### Qt 5: remove.
83 ThreadEngineBarrier();
88 bool releaseUnlessLast();
91 enum ThreadFunctionResult
{ ThrottleThread
, ThreadFinished
};
93 // The ThreadEngine controls the threads used in the computation.
94 // Can be run in three modes: single threaded, multi-threaded blocking
95 // and multi-threaded asynchronous.
96 // The code for the single threaded mode is
97 class Q_CORE_EXPORT ThreadEngineBase
: public QRunnable
102 virtual ~ThreadEngineBase();
103 void startSingleThreaded();
104 void startBlocking();
107 void waitForResume();
108 bool isProgressReportingEnabled();
109 void setProgressValue(int progress
);
110 void setProgressRange(int minimum
, int maximum
);
111 void acquireBarrierSemaphore();
113 protected: // The user overrides these:
114 virtual void start() {}
115 virtual void finish() {}
116 virtual ThreadFunctionResult
threadFunction() { return ThreadFinished
; }
117 virtual bool shouldStartThread() { return futureInterface
? !futureInterface
->isPaused() : true; }
118 virtual bool shouldThrottleThread() { return futureInterface
? futureInterface
->isPaused() : false; }
120 bool startThreadInternal();
123 bool threadThrottleExit();
125 virtual void asynchronousFinish() = 0;
126 #ifndef QT_NO_EXCEPTIONS
127 void handleException(const QtConcurrent::Exception
&exception
);
130 QFutureInterfaceBase
*futureInterface
;
131 QThreadPool
*threadPool
;
132 ThreadEngineBarrier barrier
;
133 QtConcurrent::internal::ExceptionStore exceptionStore
;
137 template <typename T
>
138 class ThreadEngine
: public virtual ThreadEngineBase
141 typedef T ResultType
;
143 virtual T
*result() { return 0; }
145 QFutureInterface
<T
> *futureInterfaceTyped()
147 return static_cast<QFutureInterface
<T
> *>(futureInterface
);
150 // Runs the user algorithm using a single thread.
151 T
*startSingleThreaded()
153 ThreadEngineBase::startSingleThreaded();
157 // Runs the user algorithm using multiple threads.
158 // This function blocks until the algorithm is finished,
159 // and then returns the result.
162 ThreadEngineBase::startBlocking();
166 // Runs the user algorithm using multiple threads.
167 // Does not block, returns a future.
168 QFuture
<T
> startAsynchronously()
170 futureInterface
= new QFutureInterface
<T
>();
172 // reportStart() must be called before starting threads, otherwise the
173 // user algorithm might finish while reportStart() is running, which
175 futureInterface
->reportStarted();
176 QFuture
<T
> future
= QFuture
<T
>(futureInterfaceTyped());
179 acquireBarrierSemaphore();
180 threadPool
->start(this);
184 void asynchronousFinish()
187 futureInterfaceTyped()->reportFinished(result());
188 delete futureInterfaceTyped();
193 void reportResult(const T
*_result
, int index
= -1)
196 futureInterfaceTyped()->reportResult(_result
, index
);
199 void reportResults(const QVector
<T
> &_result
, int index
= -1, int count
= -1)
202 futureInterfaceTyped()->reportResults(_result
, index
, count
);
206 // The ThreadEngineStarter class ecapsulates the return type
207 // from the thread engine.
208 // Depending on how the it is used, it will run
209 // the engine in either blocking mode or asynchronous mode.
210 template <typename T
>
211 class ThreadEngineStarterBase
214 ThreadEngineStarterBase(ThreadEngine
<T
> *_threadEngine
)
215 : threadEngine(_threadEngine
) { }
217 inline ThreadEngineStarterBase(const ThreadEngineStarterBase
&other
)
218 : threadEngine(other
.threadEngine
) { }
220 QFuture
<T
> startAsynchronously()
222 return threadEngine
->startAsynchronously();
225 operator QFuture
<T
>()
227 return startAsynchronously();
231 ThreadEngine
<T
> *threadEngine
;
235 // We need to factor out the code that dereferences the T pointer,
236 // with a specialization where T is void. (code that dereferences a void *
238 template <typename T
>
239 class ThreadEngineStarter
: public ThreadEngineStarterBase
<T
>
241 typedef ThreadEngineStarterBase
<T
> Base
;
242 typedef ThreadEngine
<T
> TypedThreadEngine
;
244 ThreadEngineStarter(TypedThreadEngine
*eng
)
249 T t
= *this->threadEngine
->startBlocking();
250 delete this->threadEngine
;
255 // Full template specialization where T is void.
257 class ThreadEngineStarter
<void> : public ThreadEngineStarterBase
<void>
260 ThreadEngineStarter
<void>(ThreadEngine
<void> *_threadEngine
)
261 :ThreadEngineStarterBase
<void>(_threadEngine
) {}
265 this->threadEngine
->startBlocking();
266 delete this->threadEngine
;
270 template <typename ThreadEngine
>
271 inline ThreadEngineStarter
<typename
ThreadEngine::ResultType
> startThreadEngine(ThreadEngine
*threadEngine
)
273 return ThreadEngineStarter
<typename
ThreadEngine::ResultType
>(threadEngine
);
276 } // namespace QtConcurrent
283 #endif // QT_NO_CONCURRENT