Kaydet (Commit) aa68c99d authored tarafından Michael Meeks's avatar Michael Meeks

tdf#104126 - comphelper thread-pool, use reliable std::condition_variable.

The existing osl::Condition is an API and reliability disaster area.

Change-Id: I3be84e1c6a83e58c43c40c9c8720790d923a6694
Reviewed-on: https://gerrit.libreoffice.org/31163Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMichael Meeks <michael.meeks@collabora.com>
Tested-by: 's avatarMichael Meeks <michael.meeks@collabora.com>
üst 0afbe8d5
...@@ -81,6 +81,7 @@ ...@@ -81,6 +81,7 @@
#include <toolkit/helper/vclunohelper.hxx> #include <toolkit/helper/vclunohelper.hxx>
#include <comphelper/configuration.hxx> #include <comphelper/configuration.hxx>
#include <comphelper/fileurl.hxx> #include <comphelper/fileurl.hxx>
#include <comphelper/threadpool.hxx>
#include <comphelper/processfactory.hxx> #include <comphelper/processfactory.hxx>
#include <comphelper/backupfilehelper.hxx> #include <comphelper/backupfilehelper.hxx>
#include <unotools/bootstrap.hxx> #include <unotools/bootstrap.hxx>
...@@ -1791,11 +1792,14 @@ int Desktop::doShutdown() ...@@ -1791,11 +1792,14 @@ int Desktop::doShutdown()
StarBASIC::DetachAllDocBasicItems(); StarBASIC::DetachAllDocBasicItems();
#endif #endif
} }
// be sure that path/language options gets destroyed before // be sure that path/language options gets destroyed before
// UCB is deinitialized // UCB is deinitialized
pExecGlobals->pLanguageOptions.reset( nullptr ); pExecGlobals->pLanguageOptions.reset( nullptr );
pExecGlobals->pPathOptions.reset( nullptr ); pExecGlobals->pPathOptions.reset( nullptr );
comphelper::ThreadPool::getSharedOptimalPool().shutdown();
bool bRR = pExecGlobals->bRestartRequested; bool bRR = pExecGlobals->bRestartRequested;
delete pExecGlobals; delete pExecGlobals;
pExecGlobals = nullptr; pExecGlobals = nullptr;
......
...@@ -11,11 +11,11 @@ ...@@ -11,11 +11,11 @@
#define INCLUDED_COMPHELPER_THREADPOOL_HXX #define INCLUDED_COMPHELPER_THREADPOOL_HXX
#include <sal/config.h> #include <sal/config.h>
#include <salhelper/thread.hxx>
#include <osl/mutex.hxx>
#include <osl/conditn.hxx>
#include <rtl/ref.hxx> #include <rtl/ref.hxx>
#include <comphelper/comphelperdllapi.h> #include <comphelper/comphelperdllapi.h>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <vector> #include <vector>
#include <memory> #include <memory>
...@@ -28,14 +28,19 @@ class COMPHELPER_DLLPUBLIC ThreadTask ...@@ -28,14 +28,19 @@ class COMPHELPER_DLLPUBLIC ThreadTask
{ {
friend class ThreadPool; friend class ThreadPool;
std::shared_ptr<ThreadTaskTag> mpTag; std::shared_ptr<ThreadTaskTag> mpTag;
/// execute and delete this task
void execAndDelete();
protected:
/// override to get your task performed by the pool
virtual void doWork() = 0;
/// once pushed ThreadTasks are destroyed by the pool
virtual ~ThreadTask() {}
public: public:
ThreadTask(const std::shared_ptr<ThreadTaskTag>& pTag); ThreadTask(const std::shared_ptr<ThreadTaskTag>& pTag);
virtual ~ThreadTask() {}
virtual void doWork() = 0;
const std::shared_ptr<ThreadTaskTag>& getTag() { return mpTag; }
}; };
/// A very basic thread pool implementation /// A very basic thread-safe thread pool implementation
class COMPHELPER_DLLPUBLIC ThreadPool final class COMPHELPER_DLLPUBLIC ThreadPool final
{ {
public: public:
...@@ -50,7 +55,7 @@ public: ...@@ -50,7 +55,7 @@ public:
/// returns a configurable max-concurrency /// returns a configurable max-concurrency
/// limit to avoid spawning an unnecessarily /// limit to avoid spawning an unnecessarily
/// large number of threads on high-core boxes. /// large number of threads on high-core boxes.
/// MAX_CONCURRENCY envar controls the cap. /// MAX_CONCURRENCY env. var. controls the cap.
static sal_Int32 getPreferredConcurrency(); static sal_Int32 getPreferredConcurrency();
ThreadPool( sal_Int32 nWorkers ); ThreadPool( sal_Int32 nWorkers );
...@@ -65,6 +70,9 @@ public: ...@@ -65,6 +70,9 @@ public:
/// return the number of live worker threads /// return the number of live worker threads
sal_Int32 getWorkerCount() const { return maWorkers.size(); } sal_Int32 getWorkerCount() const { return maWorkers.size(); }
/// wait until all work is completed, then join all threads
void shutdown();
private: private:
ThreadPool(const ThreadPool&) = delete; ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete; ThreadPool& operator=(const ThreadPool&) = delete;
...@@ -72,20 +80,21 @@ private: ...@@ -72,20 +80,21 @@ private:
class ThreadWorker; class ThreadWorker;
friend class ThreadWorker; friend class ThreadWorker;
/// wait until all work is completed, then join all threads /** Pop a work task
void waitAndCleanupWorkers(); @param bWait - if set wait until task present or termination
@return a new task to perform, or NULL if list empty or terminated
ThreadTask *popWork(); */
void startWork(); ThreadTask *popWorkLocked( std::unique_lock< std::mutex > & rGuard, bool bWait );
void stopWork(); void startWorkLocked();
void stopWorkLocked();
osl::Mutex maGuard;
sal_Int32 mnThreadsWorking;
/// signalled when all in-progress tasks are complete /// signalled when all in-progress tasks are complete
osl::Condition maTasksComplete; std::mutex maMutex;
bool mbTerminate; std::condition_variable maTasksChanged;
std::vector< rtl::Reference< ThreadWorker > > maWorkers; sal_Int32 mnThreadsWorking;
bool mbTerminate;
std::vector< ThreadTask * > maTasks; std::vector< ThreadTask * > maTasks;
std::vector< rtl::Reference< ThreadWorker > > maWorkers;
}; };
} // namespace comphelper } // namespace comphelper
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <osl/diagnose.h> #include <osl/diagnose.h>
#include <osl/time.h> #include <osl/time.h>
#include <osl/thread.hxx>
#include <PackageConstants.hxx> #include <PackageConstants.hxx>
#include <ZipEntry.hxx> #include <ZipEntry.hxx>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment