diff options
author | Morten Sørvig <msorvig@trolltech.com> | 2009-06-24 13:58:33 (GMT) |
---|---|---|
committer | Morten Sørvig <msorvig@trolltech.com> | 2009-06-24 13:58:33 (GMT) |
commit | fa47ad238fe7ee01f3c9f73c498c6290149ca21d (patch) | |
tree | 3daeb5bb6ebe72f706f67dfa1057eb198729e19c /src/corelib/concurrent/qtconcurrentthreadengine.h | |
parent | 055c9c41b3f659cbbf758d15e8350c5dd2b5faa1 (diff) | |
download | Qt-fa47ad238fe7ee01f3c9f73c498c6290149ca21d.zip Qt-fa47ad238fe7ee01f3c9f73c498c6290149ca21d.tar.gz Qt-fa47ad238fe7ee01f3c9f73c498c6290149ca21d.tar.bz2 |
Rename ThreadEngineSemaphore -> ThreadEngineBarrier
Also move the implementation to the .cpp file.
Diffstat (limited to 'src/corelib/concurrent/qtconcurrentthreadengine.h')
-rw-r--r-- | src/corelib/concurrent/qtconcurrentthreadengine.h | 89 |
1 files changed, 11 insertions, 78 deletions
diff --git a/src/corelib/concurrent/qtconcurrentthreadengine.h b/src/corelib/concurrent/qtconcurrentthreadengine.h index 21a3a28..286c2b8 100644 --- a/src/corelib/concurrent/qtconcurrentthreadengine.h +++ b/src/corelib/concurrent/qtconcurrentthreadengine.h @@ -63,91 +63,24 @@ QT_MODULE(Core) namespace QtConcurrent { -// The ThreadEngineSemaphore counts worker threads, and allows one +// 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 ThreadEngineSemaphore +class ThreadEngineBarrier { 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 semaphore. + // count signals that a thread is waiting on the barrier. QAtomicInt count; QSemaphore semaphore; public: - ThreadEngineSemaphore() - :count(0) { } - - void 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 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 wait() - { - forever { - int localCount = int(count); - if (localCount == 0) - return; - - if (count.testAndSetOrdered(localCount, -localCount)) { - semaphore.acquire(); - return; - } - } - } - - int currentCount() - { - return int(count); - } - - // releases a thread, unless this is the last thread. - // returns true if the thread was released. - bool 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; - } - } - } + ThreadEngineBarrier(); + void acquire(); + int release(); + void wait(); + int currentCount(); + bool releaseUnlessLast(); }; enum ThreadFunctionResult { ThrottleThread, ThreadFinished }; @@ -190,7 +123,7 @@ private: protected: QFutureInterfaceBase *futureInterface; QThreadPool *threadPool; - ThreadEngineSemaphore semaphore; + ThreadEngineBarrier barrier; QtConcurrent::internal::ExceptionStore exceptionStore; }; @@ -237,7 +170,7 @@ public: QFuture<T> future = QFuture<T>(futureInterfaceTyped()); start(); - semaphore.acquire(); + barrier.acquire(); threadPool->start(this); return future; } |