Kea  2.1.7-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 
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 
56  Callback check_cb_;
57 
59  Callback entry_cb_;
60 
62  Callback exit_cb_;
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 
135 class MultiThreadingMgr : public boost::noncopyable {
136 public:
143  static MultiThreadingMgr& instance();
144 
149  bool getMode() const;
150 
154  void setMode(bool enabled);
155 
168  void enterCriticalSection();
169 
182  void exitCriticalSection();
183 
187  bool isInCriticalSection();
188 
192  ThreadPool<std::function<void()>>& getThreadPool();
193 
197  uint32_t getThreadPoolSize() const;
198 
202  void setThreadPoolSize(uint32_t size);
203 
207  uint32_t getPacketQueueSize();
208 
212  void setPacketQueueSize(uint32_t size);
213 
219  static uint32_t detectThreadCount();
220 
233  void apply(bool enabled, uint32_t thread_count, uint32_t queue_size);
234 
247  void addCriticalSectionCallbacks(const std::string& name,
248  const CSCallbackSet::Callback& check_cb,
249  const CSCallbackSet::Callback& entry_cb,
250  const CSCallbackSet::Callback& exit_cb);
251 
259  void removeCriticalSectionCallbacks(const std::string& name);
260 
262  void removeAllCriticalSectionCallbacks();
263 
264 protected:
265 
268 
270  virtual ~MultiThreadingMgr();
271 
272 private:
273 
286  void checkCallbacksPermissions();
287 
294  void callEntryCallbacks();
295 
302  void callExitCallbacks();
303 
308  bool enabled_;
309 
316  uint32_t critical_section_count_;
317 
319  uint32_t thread_pool_size_;
320 
322  ThreadPool<std::function<void()>> thread_pool_;
323 
325  CSCallbackSetList cs_callbacks_;
326 };
327 
330 
340 class MultiThreadingCriticalSection : public boost::noncopyable {
341 public:
342 
348 
354 };
355 
356 } // namespace util
357 } // namespace isc
358 
359 #endif // MULTI_THREADING_MGR_H
RAII class creating a critical section.
Callback entry_cb_
Entry point callback associated with name.
Embodies a named set of CriticalSection callbacks.
CSCallbackSet(const std::string &name, const Callback &check_cb, const Callback &entry_cb, const Callback &exit_cb)
Constructor.
Multi Threading Manager.
Maintains list of unique CSCallbackSets.
Defines the logger used by the top-level component of kea-lfc.
Callback exit_cb_
Exit point callback associated with name.
std::function< void()> Callback
Defines a callback as a simple void() functor.
Callback check_cb_
Check permissions callback associated with name.
std::string name_
Name by which the callback can be found.
Defines a thread pool which uses a thread pool queue for managing work items.
Definition: thread_pool.h:34