LibreOffice Module comphelper (master)  1
threadpool.hxx
Go to the documentation of this file.
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>
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 
27 {
28 friend class ThreadPool;
29 friend struct std::default_delete<ThreadTask>;
30  std::shared_ptr<ThreadTaskTag> mpTag;
31 
33  void exec();
34 protected:
36  virtual void doWork() = 0;
38  virtual ~ThreadTask() {}
39 public:
40  ThreadTask(std::shared_ptr<ThreadTaskTag> pTag);
41 };
42 
45 {
46 public:
49  static ThreadPool& getSharedOptimalPool();
50 
51  static std::shared_ptr<ThreadTaskTag> createThreadTaskTag();
52 
53  static bool isTaskTagDone(const std::shared_ptr<ThreadTaskTag>&);
54 
59  static std::size_t getPreferredConcurrency();
60 
61  ThreadPool( std::size_t nWorkers );
62  ~ThreadPool();
63 
65  void pushTask( std::unique_ptr<ThreadTask> pTask);
66 
70  void waitUntilDone(const std::shared_ptr<ThreadTaskTag>&, bool bJoin = true);
71 
73  void joinThreadsIfIdle();
74 
76  bool isIdle() const { return maTasks.empty() && mnBusyWorkers == 0; };
77 
79  sal_Int32 getWorkerCount() const { return mnMaxWorkers; }
80 
82  void shutdown();
83 
84 private:
85  ThreadPool(const ThreadPool&) = delete;
86  ThreadPool& operator=(const ThreadPool&) = delete;
87 
88  class ThreadWorker;
89  friend class ThreadWorker;
90 
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();
99 
102  std::condition_variable maTasksChanged;
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;
108 };
109 
110 } // namespace comphelper
111 
112 #endif // INCLUDED_COMPHELPER_THREADPOOL_HXX
113 
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< std::unique_ptr< ThreadTask > > maTasks
Definition: threadpool.hxx:106
A very basic thread-safe thread pool implementation.
Definition: threadpool.hxx:44
sal_Int32 getWorkerCount() const
return the number of live worker threads
Definition: threadpool.hxx:79
std::mutex maMutex
signalled when all in-progress tasks are complete
Definition: threadpool.hxx:101
bool isIdle() const
return true if there are no queued or worked-on tasks
Definition: threadpool.hxx:76
std::condition_variable maTasksChanged
Definition: threadpool.hxx:102
std::vector< rtl::Reference< ThreadWorker > > maWorkers
Definition: threadpool.hxx:107
std::shared_ptr< ThreadTaskTag > mpTag
Definition: threadpool.hxx:30
std::mutex mutex
Definition: random.cxx:41
std::size_t mnBusyWorkers
Definition: threadpool.hxx:105
#define COMPHELPER_DLLPUBLIC
std::size_t const mnMaxWorkers
Definition: threadpool.hxx:104
virtual ~ThreadTask()
once pushed ThreadTasks are destroyed by the pool
Definition: threadpool.hxx:38