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 <vector>
19 #include <memory>
20 
21 namespace comphelper
22 {
23 class ThreadTaskTag;
24 
26 {
27 friend class ThreadPool;
28 friend struct std::default_delete<ThreadTask>;
29  std::shared_ptr<ThreadTaskTag> mpTag;
30 
32  void exec();
33 protected:
35  virtual void doWork() = 0;
37  virtual ~ThreadTask() {}
38 public:
39  ThreadTask(const std::shared_ptr<ThreadTaskTag>& pTag);
40 };
41 
44 {
45 public:
48  static ThreadPool& getSharedOptimalPool();
49 
50  static std::shared_ptr<ThreadTaskTag> createThreadTaskTag();
51 
52  static bool isTaskTagDone(const std::shared_ptr<ThreadTaskTag>&);
53 
58  static sal_Int32 getPreferredConcurrency();
59 
60  ThreadPool( sal_Int32 nWorkers );
61  ~ThreadPool();
62 
64  void pushTask( std::unique_ptr<ThreadTask> pTask);
65 
69  void waitUntilDone(const std::shared_ptr<ThreadTaskTag>&, bool bJoinAll = true);
70 
72  void joinAll();
73 
75  sal_Int32 getWorkerCount() const { return mnWorkers; }
76 
78  void shutdown();
79 
80 private:
81  ThreadPool(const ThreadPool&) = delete;
82  ThreadPool& operator=(const ThreadPool&) = delete;
83 
84  class ThreadWorker;
85  friend class ThreadWorker;
86 
91  std::unique_ptr<ThreadTask> popWorkLocked( std::unique_lock< std::mutex > & rGuard, bool bWait );
92  void shutdownLocked(std::unique_lock<std::mutex>&);
93 
95  std::mutex maMutex;
96  std::condition_variable maTasksChanged;
98  std::size_t const mnWorkers;
99  std::vector< std::unique_ptr<ThreadTask> > maTasks;
100  std::vector< rtl::Reference< ThreadWorker > > maWorkers;
101 };
102 
103 } // namespace comphelper
104 
105 #endif // INCLUDED_COMPHELPER_THREADPOOL_HXX
106 
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< std::unique_ptr< ThreadTask > > maTasks
Definition: threadpool.hxx:99
A very basic thread-safe thread pool implementation.
Definition: threadpool.hxx:43
sal_Int32 getWorkerCount() const
return the number of live worker threads
Definition: threadpool.hxx:75
std::mutex maMutex
signalled when all in-progress tasks are complete
Definition: threadpool.hxx:95
std::condition_variable maTasksChanged
Definition: threadpool.hxx:96
std::size_t const mnWorkers
Definition: threadpool.hxx:98
std::vector< rtl::Reference< ThreadWorker > > maWorkers
Definition: threadpool.hxx:100
std::shared_ptr< ThreadTaskTag > mpTag
Definition: threadpool.hxx:29
#define COMPHELPER_DLLPUBLIC
virtual ~ThreadTask()
once pushed ThreadTasks are destroyed by the pool
Definition: threadpool.hxx:37