Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / comphelper / threadpool.hxx
blob84f9dc9284f6ce7cc0fa2c58f2e321b91adb571e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_COMPHELPER_THREADPOOL_HXX
11 #define INCLUDED_COMPHELPER_THREADPOOL_HXX
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>
22 namespace comphelper
24 class ThreadTaskTag;
26 class COMPHELPER_DLLPUBLIC ThreadTask
28 friend class ThreadPool;
29 friend struct std::default_delete<ThreadTask>;
30 std::shared_ptr<ThreadTaskTag> mpTag;
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 virtual ~ThreadTask() {}
39 public:
40 ThreadTask(std::shared_ptr<ThreadTaskTag> pTag);
43 /// A very basic thread-safe thread pool implementation
44 class COMPHELPER_DLLPUBLIC ThreadPool final
46 public:
47 /// returns a pointer to a shared pool with optimal thread
48 /// count for the CPU
49 static ThreadPool& getSharedOptimalPool();
51 static std::shared_ptr<ThreadTaskTag> createThreadTaskTag();
53 static bool isTaskTagDone(const std::shared_ptr<ThreadTaskTag>&);
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();
61 ThreadPool( std::size_t nWorkers );
62 ~ThreadPool();
64 /// push a new task onto the work queue
65 void pushTask( std::unique_ptr<ThreadTask> pTask);
67 /** Wait until all queued tasks associated with the tag are completed
68 @param bJoin - if set call joinThreadsIfIdle() at the end
70 void waitUntilDone(const std::shared_ptr<ThreadTaskTag>&, bool bJoin = true);
72 /// join all threads if there are no tasks presently.
73 void joinThreadsIfIdle();
75 /// return true if there are no queued or worked-on tasks
76 bool isIdle() const { return maTasks.empty() && mnBusyWorkers == 0; };
78 /// return the number of live worker threads
79 sal_Int32 getWorkerCount() const { return mnMaxWorkers; }
81 /// wait until all work is completed, then join all threads
82 void shutdown();
84 private:
85 ThreadPool(const ThreadPool&) = delete;
86 ThreadPool& operator=(const ThreadPool&) = delete;
88 class ThreadWorker;
89 friend class ThreadWorker;
91 /** Pop a work task
92 @param bWait - if set wait until task present or termination
93 @return a new task to perform, or NULL if list empty or terminated
95 std::unique_ptr<ThreadTask> popWorkLocked( std::unique_lock< std::mutex > & rGuard, bool bWait );
96 void shutdownLocked(std::unique_lock<std::mutex>&);
97 void incBusyWorker();
98 void decBusyWorker();
100 /// signalled when all in-progress tasks are complete
101 std::mutex maMutex;
102 std::condition_variable maTasksChanged;
103 bool mbTerminate;
104 std::size_t const mnMaxWorkers;
105 std::size_t mnBusyWorkers;
106 std::vector< std::unique_ptr<ThreadTask> > maTasks;
107 std::vector< rtl::Reference< ThreadWorker > > maWorkers;
110 } // namespace comphelper
112 #endif // INCLUDED_COMPHELPER_THREADPOOL_HXX
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */