LibreOffice Module comphelper (master) 1
|
A very basic thread-safe thread pool implementation. More...
#include <threadpool.hxx>
Classes | |
class | ThreadWorker |
Public Member Functions | |
ThreadPool (std::size_t nWorkers) | |
~ThreadPool () | |
void | pushTask (std::unique_ptr< ThreadTask > pTask) |
push a new task onto the work queue More... | |
void | waitUntilDone (const std::shared_ptr< ThreadTaskTag > &, bool bJoin=true) |
Wait until all queued tasks associated with the tag are completed. More... | |
void | joinThreadsIfIdle () |
join all threads if there are no tasks presently. More... | |
bool | isIdle () const |
return true if there are no queued or worked-on tasks More... | |
sal_Int32 | getWorkerCount () const |
return the number of live worker threads More... | |
void | shutdown () |
wait until all work is completed, then join all threads More... | |
Static Public Member Functions | |
static ThreadPool & | getSharedOptimalPool () |
returns a pointer to a shared pool with optimal thread count for the CPU More... | |
static std::shared_ptr< ThreadTaskTag > | createThreadTaskTag () |
static bool | isTaskTagDone (const std::shared_ptr< ThreadTaskTag > &) |
static std::size_t | getPreferredConcurrency () |
returns a configurable max-concurrency limit to avoid spawning an unnecessarily large number of threads on high-core boxes. More... | |
Private Member Functions | |
ThreadPool (const ThreadPool &)=delete | |
ThreadPool & | operator= (const ThreadPool &)=delete |
std::unique_ptr< ThreadTask > | popWorkLocked (std::unique_lock< std::mutex > &rGuard, bool bWait) |
Pop a work task. More... | |
void | shutdownLocked (std::unique_lock< std::mutex > &) |
void | incBusyWorker () |
void | decBusyWorker () |
Private Attributes | |
std::mutex | maMutex |
signalled when all in-progress tasks are complete More... | |
std::condition_variable | maTasksChanged |
bool | mbTerminate |
std::size_t const | mnMaxWorkers |
std::size_t | mnBusyWorkers |
std::vector< std::unique_ptr< ThreadTask > > | maTasks |
std::vector< rtl::Reference< ThreadWorker > > | maWorkers |
Friends | |
class | ThreadWorker |
A very basic thread-safe thread pool implementation.
Definition at line 44 of file threadpool.hxx.
comphelper::ThreadPool::ThreadPool | ( | std::size_t | nWorkers | ) |
Definition at line 96 of file threadpool.cxx.
comphelper::ThreadPool::~ThreadPool | ( | ) |
Definition at line 103 of file threadpool.cxx.
References maTasks, mbTerminate, and mnBusyWorkers.
|
privatedelete |
|
static |
Definition at line 301 of file threadpool.cxx.
|
private |
Definition at line 258 of file threadpool.cxx.
References mnBusyWorkers.
Referenced by comphelper::ThreadPool::ThreadWorker::execute().
|
static |
returns a configurable max-concurrency limit to avoid spawning an unnecessarily large number of threads on high-core boxes.
MAX_CONCURRENCY env. var. controls the cap.
Definition at line 134 of file threadpool.cxx.
|
static |
returns a pointer to a shared pool with optimal thread count for the CPU
Definition at line 129 of file threadpool.cxx.
|
inline |
return the number of live worker threads
Definition at line 79 of file threadpool.hxx.
|
private |
Definition at line 253 of file threadpool.cxx.
References mnBusyWorkers.
Referenced by comphelper::ThreadPool::ThreadWorker::execute().
|
inline |
return true if there are no queued or worked-on tasks
Definition at line 76 of file threadpool.hxx.
Referenced by joinThreadsIfIdle().
|
static |
Definition at line 306 of file threadpool.cxx.
void comphelper::ThreadPool::joinThreadsIfIdle | ( | ) |
join all threads if there are no tasks presently.
Definition at line 292 of file threadpool.cxx.
References isIdle(), maMutex, and shutdownLocked().
Referenced by waitUntilDone().
|
privatedelete |
|
private |
Pop a work task.
bWait | - if set wait until task present or termination |
Definition at line 233 of file threadpool.cxx.
References maTasks, maTasksChanged, and mbTerminate.
Referenced by comphelper::ThreadPool::ThreadWorker::execute(), shutdownLocked(), and waitUntilDone().
void comphelper::ThreadPool::pushTask | ( | std::unique_ptr< ThreadTask > | pTask | ) |
push a new task onto the work queue
Definition at line 214 of file threadpool.cxx.
References maMutex, maTasks, maTasksChanged, maWorkers, mbTerminate, mnBusyWorkers, mnMaxWorkers, and ThreadWorker.
void comphelper::ThreadPool::shutdown | ( | ) |
wait until all work is completed, then join all threads
Definition at line 157 of file threadpool.cxx.
References maMutex, and shutdownLocked().
|
private |
Definition at line 166 of file threadpool.cxx.
References find(), maTasks, maTasksChanged, maWorkers, mbTerminate, and popWorkLocked().
Referenced by joinThreadsIfIdle(), and shutdown().
void comphelper::ThreadPool::waitUntilDone | ( | const std::shared_ptr< ThreadTaskTag > & | rTag, |
bool | bJoin = true |
||
) |
Wait until all queued tasks associated with the tag are completed.
bJoin | - if set call joinThreadsIfIdle() at the end |
Definition at line 264 of file threadpool.cxx.
References comphelper::gbIsWorkerThread, joinThreadsIfIdle(), maMutex, maWorkers, and popWorkLocked().
|
friend |
Definition at line 89 of file threadpool.hxx.
Referenced by pushTask().
|
private |
signalled when all in-progress tasks are complete
Definition at line 101 of file threadpool.hxx.
Referenced by comphelper::ThreadPool::ThreadWorker::execute(), joinThreadsIfIdle(), pushTask(), shutdown(), and waitUntilDone().
|
private |
Definition at line 106 of file threadpool.hxx.
Referenced by popWorkLocked(), pushTask(), shutdownLocked(), and ~ThreadPool().
|
private |
Definition at line 102 of file threadpool.hxx.
Referenced by popWorkLocked(), pushTask(), and shutdownLocked().
|
private |
Definition at line 107 of file threadpool.hxx.
Referenced by pushTask(), shutdownLocked(), and waitUntilDone().
|
private |
Definition at line 103 of file threadpool.hxx.
Referenced by comphelper::ThreadPool::ThreadWorker::execute(), popWorkLocked(), pushTask(), shutdownLocked(), and ~ThreadPool().
|
private |
Definition at line 105 of file threadpool.hxx.
Referenced by decBusyWorker(), incBusyWorker(), pushTask(), and ~ThreadPool().
|
private |
Definition at line 104 of file threadpool.hxx.
Referenced by pushTask().