Kea  1.9.9-git
multi_threading_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2019-2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef MULTI_THREADING_MGR_H
8 #define MULTI_THREADING_MGR_H
9 
10 #include <util/thread_pool.h>
11 #include <functional>
12 #include <list>
13 
14 #include <boost/noncopyable.hpp>
15 
16 #include <stdint.h>
17 
18 namespace isc {
19 namespace util {
20 
21 
31  typedef std::function<void()> Callback;
32 
38  CSCallbackPair(const std::string& name, const Callback& entry_cb,
39  const Callback& exit_cb)
40  : name_(name), entry_cb_(entry_cb), exit_cb_(exit_cb) {}
41 
43  std::string name_;
44 
46  Callback entry_cb_;
47 
49  Callback exit_cb_;
50 };
51 
59 public:
62 
71  void addCallbackPair(const std::string& name,
72  const CSCallbackPair::Callback& entry_cb,
73  const CSCallbackPair::Callback& exit_cb);
74 
79  void removeCallbackPair(const std::string& name);
80 
82  void removeAll();
83 
85  const std::list<CSCallbackPair>& getCallbackPairs();
86 
87 private:
89  std::list<CSCallbackPair> cb_pairs_;
90 };
91 
120 class MultiThreadingMgr : public boost::noncopyable {
121 public:
122 
129  static MultiThreadingMgr& instance();
130 
135  bool getMode() const;
136 
140  void setMode(bool enabled);
141 
148  void enterCriticalSection();
149 
156  void exitCriticalSection();
157 
161  bool isInCriticalSection();
162 
167 
171  uint32_t getThreadPoolSize() const;
172 
176  void setThreadPoolSize(uint32_t size);
177 
181  uint32_t getPacketQueueSize();
182 
186  void setPacketQueueSize(uint32_t size);
187 
193  static uint32_t detectThreadCount();
194 
203  void apply(bool enabled, uint32_t thread_count, uint32_t queue_size);
204 
215  void addCriticalSectionCallbacks(const std::string& name,
216  const CSCallbackPair::Callback& entry_cb,
217  const CSCallbackPair::Callback& exit_cb);
218 
226  void removeCriticalSectionCallbacks(const std::string& name);
227 
230 
231 protected:
232 
235 
237  virtual ~MultiThreadingMgr();
238 
239 private:
240 
250  void stopProcessing();
251 
261  void startProcessing();
262 
267  bool enabled_;
268 
275  uint32_t critical_section_count_;
276 
278  uint32_t thread_pool_size_;
279 
281  ThreadPool<std::function<void()>> thread_pool_;
282 
284  CSCallbackPairList cs_callbacks_;
285 };
286 
289 
299 class MultiThreadingCriticalSection : public boost::noncopyable {
300 public:
301 
307 
313 };
314 
315 } // namespace util
316 } // namespace isc
317 
318 #endif // MULTI_THREADING_MGR_H
RAII class creating a critical section.
Maintains list of unique CSCallbackPairs.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
uint32_t getThreadPoolSize() const
Get the configured dhcp thread pool size.
void removeAllCriticalSectionCallbacks()
Removes all callbacks in the list of CriticalSection callbacks.
void enterCriticalSection()
Enter critical section.
void exitCriticalSection()
Exit critical section.
void setThreadPoolSize(uint32_t size)
Set the configured dhcp thread pool size.
Multi Threading Manager.
std::function< void()> Callback
Defines a callback as a simple void() functor.
void setMode(bool enabled)
Set the multi-threading mode.
bool getMode() const
Get the multi-threading mode.
void removeAll()
Removes all callbacks from the list.
const std::list< CSCallbackPair > & getCallbackPairs()
Fetches the list of callback pairs.
void addCallbackPair(const std::string &name, const CSCallbackPair::Callback &entry_cb, const CSCallbackPair::Callback &exit_cb)
Adds a callback pair to the list.
void addCriticalSectionCallbacks(const std::string &name, const CSCallbackPair::Callback &entry_cb, const CSCallbackPair::Callback &exit_cb)
Adds a pair of callbacks to the list of CriticalSection callbacks.
virtual ~MultiThreadingMgr()
Destructor.
void setPacketQueueSize(uint32_t size)
Set the configured dhcp packet queue size.
Callback entry_cb_
Entry point callback associated with name.
Defines the logger used by the top-level component of kea-dhcp-ddns.
CSCallbackPair(const std::string &name, const Callback &entry_cb, const Callback &exit_cb)
Constructor.
Callback exit_cb_
Exit point callback associated with name.
ThreadPool< std::function< void()> > & getThreadPool()
Get the dhcp thread pool.
void removeCriticalSectionCallbacks(const std::string &name)
Removes the set of callbacks associated with a given name from the list of CriticalSection callbacks...
void removeCallbackPair(const std::string &name)
Removes a callback pair from the list.
bool isInCriticalSection()
Is in critical section flag.
Defines a thread pool which uses a thread pool queue for managing work items.
Definition: thread_pool.h:34
static uint32_t detectThreadCount()
The system current detected hardware concurrency thread count.
uint32_t getPacketQueueSize()
Get the configured dhcp packet queue size.
void apply(bool enabled, uint32_t thread_count, uint32_t queue_size)
Apply the multi-threading related settings.
std::string name_
Name by which the callback can be found.
Embodies a named pair of CriticalSection callbacks.