Ocean
ThreadPool.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_THREAD_POOL_H
9 #define META_OCEAN_BASE_THREAD_POOL_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Lock.h"
13 #include "ocean/base/Signal.h"
14 #include "ocean/base/Singleton.h"
15 #include "ocean/base/Thread.h"
16 
17 #include <functional>
18 #include <queue>
19 
20 namespace Ocean
21 {
22 
23 /**
24  * This class implements a pool holding re-usable thread objects for individual use.
25  * This class is not a singleton, use ThreadPoolSingleton in case a singleton is sufficient.
26  * @see ThreadPoolSingleton, Scheduler, TaskQueue.
27  * @ingroup base
28  */
29 class OCEAN_BASE_EXPORT ThreadPool : protected Thread
30 {
31  public:
32 
33  /**
34  * Definition of a caller function without return parameter.
35  */
36  using Function = std::function<void()>;
37 
38  protected:
39 
40  /**
41  * This class implements one thread of this thread pool.
42  */
43  class OCEAN_BASE_EXPORT PoolThread : protected Thread
44  {
45  public:
46 
47  /**
48  * Creates a new thread object.
49  * @param name The thread name
50  */
51  explicit PoolThread(const std::string& name);
52 
53  /**
54  * Destructs a thread object.
55  */
56  ~PoolThread() override;
57 
58  /**
59  * Returns whether this thread is currently busy.
60  * @return True, if so
61  */
62  inline bool isBusy() const;
63 
64  /**
65  * Sets the thread function.
66  * @param function The thread function that will be invoked in this thread
67  */
68  void invoke(Function&& function);
69 
70  protected:
71 
72  /**
73  * Thread run function.
74  */
75  void threadRun() override;
76 
77  protected:
78 
79  /// Internal signal handling the internal thread execution.
81 
82  /// The function that is invoked in this thread.
84 
85  /// Thread lock.
86  mutable Lock lock_;
87  };
88 
89  /**
90  * Definition of a unique pointer holding a pool thread.
91  */
92  using UniquePoolThread = std::unique_ptr<PoolThread>;
93 
94  /**
95  * Definition of a vector holding pool threads.
96  */
97  using PoolThreads = std::vector<UniquePoolThread>;
98 
99  /**
100  * Definition of a queue holding functions.
101  */
102  using FunctionQueue = std::queue<Function>;
103 
104  public:
105 
106  /**
107  * Creates a new thread pool object.
108  */
109  ThreadPool() = default;
110 
111  /**
112  * Destructs a thread pool.
113  */
114  ~ThreadPool() override;
115 
116  /**
117  * Returns the maximal number of thread objects allowed inside this pool.
118  * @return Maximal thread capacity, with range [1, infinity); 2 by default
119  */
120  inline size_t capacity() const;
121 
122  /**
123  * Returns the number of currently busy/active threads in this pool.
124  * @return Thread number, with range [0, infinity)
125  */
126  inline size_t size() const;
127 
128  /**
129  * Defines the maximal number of thread objects existing concurrently.
130  * @param capacity Maximal number of thread objects to be allowed inside this pool, with range (1, infinity)
131  * @return True, if succeeded
132  */
133  bool setCapacity(const size_t capacity);
134 
135  /**
136  * Invokes a function on one of the free threads of this pool.
137  * @param function The function that will be invoked by a free thread
138  * @return True, if the function will be invoked by a free thread, otherwise no free thread was available
139  */
140  bool invoke(Function&& function);
141 
142  /**
143  * Returns the number of pending functions.
144  * @return The number of pending functions which have not been executed yet as the pool is at capacity, with range [0, infinity)
145  */
146  inline size_t pending() const;
147 
148  protected:
149 
150  /**
151  * Thread run function.
152  */
153  void threadRun() override;
154 
155  protected:
156 
157  /// The busy pool threads.
159 
160  /// The idle pool threads.
162 
163  /// The pending functions.
165 
166  /// Maximal pool capacity, with range [1, infinity)
167  size_t capacity_ = 2;
168 
169  /// The counter for pool thread ids.
170  size_t poolThreadIdCounter_ = 0;
171 
172  /// Pool lock.
173  mutable Lock lock_;
174 };
175 
176 /**
177  * This class wraps the ThreadPool into a singleton for global usage.
178  * Use the standard ThreadPool in case a singleton is not sufficient.
179  * @see ThreadPool.
180  * @ingroup base
181  */
183  public Singleton<ThreadPoolSingleton>,
184  public ThreadPool
185 {
186  friend class Singleton<ThreadPoolSingleton>;
187 
188  protected:
189 
190  /**
191  * Protected default constructor.
192  */
194 };
195 
197 {
198  const ScopedLock scopedLock(lock_);
199 
200  return function_ != nullptr;
201 }
202 
203 inline size_t ThreadPool::capacity() const
204 {
205  const ScopedLock scopedLock(lock_);
206  return capacity_;
207 }
208 
209 inline size_t ThreadPool::size() const
210 {
211  const ScopedLock scopedLock(lock_);
212 
213  return busyPoolThreads_.size();
214 }
215 
216 inline size_t ThreadPool::pending() const
217 {
218  const ScopedLock scopedLock(lock_);
219 
220  return pendingFunctions_.size();
221 }
222 
223 }
224 
225 #endif // META_OCEAN_BASE_THREAD_POOL_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class implements a signal.
Definition: Signal.h:31
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a thread.
Definition: Thread.h:115
This class implements one thread of this thread pool.
Definition: ThreadPool.h:44
void invoke(Function &&function)
Sets the thread function.
PoolThread(const std::string &name)
Creates a new thread object.
Lock lock_
Thread lock.
Definition: ThreadPool.h:86
void threadRun() override
Thread run function.
bool isBusy() const
Returns whether this thread is currently busy.
Definition: ThreadPool.h:196
Signal signal_
Internal signal handling the internal thread execution.
Definition: ThreadPool.h:80
Function function_
The function that is invoked in this thread.
Definition: ThreadPool.h:83
~PoolThread() override
Destructs a thread object.
This class implements a pool holding re-usable thread objects for individual use.
Definition: ThreadPool.h:30
size_t capacity_
Maximal pool capacity, with range [1, infinity)
Definition: ThreadPool.h:167
size_t size() const
Returns the number of currently busy/active threads in this pool.
Definition: ThreadPool.h:209
FunctionQueue pendingFunctions_
The pending functions.
Definition: ThreadPool.h:164
size_t capacity() const
Returns the maximal number of thread objects allowed inside this pool.
Definition: ThreadPool.h:203
ThreadPool()=default
Creates a new thread pool object.
PoolThreads idlePoolThreads_
The idle pool threads.
Definition: ThreadPool.h:161
Lock lock_
Pool lock.
Definition: ThreadPool.h:173
std::function< void()> Function
Definition of a caller function without return parameter.
Definition: ThreadPool.h:36
std::queue< Function > FunctionQueue
Definition of a queue holding functions.
Definition: ThreadPool.h:102
bool invoke(Function &&function)
Invokes a function on one of the free threads of this pool.
bool setCapacity(const size_t capacity)
Defines the maximal number of thread objects existing concurrently.
~ThreadPool() override
Destructs a thread pool.
void threadRun() override
Thread run function.
PoolThreads busyPoolThreads_
The busy pool threads.
Definition: ThreadPool.h:158
size_t pending() const
Returns the number of pending functions.
Definition: ThreadPool.h:216
std::vector< UniquePoolThread > PoolThreads
Definition of a vector holding pool threads.
Definition: ThreadPool.h:97
std::unique_ptr< PoolThread > UniquePoolThread
Definition of a unique pointer holding a pool thread.
Definition: ThreadPool.h:92
This class wraps the ThreadPool into a singleton for global usage.
Definition: ThreadPool.h:185
ThreadPoolSingleton()
Protected default constructor.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15