From 68f912e896b14546f17ef8bd090f08229842519f Mon Sep 17 00:00:00 2001 From: Michael Meeks Date: Fri, 31 Oct 2014 12:32:12 +0000 Subject: thread-pool: fix waiting until all tasks are complete. Change-Id: Iaf5a3bf28879f229a223a8760fd878f96958a53c --- comphelper/source/misc/threadpool.cxx | 40 +++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'comphelper') diff --git a/comphelper/source/misc/threadpool.cxx b/comphelper/source/misc/threadpool.cxx index 236a314019f8..3717ffbd006c 100644 --- a/comphelper/source/misc/threadpool.cxx +++ b/comphelper/source/misc/threadpool.cxx @@ -20,10 +20,15 @@ class ThreadPool::ThreadWorker : public salhelper::Thread { ThreadPool *mpPool; osl::Condition maNewWork; + bool mbWorking; public: + ThreadWorker( ThreadPool *pPool ) : salhelper::Thread("thread-pool"), - mpPool( pPool ) {} + mpPool( pPool ), + mbWorking( false ) + { + } virtual void execute() SAL_OVERRIDE { @@ -45,6 +50,9 @@ public: while( !pRet ) { + if (mbWorking) + mpPool->stopWork(); + mbWorking = false; maNewWork.reset(); if( mpPool->mbTerminate ) @@ -59,6 +67,13 @@ public: pRet = mpPool->popWork(); } + if (pRet) + { + if (!mbWorking) + mpPool->startWork(); + mbWorking = true; + } + return pRet; } @@ -78,12 +93,13 @@ public: }; ThreadPool::ThreadPool( sal_Int32 nWorkers ) : + mnThreadsWorking( 0 ), mbTerminate( false ) { for( sal_Int32 i = 0; i < nWorkers; i++ ) maWorkers.push_back( new ThreadWorker( this ) ); - maTasksEmpty.reset(); + maTasksComplete.reset(); osl::MutexGuard aGuard( maGuard ); for( size_t i = 0; i < maWorkers.size(); i++ ) @@ -136,10 +152,11 @@ void ThreadPool::pushTask( ThreadTask *pTask ) { osl::MutexGuard aGuard( maGuard ); maTasks.insert( maTasks.begin(), pTask ); + // horrible beyond belief: for( size_t i = 0; i < maWorkers.size(); i++ ) maWorkers[ i ]->signalNewWork(); - maTasksEmpty.reset(); + maTasksComplete.reset(); } ThreadTask *ThreadPool::popWork() @@ -151,8 +168,19 @@ ThreadTask *ThreadPool::popWork() return pTask; } else - maTasksEmpty.set(); - return NULL; + return NULL; +} + +void ThreadPool::startWork() +{ + mnThreadsWorking++; +} + +void ThreadPool::stopWork() +{ + assert( mnThreadsWorking > 0 ); + if ( --mnThreadsWorking == 0 ) + maTasksComplete.set(); } void ThreadPool::waitUntilEmpty() @@ -171,7 +199,7 @@ void ThreadPool::waitUntilEmpty() else { aGuard.clear(); - maTasksEmpty.wait(); + maTasksComplete.wait(); aGuard.reset(); } assert( maTasks.empty() ); -- cgit