/src/libreoffice/include/comphelper/threadpool.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | */ |
9 | | |
10 | | #ifndef INCLUDED_COMPHELPER_THREADPOOL_HXX |
11 | | #define INCLUDED_COMPHELPER_THREADPOOL_HXX |
12 | | |
13 | | #include <sal/config.h> |
14 | | #include <rtl/ref.hxx> |
15 | | #include <comphelper/comphelperdllapi.h> |
16 | | #include <mutex> |
17 | | #include <condition_variable> |
18 | | #include <cstddef> |
19 | | #include <vector> |
20 | | #include <memory> |
21 | | |
22 | | namespace comphelper |
23 | | { |
24 | | class ThreadTaskTag; |
25 | | |
26 | | class COMPHELPER_DLLPUBLIC ThreadTask |
27 | | { |
28 | | friend class ThreadPool; |
29 | | friend struct std::default_delete<ThreadTask>; |
30 | | std::shared_ptr<ThreadTaskTag> mpTag; |
31 | | |
32 | | /// execute this task |
33 | | void exec(); |
34 | | protected: |
35 | | /// override to get your task performed by the pool |
36 | | virtual void doWork() = 0; |
37 | | /// once pushed ThreadTasks are destroyed by the pool |
38 | 9.51k | virtual ~ThreadTask() {} |
39 | | public: |
40 | | ThreadTask(std::shared_ptr<ThreadTaskTag> pTag); |
41 | | }; |
42 | | |
43 | | /// A very basic thread-safe thread pool implementation |
44 | | class COMPHELPER_DLLPUBLIC ThreadPool final |
45 | | { |
46 | | public: |
47 | | /// returns a pointer to a shared pool with optimal thread |
48 | | /// count for the CPU |
49 | | static ThreadPool& getSharedOptimalPool(); |
50 | | |
51 | | static std::shared_ptr<ThreadTaskTag> createThreadTaskTag(); |
52 | | |
53 | | static bool isTaskTagDone(const std::shared_ptr<ThreadTaskTag>&); |
54 | | |
55 | | /// returns a configurable max-concurrency |
56 | | /// limit to avoid spawning an unnecessarily |
57 | | /// large number of threads on high-core boxes. |
58 | | /// MAX_CONCURRENCY env. var. controls the cap. |
59 | | static std::size_t getPreferredConcurrency(); |
60 | | |
61 | | ThreadPool( std::size_t nWorkers ); |
62 | | ~ThreadPool(); |
63 | | |
64 | | /// push a new task onto the work queue |
65 | | void pushTask( std::unique_ptr<ThreadTask> pTask); |
66 | | |
67 | | /** Wait until all queued tasks associated with the tag are completed |
68 | | @param bJoin - if set call joinThreadsIfIdle() at the end |
69 | | */ |
70 | | void waitUntilDone(const std::shared_ptr<ThreadTaskTag>&, bool bJoin = |
71 | | #if defined EMSCRIPTEN |
72 | | false |
73 | | #else |
74 | | true |
75 | | #endif |
76 | | ); |
77 | | |
78 | | /// join all threads if there are no tasks presently. |
79 | | /// return false if !isIdle() |
80 | | bool joinThreadsIfIdle(); |
81 | | |
82 | | /// return true if there are no queued or worked-on tasks |
83 | 168k | bool isIdle() const { return maTasks.empty() && mnBusyWorkers == 0; }; |
84 | | |
85 | | /// return the number of live worker threads |
86 | 0 | sal_Int32 getWorkerCount() const { return mnMaxWorkers; } |
87 | | |
88 | | /// wait until all work is completed, then join all threads |
89 | | void shutdown(); |
90 | | |
91 | | private: |
92 | | ThreadPool(const ThreadPool&) = delete; |
93 | | ThreadPool& operator=(const ThreadPool&) = delete; |
94 | | |
95 | | class ThreadWorker; |
96 | | friend class ThreadWorker; |
97 | | |
98 | | /** Pop a work task |
99 | | @param bWait - if set wait until task present or termination |
100 | | @return a new task to perform, or NULL if list empty or terminated |
101 | | */ |
102 | | std::unique_ptr<ThreadTask> popWorkLocked( std::unique_lock< std::mutex > & rGuard, bool bWait ); |
103 | | void shutdownLocked(std::unique_lock<std::mutex>&); |
104 | | void incBusyWorker(); |
105 | | void decBusyWorker(); |
106 | | |
107 | | /// signalled when all in-progress tasks are complete |
108 | | std::mutex maMutex; |
109 | | std::condition_variable maTasksChanged; |
110 | | bool mbTerminate; |
111 | | std::size_t const mnMaxWorkers; |
112 | | std::size_t mnBusyWorkers; |
113 | | std::vector< std::unique_ptr<ThreadTask> > maTasks; |
114 | | std::vector< rtl::Reference< ThreadWorker > > maWorkers; |
115 | | }; |
116 | | |
117 | | } // namespace comphelper |
118 | | |
119 | | #endif // INCLUDED_COMPHELPER_THREADPOOL_HXX |
120 | | |
121 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |