Kea: multi_threading_mgr.h Source File

| Kea 3.2.0-git

multi_threading_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2019-2025 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
18namespace isc {
19namespace 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
71 class CSCallbackSetList {
72public:
74 CSCallbackSetList() {}
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
102private:
104 std::list<CSCallbackSet> cb_sets_;
105};
106
136 class MultiThreadingMgr : public boost::noncopyable {
137public:
144 static MultiThreadingMgr& instance();
145
150 bool getMode() const;
151
155 void setMode(bool enabled);
156
161 void setTestMode(const bool test_mode) {
162 test_mode_ = test_mode;
163 }
164
169 bool isTestMode() const {
170 return (test_mode_);
171 }
172
185 void enterCriticalSection();
186
199 void exitCriticalSection();
200
204 bool isInCriticalSection();
205
209 ThreadPool<std::function<void()>>& getThreadPool();
210
214 uint32_t getThreadPoolSize() const;
215
219 void setThreadPoolSize(uint32_t size);
220
224 uint32_t getPacketQueueSize();
225
229 void setPacketQueueSize(uint32_t size);
230
236 static uint32_t detectThreadCount();
237
250 void apply(bool enabled, uint32_t thread_count, uint32_t queue_size);
251
264 void addCriticalSectionCallbacks(const std::string& name,
265 const CSCallbackSet::Callback& check_cb,
266 const CSCallbackSet::Callback& entry_cb,
267 const CSCallbackSet::Callback& exit_cb);
268
276 void removeCriticalSectionCallbacks(const std::string& name);
277
279 void removeAllCriticalSectionCallbacks();
280
281protected:
282
284 MultiThreadingMgr();
285
287 virtual ~MultiThreadingMgr();
288
289private:
290
303 void checkCallbacksPermissions();
304
311 void callEntryCallbacks();
312
319 void callExitCallbacks();
320
325 bool enabled_;
326
328 bool test_mode_;
329
336 uint32_t critical_section_count_;
337
339 uint32_t thread_pool_size_;
340
342 ThreadPool<std::function<void()>> thread_pool_;
343
345 CSCallbackSetList cs_callbacks_;
346};
347
349 struct MultiThreadingLock {
355 MultiThreadingLock(std::mutex& mutex);
356
357private:
359 std::unique_lock<std::mutex> lock_;
360};
361
364
374 class MultiThreadingCriticalSection : public boost::noncopyable {
375public:
376
381 MultiThreadingCriticalSection();
382
387 virtual ~MultiThreadingCriticalSection();
388};
389
393 class MtTestMode : public boost::noncopyable {
394public:
395 MtTestMode() {
396 MultiThreadingMgr::instance().setTestMode(true);
397 }
398
399 ~MtTestMode() {
400 MultiThreadingMgr::instance().setTestMode(false);
401 }
402};
403
404} // namespace util
405} // namespace isc
406
407#endif // MULTI_THREADING_MGR_H
isc::util::CSCallbackSetList
Maintains list of unique CSCallbackSets.
Definition multi_threading_mgr.h:71
isc::util::CSCallbackSetList::removeCallbackSet
void removeCallbackSet(const std::string &name)
Removes a callback set from the list.
Definition multi_threading_mgr.cc:282
isc::util::CSCallbackSetList::getCallbackSets
const std::list< CSCallbackSet > & getCallbackSets()
Fetches the list of callback sets.
Definition multi_threading_mgr.cc:297
isc::util::CSCallbackSetList::CSCallbackSetList
CSCallbackSetList()
Constructor.
Definition multi_threading_mgr.h:74
isc::util::CSCallbackSetList::removeAll
void removeAll()
Removes all callbacks from the list.
Definition multi_threading_mgr.cc:292
isc::util::CSCallbackSetList::addCallbackSet
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.
Definition multi_threading_mgr.cc:248
isc::util::MtTestMode::~MtTestMode
~MtTestMode()
Definition multi_threading_mgr.h:399
isc::util::MtTestMode::MtTestMode
MtTestMode()
Definition multi_threading_mgr.h:395
isc::util::MultiThreadingCriticalSection::MultiThreadingCriticalSection
MultiThreadingCriticalSection()
Constructor.
Definition multi_threading_mgr.cc:233
isc::util::MultiThreadingCriticalSection::~MultiThreadingCriticalSection
virtual ~MultiThreadingCriticalSection()
Destructor.
Definition multi_threading_mgr.cc:237
isc::util::MultiThreadingMgr::removeAllCriticalSectionCallbacks
void removeAllCriticalSectionCallbacks()
Removes all callbacks in the list of CriticalSection callbacks.
Definition multi_threading_mgr.cc:229
isc::util::MultiThreadingMgr::setMode
void setMode(bool enabled)
Set the multi-threading mode.
Definition multi_threading_mgr.cc:36
isc::util::MultiThreadingMgr::~MultiThreadingMgr
virtual ~MultiThreadingMgr()
Destructor.
Definition multi_threading_mgr.cc:21
isc::util::MultiThreadingMgr::isTestMode
bool isTestMode() const
Checks if the MultiThreadingMgr is in the test mode.
Definition multi_threading_mgr.h:169
isc::util::MultiThreadingMgr::MultiThreadingMgr
MultiThreadingMgr()
Constructor.
Definition multi_threading_mgr.cc:16
isc::util::MultiThreadingMgr::instance
static MultiThreadingMgr & instance()
Returns a single instance of Multi Threading Manager.
Definition multi_threading_mgr.cc:25
isc::util::MultiThreadingMgr::enterCriticalSection
void enterCriticalSection()
Enter critical section.
Definition multi_threading_mgr.cc:41
isc::util::MultiThreadingMgr::setThreadPoolSize
void setThreadPoolSize(uint32_t size)
Set the configured dhcp thread pool size.
Definition multi_threading_mgr.cc:104
isc::util::MultiThreadingMgr::getPacketQueueSize
uint32_t getPacketQueueSize()
Get the configured dhcp packet queue size.
Definition multi_threading_mgr.cc:109
isc::util::MultiThreadingMgr::removeCriticalSectionCallbacks
void removeCriticalSectionCallbacks(const std::string &name)
Removes the set of callbacks associated with a given name from the list of CriticalSection callbacks.
Definition multi_threading_mgr.cc:224
isc::util::MultiThreadingMgr::setPacketQueueSize
void setPacketQueueSize(uint32_t size)
Set the configured dhcp packet queue size.
Definition multi_threading_mgr.cc:114
isc::util::MultiThreadingMgr::getThreadPoolSize
uint32_t getThreadPoolSize() const
Get the configured dhcp thread pool size.
Definition multi_threading_mgr.cc:99
isc::util::MultiThreadingMgr::getThreadPool
ThreadPool< std::function< void()> > & getThreadPool()
Get the dhcp thread pool.
Definition multi_threading_mgr.cc:94
isc::util::MultiThreadingMgr::isInCriticalSection
bool isInCriticalSection()
Is in critical section flag.
Definition multi_threading_mgr.cc:89
isc::util::MultiThreadingMgr::setTestMode
void setTestMode(const bool test_mode)
Sets or clears the test mode for MultiThreadingMgr.
Definition multi_threading_mgr.h:161
isc::util::MultiThreadingMgr::detectThreadCount
static uint32_t detectThreadCount()
The system current detected hardware concurrency thread count.
Definition multi_threading_mgr.cc:119
isc::util::MultiThreadingMgr::apply
void apply(bool enabled, uint32_t thread_count, uint32_t queue_size)
Apply the multi-threading related settings.
Definition multi_threading_mgr.cc:124
isc::util::MultiThreadingMgr::getMode
bool getMode() const
Get the multi-threading mode.
Definition multi_threading_mgr.cc:31
isc::util::MultiThreadingMgr::addCriticalSectionCallbacks
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.
Definition multi_threading_mgr.cc:216
isc::util::MultiThreadingMgr::exitCriticalSection
void exitCriticalSection()
Exit critical section.
Definition multi_threading_mgr.cc:60
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::util::CSCallbackSet::check_cb_
Callback check_cb_
Check permissions callback associated with name.
Definition multi_threading_mgr.h:56
isc::util::CSCallbackSet::CSCallbackSet
CSCallbackSet(const std::string &name, const Callback &check_cb, const Callback &entry_cb, const Callback &exit_cb)
Constructor.
Definition multi_threading_mgr.h:47
isc::util::CSCallbackSet::Callback
std::function< void()> Callback
Defines a callback as a simple void() functor.
Definition multi_threading_mgr.h:38
isc::util::CSCallbackSet::entry_cb_
Callback entry_cb_
Entry point callback associated with name.
Definition multi_threading_mgr.h:59
isc::util::CSCallbackSet::name_
std::string name_
Name by which the callback can be found.
Definition multi_threading_mgr.h:53
isc::util::CSCallbackSet::exit_cb_
Callback exit_cb_
Exit point callback associated with name.
Definition multi_threading_mgr.h:62
isc::util::MultiThreadingLock::MultiThreadingLock
MultiThreadingLock(std::mutex &mutex)
Constructor locks the mutex if multi-threading is enabled.
Definition multi_threading_mgr.cc:241
isc::util::ThreadPool
Defines a thread pool which uses a thread pool queue for managing work items.
Definition thread_pool.h:34
thread_pool.h