IterateKernel< Iterator, T > Class Template Reference

IterateKernel< Iterator, T > Class Template Reference

#include <qtconcurrentiteratekernel.h>

Class Description

template<typename Iterator, typename T>
class QtConcurrent::IterateKernel< Iterator, T >

Definition at line 178 of file qtconcurrentiteratekernel.h.

+ Inheritance diagram for IterateKernel< Iterator, T >:

Public Types

typedef T ResultType
 
- Public Types inherited from ThreadEngine< T >
typedef T ResultType
 

Public Member Functions

 IterateKernel (Iterator _begin, Iterator _end)
 
virtual ~IterateKernel ()
 
virtual bool runIteration (Iterator it, int index, T *result)
 
virtual bool runIterations (Iterator _begin, int beginIndex, int endIndex, T *results)
 
void start ()
 
bool shouldStartThread ()
 
ThreadFunctionResult threadFunction ()
 
ThreadFunctionResult forThreadFunction ()
 
ThreadFunctionResult whileThreadFunction ()
 
- Public Member Functions inherited from ThreadEngine< T >
virtual T * result ()
 
QFutureInterface< T > * futureInterfaceTyped ()
 
T * startSingleThreaded ()
 
T * startBlocking ()
 
QFuture< T > startAsynchronously ()
 
void asynchronousFinish ()
 
void reportResult (const T *_result, int index=-1)
 
void reportResults (const QVector< T > &_result, int index=-1, int count=-1)
 
- Public Member Functions inherited from ThreadEngineBase
 ThreadEngineBase ()
 
virtual ~ThreadEngineBase ()
 
void startSingleThreaded ()
 
void startBlocking ()
 
void startThread ()
 
bool isCanceled ()
 
void waitForResume ()
 
bool isProgressReportingEnabled ()
 
void setProgressValue (int progress)
 
void setProgressRange (int minimum, int maximum)
 
void acquireBarrierSemaphore ()
 
- Public Member Functions inherited from QRunnable
 QRunnable ()
 
virtual ~QRunnable ()
 
bool autoDelete () const
 
void setAutoDelete (bool _autoDelete)
 

Public Attributes

const Iterator begin
 
const Iterator end
 
Iterator current
 
QAtomicInt currentIndex
 
bool forIteration
 
QAtomicInt iteratorThreads
 
int iterationCount
 
bool progressReportingEnabled
 
QAtomicInt completed
 

Additional Inherited Members

- Protected Member Functions inherited from ThreadEngineBase
virtual void finish ()
 
virtual bool shouldThrottleThread ()
 
- Protected Attributes inherited from ThreadEngineBase
QFutureInterfaceBasefutureInterface
 
QThreadPoolthreadPool
 
ThreadEngineBarrier barrier
 
QtConcurrent::internal::ExceptionStore exceptionStore
 

Member Typedef Documentation

typedef T ResultType

Definition at line 181 of file qtconcurrentiteratekernel.h.

Constructor & Destructor Documentation

IterateKernel ( Iterator  _begin,
Iterator  _end 
)
inline

Definition at line 183 of file qtconcurrentiteratekernel.h.

185  : begin(_begin), end(_end), current(_begin), currentIndex(0),
187 #else
188  : begin(_begin), end(_end), current(_begin), currentIndex(0),
189  forIteration(selectIteration(typename std::iterator_traits<Iterator>::iterator_category())), progressReportingEnabled(true)
190 #endif
191  {
192 #if defined (QT_NO_STL)
193  iterationCount = 0;
194 #else
195  iterationCount = forIteration ? std::distance(_begin, _end) : 0;
196 
197 #endif
198  }
bool selectIteration(std::bidirectional_iterator_tag)
virtual ~IterateKernel ( )
inlinevirtual

Definition at line 200 of file qtconcurrentiteratekernel.h.

200 { }

Member Function Documentation

virtual bool runIteration ( Iterator  it,
int  index,
T *  result 
)
inlinevirtual
virtual bool runIterations ( Iterator  _begin,
int  beginIndex,
int  endIndex,
T *  results 
)
inlinevirtual
void start ( )
inlinevirtual
bool shouldStartThread ( )
inlinevirtual

Reimplemented from ThreadEngineBase.

Definition at line 214 of file qtconcurrentiteratekernel.h.

ThreadFunctionResult threadFunction ( )
inlinevirtual

Reimplemented from ThreadEngineBase.

Definition at line 222 of file qtconcurrentiteratekernel.h.

