diff options
Diffstat (limited to 'src/corelib/concurrent')
-rw-r--r-- | src/corelib/concurrent/qfuture.cpp | 6 | ||||
-rw-r--r-- | src/corelib/concurrent/qfuturesynchronizer.cpp | 6 | ||||
-rw-r--r-- | src/corelib/concurrent/qfuturewatcher.cpp | 10 | ||||
-rw-r--r-- | src/corelib/concurrent/qfuturewatcher_p.h | 6 | ||||
-rw-r--r-- | src/corelib/concurrent/qrunnable.cpp | 2 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentfilter.cpp | 4 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentiteratekernel.cpp | 16 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentiteratekernel.h | 23 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentmap.cpp | 15 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentrun.cpp | 6 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentthreadengine.cpp | 94 | ||||
-rw-r--r-- | src/corelib/concurrent/qtconcurrentthreadengine.h | 79 | ||||
-rw-r--r-- | src/corelib/concurrent/qthreadpool.cpp | 20 |
13 files changed, 193 insertions, 94 deletions
diff --git a/src/corelib/concurrent/qfuture.cpp b/src/corelib/concurrent/qfuture.cpp index 5f5f6c6..d2e4717 100644 --- a/src/corelib/concurrent/qfuture.cpp +++ b/src/corelib/concurrent/qfuture.cpp @@ -44,8 +44,10 @@ \brief The QFuture class represents the result of an asynchronous computation. \since 4.4 + \ingroup thread + To start a computation, use one of the APIs in the - \l {threads.html#qtconcurrent-intro}{Qt Concurrent} framework. + \l {Concurrent Programming}{Qt Concurrent} framework. QFuture allows threads to be synchronized against one or more results which will be ready at a later point in time. The result can be of any type @@ -90,7 +92,7 @@ To interact with running tasks using signals and slots, use QFutureWatcher. - \sa QFutureWatcher, {threads.html#qtconcurrent-intro}{Qt Concurrent} + \sa QFutureWatcher, {Concurrent Programming}{Qt Concurrent} */ /*! \fn QFuture::QFuture() diff --git a/src/corelib/concurrent/qfuturesynchronizer.cpp b/src/corelib/concurrent/qfuturesynchronizer.cpp index 01b3a96..f4a97ff 100644 --- a/src/corelib/concurrent/qfuturesynchronizer.cpp +++ b/src/corelib/concurrent/qfuturesynchronizer.cpp @@ -44,7 +44,9 @@ \brief The QFutureSynchronizer class is a convenience class that simplifies QFuture synchronization. - + + \ingroup thread + QFutureSynchronizer is a template class that simplifies synchronization of one or more QFuture objects. Futures are added using the addFuture() or setFuture() functions. The futures() function returns a list of futures. @@ -63,7 +65,7 @@ You can query the status of the cancel-on-wait feature using the cancelOnWait() function. - \sa QFuture, QFutureWatcher, {threads.html#qtconcurrent-intro}{Qt Concurrent} + \sa QFuture, QFutureWatcher, {Concurrent Programming}{Qt Concurrent} */ /*! diff --git a/src/corelib/concurrent/qfuturewatcher.cpp b/src/corelib/concurrent/qfuturewatcher.cpp index 640ad3c..ad2b2da 100644 --- a/src/corelib/concurrent/qfuturewatcher.cpp +++ b/src/corelib/concurrent/qfuturewatcher.cpp @@ -43,9 +43,9 @@ #ifndef QT_NO_QFUTURE -#include <QEvent> -#include <QCoreApplication> -#include <QThread> +#include <QtCore/qcoreevent.h> +#include <QtCore/qcoreapplication.h> +#include <QtCore/qthread.h> #include "qfuturewatcher_p.h" @@ -55,6 +55,8 @@ QT_BEGIN_NAMESPACE \reentrant \since 4.4 + \ingroup thread + \brief The QFutureWatcher class allows monitoring a QFuture using signals and slots. @@ -94,7 +96,7 @@ QT_BEGIN_NAMESPACE QFutureWatcher<void> as well. This is useful if only status or progress information is needed; not the actual result data. - \sa QFuture, {threads.html#qtconcurrent-intro}{Qt Concurrent} + \sa QFuture, {Concurrent Programming}{Qt Concurrent} */ /*! \fn QFutureWatcher::QFutureWatcher(QObject *parent) diff --git a/src/corelib/concurrent/qfuturewatcher_p.h b/src/corelib/concurrent/qfuturewatcher_p.h index 90544f2..99cf85b 100644 --- a/src/corelib/concurrent/qfuturewatcher_p.h +++ b/src/corelib/concurrent/qfuturewatcher_p.h @@ -54,7 +54,7 @@ // #include "qfutureinterface_p.h" -#include <QList> +#include <qlist.h> #ifndef QT_NO_QFUTURE @@ -63,8 +63,8 @@ QT_BEGIN_NAMESPACE class QFutureWatcherBase; -class Q_CORE_EXPORT QFutureWatcherBasePrivate : public QObjectPrivate, - public QFutureCallOutInterface +class QFutureWatcherBasePrivate : public QObjectPrivate, + public QFutureCallOutInterface { Q_DECLARE_PUBLIC(QFutureWatcherBase) diff --git a/src/corelib/concurrent/qrunnable.cpp b/src/corelib/concurrent/qrunnable.cpp index fb946ed..829f63e 100644 --- a/src/corelib/concurrent/qrunnable.cpp +++ b/src/corelib/concurrent/qrunnable.cpp @@ -44,6 +44,8 @@ \since 4.4 \brief The QRunnable class is the base class for all runnable objects. + \ingroup thread + The QRunnable class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the run() function. diff --git a/src/corelib/concurrent/qtconcurrentfilter.cpp b/src/corelib/concurrent/qtconcurrentfilter.cpp index 93d79f7..805e784 100644 --- a/src/corelib/concurrent/qtconcurrentfilter.cpp +++ b/src/corelib/concurrent/qtconcurrentfilter.cpp @@ -42,12 +42,12 @@ /*! \headerfile <QtConcurrentFilter> \title Concurrent Filter and Filter-Reduce - \ingroup threading + \ingroup thread \brief The <QtConcurrentFilter> header provides concurrent Filter and Filter-Reduce. - These functions are a part of the \l {threads.html#qtconcurrent-intro}{Qt Concurrent} framework. + These functions are a part of the \l {Concurrent Programming}{Qt Concurrent} framework. The QtConcurrent::filter(), QtConcurrent::filtered() and QtConcurrent::filteredReduced() functions filter items in a sequence such diff --git a/src/corelib/concurrent/qtconcurrentiteratekernel.cpp b/src/corelib/concurrent/qtconcurrentiteratekernel.cpp index a9106bd..15b8ecd 100644 --- a/src/corelib/concurrent/qtconcurrentiteratekernel.cpp +++ b/src/corelib/concurrent/qtconcurrentiteratekernel.cpp @@ -42,22 +42,18 @@ #include "qtconcurrentiteratekernel.h" #if defined(Q_OS_MAC) - #include <mach/mach.h> #include <mach/mach_time.h> #include <unistd.h> - #elif defined(Q_OS_UNIX) - #include <time.h> #include <unistd.h> - #elif defined(Q_OS_WIN) - -#include <windows.h> - +#include <qt_windows.h> #endif +#include "private/qfunctions_p.h" + #ifndef QT_NO_CONCURRENT @@ -108,11 +104,17 @@ static qint64 getticks() return 0; return (ts.tv_sec * 1000000000) + ts.tv_nsec; #else + +#ifdef Q_OS_SYMBIAN + return clock(); +#else // no clock_gettime(), fall back to wall time struct timeval tv; gettimeofday(&tv, 0); return (tv.tv_sec * 1000000) + tv.tv_usec; #endif + +#endif } #elif defined(Q_OS_WIN) diff --git a/src/corelib/concurrent/qtconcurrentiteratekernel.h b/src/corelib/concurrent/qtconcurrentiteratekernel.h index 341b075..ab50e42 100644 --- a/src/corelib/concurrent/qtconcurrentiteratekernel.h +++ b/src/corelib/concurrent/qtconcurrentiteratekernel.h @@ -49,8 +49,10 @@ #include <QtCore/qatomic.h> #include <QtCore/qtconcurrentmedian.h> #include <QtCore/qtconcurrentthreadengine.h> -#include <iterator> +#ifndef QT_NO_STL +# include <iterator> +#endif QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -148,6 +150,7 @@ public: inline void * getPointer() { return 0; } }; +#ifndef QT_NO_STL inline bool selectIteration(std::bidirectional_iterator_tag) { return false; // while @@ -162,6 +165,14 @@ inline bool selectIteration(std::random_access_iterator_tag) { return true; // for } +#else +// no stl support, always use while iteration +template <typename T> +inline bool selectIteration(T) +{ + return false; // while +} +#endif template <typename Iterator, typename T> class IterateKernel : public ThreadEngine<T> @@ -170,7 +181,10 @@ public: typedef T ResultType; IterateKernel(Iterator _begin, Iterator _end) -#ifndef QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION +#if defined (QT_NO_STL) + : 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 @@ -178,7 +192,12 @@ public: 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() { } diff --git a/src/corelib/concurrent/qtconcurrentmap.cpp b/src/corelib/concurrent/qtconcurrentmap.cpp index 989c989..0a3a1c1 100644 --- a/src/corelib/concurrent/qtconcurrentmap.cpp +++ b/src/corelib/concurrent/qtconcurrentmap.cpp @@ -1,4 +1,4 @@ - /**************************************************************************** +/**************************************************************************** ** ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. @@ -41,13 +41,16 @@ /*! \namespace QtConcurrent + \inmodule QtCore \since 4.4 \brief The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives. - See the \l {threads.html#qtconcurrent-intro}{Qt Concurrent} section in the \l{threads.html}{threading} documentation. - + See the \l {Concurrent Programming}{Qt Concurrent} chapter in + the \l{threads.html}{threading} documentation. + + \inheaderfile QtCore \ingroup thread */ @@ -57,8 +60,6 @@ \brief The QtConcurrent::internal namespace contains QtConcurrent implementation details. - - \ingroup thread */ /*! @@ -77,11 +78,11 @@ /*! \headerfile <QtConcurrentMap> \title Concurrent Map and Map-Reduce - \ingroup threading + \ingroup thread \brief The <QtConcurrentMap> header provides concurrent Map and MapReduce. - These functions are a part of the \l {threads.html#qtconcurrent-intro}{Qt Concurrent} framework. + These functions are a part of the \l {Concurrent Programming}{Qt Concurrent} framework. The QtConcurrent::map(), QtConcurrent::mapped() and QtConcurrent::mappedReduced() functions run computations in parallel on diff --git a/src/corelib/concurrent/qtconcurrentrun.cpp b/src/corelib/concurrent/qtconcurrentrun.cpp index 433ab10..36e8914 100644 --- a/src/corelib/concurrent/qtconcurrentrun.cpp +++ b/src/corelib/concurrent/qtconcurrentrun.cpp @@ -42,11 +42,13 @@ /*! \headerfile <QtConcurrentRun> \title Asynchronous Run - + \brief The <QtConcurrentRun> header provides a way to run a function in a separate thread. + + \ingroup thread - This function is a part of the \l {threads.html#qtconcurrent-intro}{Qt Concurrent} framework. + This function is a part of the \l {Concurrent Programming}{Qt Concurrent} framework. The QtConcurrent::run() function runs a function in a separate thread. The return value of the function is made available through the QFuture API. diff --git a/src/corelib/concurrent/qtconcurrentthreadengine.cpp b/src/corelib/concurrent/qtconcurrentthreadengine.cpp index 05a748b..4df96fa 100644 --- a/src/corelib/concurrent/qtconcurrentthreadengine.cpp +++ b/src/corelib/concurrent/qtconcurrentthreadengine.cpp @@ -47,6 +47,81 @@ QT_BEGIN_NAMESPACE namespace QtConcurrent { +ThreadEngineBarrier::ThreadEngineBarrier() +:count(0) { } + +void ThreadEngineBarrier::acquire() +{ + forever { + int localCount = int(count); + if (localCount < 0) { + if (count.testAndSetOrdered(localCount, localCount -1)) + return; + } else { + if (count.testAndSetOrdered(localCount, localCount + 1)) + return; + } + } +} + +int ThreadEngineBarrier::release() +{ + forever { + int localCount = int(count); + if (localCount == -1) { + if (count.testAndSetOrdered(-1, 0)) { + semaphore.release(); + return 0; + } + } else if (localCount < 0) { + if (count.testAndSetOrdered(localCount, localCount + 1)) + return qAbs(localCount + 1); + } else { + if (count.testAndSetOrdered(localCount, localCount - 1)) + return localCount - 1; + } + } +} + +// Wait until all threads have been released +void ThreadEngineBarrier::wait() +{ + forever { + int localCount = int(count); + if (localCount == 0) + return; + + Q_ASSERT(localCount > 0); // multiple waiters are not allowed. + if (count.testAndSetOrdered(localCount, -localCount)) { + semaphore.acquire(); + return; + } + } +} + +int ThreadEngineBarrier::currentCount() +{ + return int(count); +} + +// releases a thread, unless this is the last thread. +// returns true if the thread was released. +bool ThreadEngineBarrier::releaseUnlessLast() +{ + forever { + int localCount = int(count); + if (qAbs(localCount) == 1) { + return false; + } else if (localCount < 0) { + if (count.testAndSetOrdered(localCount, localCount + 1)) + return true; + } else { + if (count.testAndSetOrdered(localCount, localCount - 1)) + return true; + } + } +} + ThreadEngineBase::ThreadEngineBase() :futureInterface(0), threadPool(QThreadPool::globalInstance()) { @@ -66,7 +141,7 @@ void ThreadEngineBase::startSingleThreaded() void ThreadEngineBase::startBlocking() { start(); - semaphore.acquire(); + barrier.acquire(); startThreads(); bool throttled = false; @@ -88,10 +163,10 @@ void ThreadEngineBase::startBlocking() #endif if (throttled == false) { - semaphore.release(); + barrier.release(); } - semaphore.wait(); + barrier.wait(); finish(); exceptionStore.throwPossibleException(); } @@ -101,6 +176,11 @@ void ThreadEngineBase::startThread() startThreadInternal(); } +void ThreadEngineBase::acquireBarrierSemaphore() +{ + barrier.acquire(); +} + bool ThreadEngineBase::isCanceled() { if (futureInterface) @@ -138,9 +218,9 @@ bool ThreadEngineBase::startThreadInternal() if (this->isCanceled()) return false; - semaphore.acquire(); + barrier.acquire(); if (!threadPool->tryStart(this)) { - semaphore.release(); + barrier.release(); return false; } return true; @@ -155,7 +235,7 @@ void ThreadEngineBase::startThreads() void ThreadEngineBase::threadExit() { const bool asynchronous = futureInterface != 0; - const int lastThread = (semaphore.release() == 0); + const int lastThread = (barrier.release() == 0); if (lastThread && asynchronous) this->asynchronousFinish(); @@ -166,7 +246,7 @@ void ThreadEngineBase::threadExit() // this function returns one. bool ThreadEngineBase::threadThrottleExit() { - return semaphore.releaseUnlessLast(); + return barrier.releaseUnlessLast(); } void ThreadEngineBase::run() // implements QRunnable. diff --git a/src/corelib/concurrent/qtconcurrentthreadengine.h b/src/corelib/concurrent/qtconcurrentthreadengine.h index 97f8f23..1e8bfe5 100644 --- a/src/corelib/concurrent/qtconcurrentthreadengine.h +++ b/src/corelib/concurrent/qtconcurrentthreadengine.h @@ -51,6 +51,8 @@ #include <QtCore/qdebug.h> #include <QtCore/qtconcurrentexception.h> #include <QtCore/qwaitcondition.h> +#include <QtCore/qatomic.h> +#include <QtCore/qsemaphore.h> QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -61,55 +63,29 @@ QT_MODULE(Core) namespace QtConcurrent { -// A Semaphore that can wait until all resources are returned. -class ThreadEngineSemaphore +// The ThreadEngineBarrier counts worker threads, and allows one +// thread to wait for all others to finish. Tested for its use in +// QtConcurrent, requires more testing for use as a general class. +class ThreadEngineBarrier { -public: - ThreadEngineSemaphore() - :count(0) { } - - void acquire() - { - QMutexLocker lock(&mutex); - ++count; - } - - int release() - { - QMutexLocker lock(&mutex); - if (--count == 0) - waitCondition.wakeAll(); - return count; - } - - // Wait until all resources are released. - void wait() - { - QMutexLocker lock(&mutex); - if (count != 0) - waitCondition.wait(&mutex); - } - - int currentCount() - { - return count; - } - - // releases a resource, unless this is the last resource. - // returns true if a resource was released. - bool releaseUnlessLast() - { - QMutexLocker lock(&mutex); - if (count == 1) - return false; - --count; - return true; - } - private: + // The thread count is maintained as an integer in the count atomic + // variable. The count can be either positive or negative - a negative + // count signals that a thread is waiting on the barrier. + + // BC note: inlined code from Qt < 4.6 will expect to find the QMutex + // and QAtomicInt here. ### Qt 5: remove. QMutex mutex; - int count; - QWaitCondition waitCondition; + QAtomicInt count; + + QSemaphore semaphore; +public: + ThreadEngineBarrier(); + void acquire(); + int release(); + void wait(); + int currentCount(); + bool releaseUnlessLast(); }; enum ThreadFunctionResult { ThrottleThread, ThreadFinished }; @@ -132,6 +108,7 @@ public: bool isProgressReportingEnabled(); void setProgressValue(int progress); void setProgressRange(int minimum, int maximum); + void acquireBarrierSemaphore(); protected: // The user overrides these: virtual void start() {} @@ -152,7 +129,7 @@ private: protected: QFutureInterfaceBase *futureInterface; QThreadPool *threadPool; - ThreadEngineSemaphore semaphore; + ThreadEngineBarrier barrier; QtConcurrent::internal::ExceptionStore exceptionStore; }; @@ -199,7 +176,7 @@ public: QFuture<T> future = QFuture<T>(futureInterfaceTyped()); start(); - semaphore.acquire(); + acquireBarrierSemaphore(); threadPool->start(this); return future; } @@ -261,9 +238,11 @@ protected: template <typename T> class ThreadEngineStarter : public ThreadEngineStarterBase<T> { + typedef ThreadEngineStarterBase<T> Base; + typedef ThreadEngine<T> TypedThreadEngine; public: - ThreadEngineStarter(ThreadEngine<T> *threadEngine) - :ThreadEngineStarterBase<T>(threadEngine) {} + ThreadEngineStarter(TypedThreadEngine *eng) + : Base(eng) { } T startBlocking() { diff --git a/src/corelib/concurrent/qthreadpool.cpp b/src/corelib/concurrent/qthreadpool.cpp index dcfd7e8..64309d8 100644 --- a/src/corelib/concurrent/qthreadpool.cpp +++ b/src/corelib/concurrent/qthreadpool.cpp @@ -222,6 +222,8 @@ void QThreadPoolPrivate::enqueueTask(QRunnable *runnable, int priority) int QThreadPoolPrivate::activeThreadCount() const { + // To improve scalability this function is called without holding + // the mutex lock -- keep it thread-safe. return (allThreads.count() - expiredThreads.count() - waitingThreads @@ -246,14 +248,14 @@ bool QThreadPoolPrivate::tooManyThreadsActive() const */ void QThreadPoolPrivate::startThread(QRunnable *runnable) { - QThreadPoolThread *thread = new QThreadPoolThread(this); - allThreads.insert(thread); + QScopedPointer <QThreadPoolThread> thread(new QThreadPoolThread(this)); + allThreads.insert(thread.data()); ++activeThreads; if (runnable->autoDelete()) ++runnable->ref; thread->runnable = runnable; - thread->start(); + thread.take()->start(); } /*! \internal @@ -362,6 +364,8 @@ void QThreadPoolPrivate::stealRunnable(QRunnable *runnable) \since 4.4 \threadsafe + \ingroup thread + QThreadPool manages and recyles individual QThread objects to help reduce thread creation costs in programs that use threads. Each Qt application has one global QThreadPool object, which can be accessed by calling @@ -402,7 +406,7 @@ void QThreadPoolPrivate::stealRunnable(QRunnable *runnable) Note that QThreadPool is a low-level class for managing threads, see QtConcurrent::run() or the other - \l {threads.html#qtconcurrent-intro}{Qt Concurrent} APIs for higher + \l {Concurrent Programming}{Qt Concurrent} APIs for higher level alternatives. \sa QRunnable @@ -481,6 +485,12 @@ bool QThreadPool::tryStart(QRunnable *runnable) return false; Q_D(QThreadPool); + + // To improve scalability perform a check on the thread count + // before locking the mutex. + if (d->allThreads.isEmpty() == false && d->activeThreadCount() >= d->maxThreadCount) + return false; + QMutexLocker locker(&d->mutex); return d->tryStart(runnable); } @@ -527,7 +537,6 @@ void QThreadPool::setExpiryTimeout(int expiryTimeout) int QThreadPool::maxThreadCount() const { Q_D(const QThreadPool); - QMutexLocker locker(&d->mutex); return d->maxThreadCount; } @@ -556,7 +565,6 @@ void QThreadPool::setMaxThreadCount(int maxThreadCount) int QThreadPool::activeThreadCount() const { Q_D(const QThreadPool); - QMutexLocker locker(&d->mutex); return d->activeThreadCount(); } |