1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QTCONCURRENT_THREADENGINE_H
|
---|
43 | #define QTCONCURRENT_THREADENGINE_H
|
---|
44 |
|
---|
45 | #include <QtCore/qglobal.h>
|
---|
46 |
|
---|
47 | #ifndef QT_NO_CONCURRENT
|
---|
48 |
|
---|
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 |
|
---|
55 | QT_BEGIN_HEADER
|
---|
56 | QT_BEGIN_NAMESPACE
|
---|
57 |
|
---|
58 | QT_MODULE(Core)
|
---|
59 |
|
---|
60 | #ifndef qdoc
|
---|
61 |
|
---|
62 | namespace QtConcurrent {
|
---|
63 |
|
---|
64 | // A Semaphore that can wait until all resources are returned.
|
---|
65 | class ThreadEngineSemaphore
|
---|
66 | {
|
---|
67 | public:
|
---|
68 | ThreadEngineSemaphore()
|
---|
69 | :count(0) { }
|
---|
70 |
|
---|
71 | void acquire()
|
---|
72 | {
|
---|
73 | QMutexLocker lock(&mutex);
|
---|
74 | ++count;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int release()
|
---|
78 | {
|
---|
79 | QMutexLocker lock(&mutex);
|
---|
80 | if (--count == 0)
|
---|
81 | waitCondition.wakeAll();
|
---|
82 | return count;
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Wait until all resources are released.
|
---|
86 | void wait()
|
---|
87 | {
|
---|
88 | QMutexLocker lock(&mutex);
|
---|
89 | if (count != 0)
|
---|
90 | waitCondition.wait(&mutex);
|
---|
91 | }
|
---|
92 |
|
---|
93 | int currentCount()
|
---|
94 | {
|
---|
95 | return count;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // releases a resource, unless this is the last resource.
|
---|
99 | // returns true if a resource was released.
|
---|
100 | bool releaseUnlessLast()
|
---|
101 | {
|
---|
102 | QMutexLocker lock(&mutex);
|
---|
103 | if (count == 1)
|
---|
104 | return false;
|
---|
105 | --count;
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | private:
|
---|
110 | QMutex mutex;
|
---|
111 | int count;
|
---|
112 | QWaitCondition waitCondition;
|
---|
113 | };
|
---|
114 |
|
---|
115 | enum ThreadFunctionResult { ThrottleThread, ThreadFinished };
|
---|
116 |
|
---|
117 | // The ThreadEngine controls the threads used in the computation.
|
---|
118 | // Can be run in three modes: single threaded, multi-threaded blocking
|
---|
119 | // and multi-threaded asynchronous.
|
---|
120 | // The code for the single threaded mode is
|
---|
121 | class Q_CORE_EXPORT ThreadEngineBase: public QRunnable
|
---|
122 | {
|
---|
123 | public:
|
---|
124 | // Public API:
|
---|
125 | ThreadEngineBase();
|
---|
126 | virtual ~ThreadEngineBase();
|
---|
127 | void startSingleThreaded();
|
---|
128 | void startBlocking();
|
---|
129 | void startThread();
|
---|
130 | bool isCanceled();
|
---|
131 | void waitForResume();
|
---|
132 | bool isProgressReportingEnabled();
|
---|
133 | void setProgressValue(int progress);
|
---|
134 | void setProgressRange(int minimum, int maximum);
|
---|
135 |
|
---|
136 | protected: // The user overrides these:
|
---|
137 | virtual void start() {}
|
---|
138 | virtual void finish() {}
|
---|
139 | virtual ThreadFunctionResult threadFunction() { return ThreadFinished; }
|
---|
140 | virtual bool shouldStartThread() { return futureInterface ? !futureInterface->isPaused() : true; }
|
---|
141 | virtual bool shouldThrottleThread() { return futureInterface ? futureInterface->isPaused() : false; }
|
---|
142 | private:
|
---|
143 | bool startThreadInternal();
|
---|
144 | void startThreads();
|
---|
145 | void threadExit();
|
---|
146 | bool threadThrottleExit();
|
---|
147 | void run();
|
---|
148 | virtual void asynchronousFinish() = 0;
|
---|
149 | #ifndef QT_NO_EXCEPTIONS
|
---|
150 | void handleException(const QtConcurrent::Exception &exception);
|
---|
151 | #endif
|
---|
152 | protected:
|
---|
153 | QFutureInterfaceBase *futureInterface;
|
---|
154 | QThreadPool *threadPool;
|
---|
155 | ThreadEngineSemaphore semaphore;
|
---|
156 | QtConcurrent::internal::ExceptionStore exceptionStore;
|
---|
157 | };
|
---|
158 |
|
---|
159 |
|
---|
160 | template <typename T>
|
---|
161 | class ThreadEngine : public virtual ThreadEngineBase
|
---|
162 | {
|
---|
163 | public:
|
---|
164 | typedef T ResultType;
|
---|
165 |
|
---|
166 | virtual T *result() { return 0; }
|
---|
167 |
|
---|
168 | QFutureInterface<T> *futureInterfaceTyped()
|
---|
169 | {
|
---|
170 | return static_cast<QFutureInterface<T> *>(futureInterface);
|
---|
171 | }
|
---|
172 |
|
---|
173 | // Runs the user algorithm using a single thread.
|
---|
174 | T *startSingleThreaded()
|
---|
175 | {
|
---|
176 | ThreadEngineBase::startSingleThreaded();
|
---|
177 | return result();
|
---|
178 | }
|
---|
179 |
|
---|
180 | // Runs the user algorithm using multiple threads.
|
---|
181 | // This function blocks until the algorithm is finished,
|
---|
182 | // and then returns the result.
|
---|
183 | T *startBlocking()
|
---|
184 | {
|
---|
185 | ThreadEngineBase::startBlocking();
|
---|
186 | return result();
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Runs the user algorithm using multiple threads.
|
---|
190 | // Does not block, returns a future.
|
---|
191 | QFuture<T> startAsynchronously()
|
---|
192 | {
|
---|
193 | futureInterface = new QFutureInterface<T>();
|
---|
194 |
|
---|
195 | // reportStart() must be called before starting threads, otherwise the
|
---|
196 | // user algorithm might finish while reportStart() is running, which
|
---|
197 | // is very bad.
|
---|
198 | futureInterface->reportStarted();
|
---|
199 | QFuture<T> future = QFuture<T>(futureInterfaceTyped());
|
---|
200 | start();
|
---|
201 |
|
---|
202 | semaphore.acquire();
|
---|
203 | threadPool->start(this);
|
---|
204 | return future;
|
---|
205 | }
|
---|
206 |
|
---|
207 | void asynchronousFinish()
|
---|
208 | {
|
---|
209 | finish();
|
---|
210 | futureInterfaceTyped()->reportFinished(result());
|
---|
211 | delete futureInterfaceTyped();
|
---|
212 | delete this;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void reportResult(const T *_result, int index = -1)
|
---|
217 | {
|
---|
218 | if (futureInterface)
|
---|
219 | futureInterfaceTyped()->reportResult(_result, index);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void reportResults(const QVector<T> &_result, int index = -1, int count = -1)
|
---|
223 | {
|
---|
224 | if (futureInterface)
|
---|
225 | futureInterfaceTyped()->reportResults(_result, index, count);
|
---|
226 | }
|
---|
227 | };
|
---|
228 |
|
---|
229 | // The ThreadEngineStarter class ecapsulates the return type
|
---|
230 | // from the thread engine.
|
---|
231 | // Depending on how the it is used, it will run
|
---|
232 | // the engine in either blocking mode or asynchronous mode.
|
---|
233 | template <typename T>
|
---|
234 | class ThreadEngineStarterBase
|
---|
235 | {
|
---|
236 | public:
|
---|
237 | ThreadEngineStarterBase(ThreadEngine<T> *_threadEngine)
|
---|
238 | : threadEngine(_threadEngine) { }
|
---|
239 |
|
---|
240 | inline ThreadEngineStarterBase(const ThreadEngineStarterBase &other)
|
---|
241 | : threadEngine(other.threadEngine) { }
|
---|
242 |
|
---|
243 | QFuture<T> startAsynchronously()
|
---|
244 | {
|
---|
245 | return threadEngine->startAsynchronously();
|
---|
246 | }
|
---|
247 |
|
---|
248 | operator QFuture<T>()
|
---|
249 | {
|
---|
250 | return startAsynchronously();
|
---|
251 | }
|
---|
252 |
|
---|
253 | protected:
|
---|
254 | ThreadEngine<T> *threadEngine;
|
---|
255 | };
|
---|
256 |
|
---|
257 |
|
---|
258 | // We need to factor out the code that dereferences the T pointer,
|
---|
259 | // with a specialization where T is void. (code that dereferences a void *
|
---|
260 | // won't compile)
|
---|
261 | template <typename T>
|
---|
262 | class ThreadEngineStarter : public ThreadEngineStarterBase<T>
|
---|
263 | {
|
---|
264 | public:
|
---|
265 | ThreadEngineStarter(ThreadEngine<T> *threadEngine)
|
---|
266 | :ThreadEngineStarterBase<T>(threadEngine) {}
|
---|
267 |
|
---|
268 | T startBlocking()
|
---|
269 | {
|
---|
270 | T t = *this->threadEngine->startBlocking();
|
---|
271 | delete this->threadEngine;
|
---|
272 | return t;
|
---|
273 | }
|
---|
274 | };
|
---|
275 |
|
---|
276 | // Full template specialization where T is void.
|
---|
277 | template <>
|
---|
278 | class ThreadEngineStarter<void> : public ThreadEngineStarterBase<void>
|
---|
279 | {
|
---|
280 | public:
|
---|
281 | ThreadEngineStarter<void>(ThreadEngine<void> *_threadEngine)
|
---|
282 | :ThreadEngineStarterBase<void>(_threadEngine) {}
|
---|
283 |
|
---|
284 | void startBlocking()
|
---|
285 | {
|
---|
286 | this->threadEngine->startBlocking();
|
---|
287 | delete this->threadEngine;
|
---|
288 | }
|
---|
289 | };
|
---|
290 |
|
---|
291 | template <typename ThreadEngine>
|
---|
292 | inline ThreadEngineStarter<typename ThreadEngine::ResultType> startThreadEngine(ThreadEngine *threadEngine)
|
---|
293 | {
|
---|
294 | return ThreadEngineStarter<typename ThreadEngine::ResultType>(threadEngine);
|
---|
295 | }
|
---|
296 |
|
---|
297 | } // namespace QtConcurrent
|
---|
298 |
|
---|
299 | #endif //qdoc
|
---|
300 |
|
---|
301 | QT_END_NAMESPACE
|
---|
302 | QT_END_HEADER
|
---|
303 |
|
---|
304 | #endif // QT_NO_CONCURRENT
|
---|
305 |
|
---|
306 | #endif
|
---|