223  {
224  if (forIteration)
225  return this->forThreadFunction();
226  else // whileIteration
227  return this->whileThreadFunction();
228  }
ThreadFunctionResult whileThreadFunction()
ThreadFunctionResult forThreadFunction()
ThreadFunctionResult forThreadFunction ( )
inline

Definition at line 230 of file qtconcurrentiteratekernel.h.

231  {
232  BlockSizeManager blockSizeManager(iterationCount);
233  ResultReporter<T> resultReporter(this);
234 
235  for(;;) {
236  if (this->isCanceled())
237  break;
238 
239  const int currentBlockSize = blockSizeManager.blockSize();
240 
242  break;
243 
244  // Atomically reserve a block of iterationCount for this thread.
245  const int beginIndex = currentIndex.fetchAndAddRelease(currentBlockSize);
246  const int endIndex = qMin(beginIndex + currentBlockSize, iterationCount);
247 
248  if (beginIndex >= endIndex) {
249  // No more work
250  break;
251  }
252 
253  this->waitForResume(); // (only waits if the qfuture is paused.)
254 
255  if (shouldStartThread())
256  this->startThread();
257 
258  const int finalBlockSize = endIndex - beginIndex; // block size adjusted for possible end-of-range
259  resultReporter.reserveSpace(finalBlockSize);
260 
261  // Call user code with the current iteration range.
262  blockSizeManager.timeBeforeUser();
263  const bool resultsAvailable = this->runIterations(begin, beginIndex, endIndex, resultReporter.getPointer());
264  blockSizeManager.timeAfterUser();
265 
266  if (resultsAvailable)
267  resultReporter.reportResults(beginIndex);
268 
269  // Report progress if progress reporting enabled.
271  completed.fetchAndAddAcquire(finalBlockSize);
272  this->setProgressValue(this->completed);
273  }
274 
275  if (this->shouldThrottleThread())
276  return ThrottleThread;
277  }
278  return ThreadFinished;
279  }
void setProgressValue(int progress)
int fetchAndAddAcquire(int valueToAdd)
virtual bool runIterations(Iterator _begin, int beginIndex, int endIndex, T *results)
int fetchAndAddRelease(int valueToAdd)
ThreadFunctionResult whileThreadFunction ( )
inline

Definition at line 281 of file qtconcurrentiteratekernel.h.

282  {
283  if (iteratorThreads.testAndSetAcquire(0, 1) == false)
284  return ThreadFinished;
285 
286  ResultReporter<T> resultReporter(this);
287  resultReporter.reserveSpace(1);
288 
289  while (current != end) {
290  // The following two lines breaks support for input iterators according to
291  // the sgi docs: dereferencing prev after calling ++current is not allowed
292  // on input iterators. (prev is dereferenced inside user.runIteration())
293  Iterator prev = current;
294  ++current;
297 
298  this->waitForResume(); // (only waits if the qfuture is paused.)
299 
300  if (shouldStartThread())
301  this->startThread();
302 
303  const bool resultAavailable = this->runIteration(prev, index, resultReporter.getPointer());
304  if (resultAavailable)
305  resultReporter.reportResults(index);
306 
307  if (this->shouldThrottleThread())
308  return ThrottleThread;
309 
310  if (iteratorThreads.testAndSetAcquire(0, 1) == false)
311  return ThreadFinished;
312  }
313 
314  return ThreadFinished;
315  }
bool testAndSetRelease(int expectedValue, int newValue)
int fetchAndAddRelaxed(int valueToAdd)
virtual bool runIteration(Iterator it, int index, T *result)
bool testAndSetAcquire(int expectedValue, int newValue)
GLuint GLuint end
Definition: GLee.h:872
GLuint index
Definition: GLee.h:1704

Member Data Documentation

const Iterator begin

Definition at line 319 of file qtconcurrentiteratekernel.h.

const Iterator end

Definition at line 320 of file qtconcurrentiteratekernel.h.

Iterator current

Definition at line 321 of file qtconcurrentiteratekernel.h.

QAtomicInt currentIndex

Definition at line 322 of file qtconcurrentiteratekernel.h.

bool forIteration

Definition at line 323 of file qtconcurrentiteratekernel.h.

QAtomicInt iteratorThreads

Definition at line 324 of file qtconcurrentiteratekernel.h.

int iterationCount

Definition at line 325 of file qtconcurrentiteratekernel.h.

bool progressReportingEnabled

Definition at line 327 of file qtconcurrentiteratekernel.h.

QAtomicInt completed

Definition at line 328 of file qtconcurrentiteratekernel.h.


The documentation for this class was generated from the following file: