2013-11-27 18:11:34 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2014-10-30 18:37:42 +00:00
|
|
|
#ifndef INCLUDED_COMPHELPER_THREADPOOL_HXX
|
|
|
|
#define INCLUDED_COMPHELPER_THREADPOOL_HXX
|
2013-11-27 18:11:34 +00:00
|
|
|
|
|
|
|
#include <sal/config.h>
|
|
|
|
#include <rtl/ref.hxx>
|
2014-10-30 18:37:42 +00:00
|
|
|
#include <comphelper/comphelperdllapi.h>
|
2016-12-01 11:14:24 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2016-07-08 14:29:53 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
2013-11-27 18:11:34 +00:00
|
|
|
|
2014-10-30 18:37:42 +00:00
|
|
|
namespace comphelper
|
|
|
|
{
|
2016-07-08 14:29:53 +02:00
|
|
|
class ThreadTaskTag;
|
2014-10-30 18:37:42 +00:00
|
|
|
|
|
|
|
class COMPHELPER_DLLPUBLIC ThreadTask
|
2013-11-27 18:11:34 +00:00
|
|
|
{
|
2016-07-08 14:29:53 +02:00
|
|
|
friend class ThreadPool;
|
2018-07-03 16:19:55 +02:00
|
|
|
friend struct std::default_delete<ThreadTask>;
|
2016-07-08 14:29:53 +02:00
|
|
|
std::shared_ptr<ThreadTaskTag> mpTag;
|
2016-12-01 11:14:24 +00:00
|
|
|
|
2018-07-03 16:19:55 +02:00
|
|
|
/// execute this task
|
|
|
|
void exec();
|
2016-12-01 11:14:24 +00:00
|
|
|
protected:
|
|
|
|
/// override to get your task performed by the pool
|
|
|
|
virtual void doWork() = 0;
|
|
|
|
/// once pushed ThreadTasks are destroyed by the pool
|
|
|
|
virtual ~ThreadTask() {}
|
2013-11-27 18:11:34 +00:00
|
|
|
public:
|
2016-07-08 14:29:53 +02:00
|
|
|
ThreadTask(const std::shared_ptr<ThreadTaskTag>& pTag);
|
2013-11-27 18:11:34 +00:00
|
|
|
};
|
|
|
|
|
2016-12-01 11:14:24 +00:00
|
|
|
/// A very basic thread-safe thread pool implementation
|
2016-11-08 08:13:11 +02:00
|
|
|
class COMPHELPER_DLLPUBLIC ThreadPool final
|
2013-11-27 18:11:34 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-30 18:37:42 +00:00
|
|
|
/// returns a pointer to a shared pool with optimal thread
|
|
|
|
/// count for the CPU
|
|
|
|
static ThreadPool& getSharedOptimalPool();
|
|
|
|
|
2016-07-08 14:29:53 +02:00
|
|
|
static std::shared_ptr<ThreadTaskTag> createThreadTaskTag();
|
|
|
|
|
|
|
|
static bool isTaskTagDone(const std::shared_ptr<ThreadTaskTag>&);
|
|
|
|
|
2016-06-14 07:19:20 -04:00
|
|
|
/// returns a configurable max-concurrency
|
|
|
|
/// limit to avoid spawning an unnecessarily
|
|
|
|
/// large number of threads on high-core boxes.
|
2016-12-01 11:14:24 +00:00
|
|
|
/// MAX_CONCURRENCY env. var. controls the cap.
|
2016-06-14 07:19:20 -04:00
|
|
|
static sal_Int32 getPreferredConcurrency();
|
|
|
|
|
2016-07-08 14:29:53 +02:00
|
|
|
ThreadPool( sal_Int32 nWorkers );
|
2016-11-08 08:13:11 +02:00
|
|
|
~ThreadPool();
|
2014-10-30 18:37:42 +00:00
|
|
|
|
2014-10-30 21:58:36 +00:00
|
|
|
/// push a new task onto the work queue
|
2018-07-03 16:19:55 +02:00
|
|
|
void pushTask( std::unique_ptr<ThreadTask> pTask);
|
2014-10-30 21:58:36 +00:00
|
|
|
|
2019-03-20 20:07:58 +05:30
|
|
|
/** Wait until all queued tasks associated with the tag are completed
|
|
|
|
@param bJoinAll - if set it joins all threads at the end if no other tasks from other tags.
|
|
|
|
*/
|
|
|
|
void waitUntilDone(const std::shared_ptr<ThreadTaskTag>&, bool bJoinAll = true);
|
|
|
|
|
|
|
|
/// join all threads if there are no tasks presently.
|
|
|
|
void joinAll();
|
2013-11-27 18:11:34 +00:00
|
|
|
|
2014-10-16 08:46:11 -03:00
|
|
|
/// return the number of live worker threads
|
2017-03-21 15:18:01 +01:00
|
|
|
sal_Int32 getWorkerCount() const { return mnWorkers; }
|
2014-10-16 08:46:11 -03:00
|
|
|
|
2016-12-01 11:14:24 +00:00
|
|
|
/// wait until all work is completed, then join all threads
|
|
|
|
void shutdown();
|
|
|
|
|
2013-11-27 18:11:34 +00:00
|
|
|
private:
|
2015-10-12 15:25:41 +02:00
|
|
|
ThreadPool(const ThreadPool&) = delete;
|
|
|
|
ThreadPool& operator=(const ThreadPool&) = delete;
|
2015-02-16 13:03:53 +00:00
|
|
|
|
2013-11-27 18:11:34 +00:00
|
|
|
class ThreadWorker;
|
|
|
|
friend class ThreadWorker;
|
|
|
|
|
2016-12-01 11:14:24 +00:00
|
|
|
/** Pop a work task
|
|
|
|
@param bWait - if set wait until task present or termination
|
|
|
|
@return a new task to perform, or NULL if list empty or terminated
|
|
|
|
*/
|
2018-07-03 16:19:55 +02:00
|
|
|
std::unique_ptr<ThreadTask> popWorkLocked( std::unique_lock< std::mutex > & rGuard, bool bWait );
|
2017-03-21 15:18:01 +01:00
|
|
|
void shutdownLocked(std::unique_lock<std::mutex>&);
|
2013-11-27 18:11:34 +00:00
|
|
|
|
2014-10-31 12:32:12 +00:00
|
|
|
/// signalled when all in-progress tasks are complete
|
2016-12-01 11:14:24 +00:00
|
|
|
std::mutex maMutex;
|
|
|
|
std::condition_variable maTasksChanged;
|
|
|
|
bool mbTerminate;
|
2018-07-19 16:28:37 +02:00
|
|
|
std::size_t const mnWorkers;
|
2018-07-03 16:19:55 +02:00
|
|
|
std::vector< std::unique_ptr<ThreadTask> > maTasks;
|
2016-12-01 11:14:24 +00:00
|
|
|
std::vector< rtl::Reference< ThreadWorker > > maWorkers;
|
2013-11-27 18:11:34 +00:00
|
|
|
};
|
|
|
|
|
2014-10-30 18:37:42 +00:00
|
|
|
} // namespace comphelper
|
|
|
|
|
|
|
|
#endif // INCLUDED_COMPHELPER_THREADPOOL_HXX
|
2013-11-27 18:11:34 +00:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|