IterateKernel< Iterator, T > Class Template Reference


Detailed Description

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

Definition at line 178 of file qtconcurrentiteratekernel.h.

#include <qtconcurrentiteratekernel.h>

Inheritance diagram for IterateKernel< Iterator, T >:
Inheritance graph
[legend]

List of all members.

Public Types

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 Attributes

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

Member Typedef Documentation


Constructor & Destructor Documentation

IterateKernel ( Iterator  _begin,
Iterator  _end 
) [inline]

Definition at line 183 of file qtconcurrentiteratekernel.h.

        : begin(_begin), end(_end), current(_begin), currentIndex(0),
           forIteration(false), progressReportingEnabled(true)
#elif !defined(QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION)
        : begin(_begin), end(_end), current(_begin), currentIndex(0),
           forIteration(selectIteration(typename std::iterator_traits<Iterator>::iterator_category())), progressReportingEnabled(true)
#else
        : begin(_begin), end(_end), currentIndex(0),
          forIteration(selectIteration(std::iterator_category(_begin))), progressReportingEnabled(true)
#endif
    {
#if defined (QT_NO_STL)
       iterationCount = 0;
#else
        iterationCount =  forIteration ? std::distance(_begin, _end) : 0;

#endif
    }
virtual ~IterateKernel ( ) [inline, virtual]

Definition at line 203 of file qtconcurrentiteratekernel.h.

{ }

Member Function Documentation

virtual bool runIterations ( Iterator  _begin,
int  beginIndex,
int  endIndex,
T *  results 
) [inline, virtual]
ThreadFunctionResult threadFunction ( ) [inline, virtual]

Reimplemented from ThreadEngineBase.

Definition at line 225 of file qtconcurrentiteratekernel.h.

    {
        if (forIteration)
            return this->forThreadFunction();
        else // whileIteration
            return this->whileThreadFunction();
    }
ThreadFunctionResult forThreadFunction ( ) [inline]

Definition at line 233 of file qtconcurrentiteratekernel.h.

    {
        BlockSizeManager blockSizeManager(iterationCount);
        ResultReporter<T> resultReporter(this);

        for(;;) {
            if (this->isCanceled())
                break;

            const int currentBlockSize = blockSizeManager.blockSize();

            if (currentIndex >= iterationCount)
                break;

            // Atomically reserve a block of iterationCount for this thread.
            const int beginIndex = currentIndex.fetchAndAddRelease(currentBlockSize);
            const int endIndex = qMin(beginIndex + currentBlockSize, iterationCount);

            if (beginIndex >= endIndex) {
                // No more work
                break;
            }

            this->waitForResume(); // (only waits if the qfuture is paused.)

            if (shouldStartThread())
                this->startThread();

            const int finalBlockSize = endIndex - beginIndex; // block size adjusted for possible end-of-range
            resultReporter.reserveSpace(finalBlockSize);

            // Call user code with the current iteration range.
            blockSizeManager.timeBeforeUser();
            const bool resultsAvailable = this->runIterations(begin, beginIndex, endIndex, resultReporter.getPointer());
            blockSizeManager.timeAfterUser();

            if (resultsAvailable)
                resultReporter.reportResults(beginIndex);

            // Report progress if progress reporting enabled.
            if (progressReportingEnabled) {
                completed.fetchAndAddAcquire(finalBlockSize);
                this->setProgressValue(this->completed);
            }

            if (this->shouldThrottleThread())
                return ThrottleThread;
        }
        return ThreadFinished;
    }
ThreadFunctionResult whileThreadFunction ( ) [inline]

Definition at line 284 of file qtconcurrentiteratekernel.h.

    {
        if (iteratorThreads.testAndSetAcquire(0, 1) == false)
            return ThreadFinished;

        ResultReporter<T> resultReporter(this);
        resultReporter.reserveSpace(1);

        while (current != end) {
            // The following two lines breaks support for input iterators according to
            // the sgi docs: dereferencing prev after calling ++current is not allowed
            // on input iterators. (prev is dereferenced inside user.runIteration())
            Iterator prev = current;
            ++current;
            int index = currentIndex.fetchAndAddRelaxed(1);
            iteratorThreads.testAndSetRelease(1, 0);

            this->waitForResume(); // (only waits if the qfuture is paused.)

            if (shouldStartThread())
                this->startThread();

            const bool resultAavailable = this->runIteration(prev, index, resultReporter.getPointer());
            if (resultAavailable)
                resultReporter.reportResults(index);

            if (this->shouldThrottleThread())
                return ThrottleThread;

            if (iteratorThreads.testAndSetAcquire(0, 1) == false)
                return ThreadFinished;
        }

        return ThreadFinished;
    }

Member Data Documentation

const Iterator begin
const Iterator end

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