44 #ifndef QT_NO_CONCURRENT 50 ThreadEngineBarrier::ThreadEngineBarrier()
53 void ThreadEngineBarrier::acquire()
56 int localCount = int(count);
58 if (count.testAndSetOrdered(localCount, localCount -1))
61 if (count.testAndSetOrdered(localCount, localCount + 1))
67 int ThreadEngineBarrier::release()
70 int localCount = int(count);
71 if (localCount == -1) {
72 if (count.testAndSetOrdered(-1, 0)) {
76 }
else if (localCount < 0) {
77 if (count.testAndSetOrdered(localCount, localCount + 1))
78 return qAbs(localCount + 1);
80 if (count.testAndSetOrdered(localCount, localCount - 1))
81 return localCount - 1;
87 void ThreadEngineBarrier::wait()
90 int localCount = int(count);
95 if (count.testAndSetOrdered(localCount, -localCount)) {
102 int ThreadEngineBarrier::currentCount()
109 bool ThreadEngineBarrier::releaseUnlessLast()
112 int localCount = int(count);
113 if (
qAbs(localCount) == 1) {
115 }
else if (localCount < 0) {
116 if (count.testAndSetOrdered(localCount, localCount + 1))
119 if (count.testAndSetOrdered(localCount, localCount - 1))
125 ThreadEngineBase::ThreadEngineBase()
126 :futureInterface(0), threadPool(
QThreadPool::globalInstance())
128 setAutoDelete(
false);
131 ThreadEngineBase::~ThreadEngineBase() {}
133 void ThreadEngineBase::startSingleThreaded()
136 while (threadFunction() != ThreadFinished)
141 void ThreadEngineBase::startBlocking()
147 bool throttled =
false;
148 #ifndef QT_NO_EXCEPTIONS 151 while (threadFunction() == ThrottleThread) {
152 if (threadThrottleExit()) {
157 #ifndef QT_NO_EXCEPTIONS 165 if (throttled ==
false) {
171 exceptionStore.throwPossibleException();
174 void ThreadEngineBase::startThread()
176 startThreadInternal();
179 void ThreadEngineBase::acquireBarrierSemaphore()
184 bool ThreadEngineBase::isCanceled()
187 return futureInterface->isCanceled();
192 void ThreadEngineBase::waitForResume()
195 futureInterface->waitForResume();
198 bool ThreadEngineBase::isProgressReportingEnabled()
201 return (futureInterface != 0);
204 void ThreadEngineBase::setProgressValue(
int progress)
207 futureInterface->setProgressValue(progress);
210 void ThreadEngineBase::setProgressRange(
int minimum,
int maximum)
213 futureInterface->setProgressRange(minimum, maximum);
216 bool ThreadEngineBase::startThreadInternal()
218 if (this->isCanceled())
222 if (!threadPool->tryStart(
this)) {
229 void ThreadEngineBase::startThreads()
231 while (shouldStartThread() && startThreadInternal())
235 void ThreadEngineBase::threadExit()
237 const bool asynchronous = futureInterface != 0;
238 const int lastThread = (barrier.release() == 0);
240 if (lastThread && asynchronous)
241 this->asynchronousFinish();
247 bool ThreadEngineBase::threadThrottleExit()
249 return barrier.releaseUnlessLast();
254 if (this->isCanceled()) {
261 #ifndef QT_NO_EXCEPTIONS 264 while (threadFunction() == ThrottleThread) {
269 if (threadThrottleExit())
273 #ifndef QT_NO_EXCEPTIONS 283 #ifndef QT_NO_EXCEPTIONS 288 futureInterface->reportException(exception);
290 exceptionStore.setException(exception);
299 #endif // QT_NO_CONCURRENT #define QT_END_NAMESPACE
This macro expands to.
The UnhandledException class represents an unhandled exception in a worker thread.
Q_DECL_CONSTEXPR T qAbs(const T &t)
The QThreadPool class manages a collection of QThreads.
#define QT_BEGIN_NAMESPACE
This macro expands to.
The Exception class provides a base class for exceptions that can transferred across threads...
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded pro...
QFuture< T > run(Function function,...)
#define forever
This macro is provided for convenience for writing infinite loops.