Kea  2.5.3
multi_threading_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2019-2022 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 
36 struct CSCallbackSet {
38  typedef std::function<void()> Callback;
39 
47  CSCallbackSet(const std::string& name, const Callback& check_cb,
48  const Callback& entry_cb, const Callback& exit_cb)
49  : name_(name), check_cb_(check_cb), entry_cb_(entry_cb),
50  exit_cb_(exit_cb) {}
51 
53  std::string name_;
54 
57 
60 
63 };
64 
72 public:
75 
85  void addCallbackSet(const std::string& name,
86  const CSCallbackSet::Callback& check_cb,
87  const CSCallbackSet::Callback& entry_cb,
88  const CSCallbackSet::Callback& exit_cb);
89 
94  void removeCallbackSet(const std::string& name);
95 
97  void removeAll();
98 
100  const std::list<CSCallbackSet>& getCallbackSets();
101 
102 private:
104  std::list<CSCallbackSet> cb_sets_;
105 };
106 
136 class MultiThreadingMgr : public boost::noncopyable {
137 public:
144  static MultiThreadingMgr& instance();
145 
150  bool getMode() const;
151 
155  void setMode(bool enabled);
156 
169  void enterCriticalSection();
170 
183  void exitCriticalSection();
184 
188  bool isInCriticalSection();
189 
193  ThreadPool<std::function<void()>>& getThreadPool();
194 
198  uint32_t getThreadPoolSize() const;
199 
203  void setThreadPoolSize(uint32_t size);
204 
208  uint32_t getPacketQueueSize();
209 
213  void setPacketQueueSize(uint32_t size);
214 
220  static uint32_t detectThreadCount();
221 
234  void apply(bool enabled, uint32_t thread_count, uint32_t queue_size);
235 
248  void addCriticalSectionCallbacks(const std::string& name,
249  const CSCallbackSet::Callback& check_cb,
250  const CSCallbackSet::Callback& entry_cb,
251  const CSCallbackSet::Callback& exit_cb);
252 
260  void removeCriticalSectionCallbacks(const std::string& name);
261 
264 
265 protected:
266 
269 
271  virtual ~MultiThreadingMgr();
272 
273 private:
274 
287  void checkCallbacksPermissions();
288 
295  void callEntryCallbacks();
296 
303  void callExitCallbacks();
304 
309  bool enabled_;
310 
317  uint32_t critical_section_count_;
318 
320  uint32_t thread_pool_size_;
321 
323  ThreadPool<std::function<void()>> thread_pool_;
324 
326  CSCallbackSetList cs_callbacks_;
327 };
328 
336  MultiThreadingLock(std::mutex& mutex);
337 
338 private:
340  std::unique_lock<std::mutex> lock_;
341 };
342 
345 
355 class MultiThreadingCriticalSection : public boost::noncopyable {
356 public:
357 
363 
369 };
370 
371 } // namespace util
372 } // namespace isc
373 
374 #endif // MULTI_THREADING_MGR_H
Maintains list of unique CSCallbackSets.
void removeCallbackSet(const std::string &name)
Removes a callback set from the list.
const std::list< CSCallbackSet > & getCallbackSets()
Fetches the list of callback sets.
void removeAll()
Removes all callbacks from the list.
void addCallbackSet(const std::string &name, const CSCallbackSet::Callback &check_cb, const CSCallbackSet::Callback &entry_cb, const CSCallbackSet::Callback &exit_cb)
Adds a callback set to the list.
RAII class creating a critical section.
Multi Threading Manager.
void removeAllCriticalSectionCallbacks()
Removes all callbacks in the list of CriticalSection callbacks.
void setMode(bool enabled)
Set the multi-threading mode.
virtual ~MultiThreadingMgr()
Destructor.
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
void enterCriticalSection()
Enter critical section.
void setThreadPoolSize(uint32_t size)
Set the configured dhcp thread pool size.
uint32_t getPacketQueueSize()
Get the configured dhcp packet queue size.
void removeCriticalSectionCallbacks(const std::string &name)
Removes the set of callbacks associated with a given name from the list of CriticalSection callbacks.
void setPacketQueueSize(uint32_t size)
Set the configured dhcp packet queue size.
uint32_t getThreadPoolSize() const
Get the configured dhcp thread pool size.
ThreadPool< std::function< void()> > & getThreadPool()
Get the dhcp thread pool.
bool isInCriticalSection()
Is in critical section flag.
static uint32_t detectThreadCount()
The system current detected hardware concurrency thread count.
void apply(bool enabled, uint32_t thread_count, uint32_t queue_size)
Apply the multi-threading related settings.
bool getMode() const
Get the multi-threading mode.
void addCriticalSectionCallbacks(const std::string &name, const CSCallbackSet::Callback &check_cb, const CSCallbackSet::Callback &entry_cb, const CSCallbackSet::Callback &exit_cb)
Adds a set of callbacks to the list of CriticalSection callbacks.
void exitCriticalSection()
Exit critical section.
Defines the logger used by the top-level component of kea-lfc.
Embodies a named set of CriticalSection callbacks.
Callback check_cb_
Check permissions callback associated with name.
CSCallbackSet(const std::string &name, const Callback &check_cb, const Callback &entry_cb, const Callback &exit_cb)
Constructor.
std::function< void()> Callback
Defines a callback as a simple void() functor.
Callback entry_cb_
Entry point callback associated with name.
std::string name_
Name by which the callback can be found.
Callback exit_cb_
Exit point callback associated with name.
RAII lock object to protect the code in the same scope with a mutex.
MultiThreadingLock(std::mutex &mutex)
Constructor locks the mutex if multi-threading is enabled.
Defines a thread pool which uses a thread pool queue for managing work items.
Definition: thread_pool.h:34