11#include <boost/range/adaptor/reversed.hpp>
17 : enabled_(false), test_mode_(false), critical_section_count_(0),
18 thread_pool_size_(0) {
42 checkCallbacksPermissions();
48 ++critical_section_count_;
71 --critical_section_count_;
90 return (critical_section_count_ != 0);
100 return (thread_pool_size_);
105 thread_pool_size_ = size;
120 return (std::thread::hardware_concurrency());
139 if (thread_pool_.
size()) {
146 thread_pool_.
start(thread_count);
150 thread_pool_.
reset();
158MultiThreadingMgr::checkCallbacksPermissions() {
160 for (
auto const& cb : cs_callbacks_.getCallbackSets()) {
181MultiThreadingMgr::callEntryCallbacks() {
184 for (
auto const& cb_it : callbacks) {
197MultiThreadingMgr::callExitCallbacks() {
200 for (
auto const& cb_it :
boost::adaptors::reverse(callbacks)) {
243 lock_ = std::unique_lock<std::mutex>(mutex);
258 <<
" cannot be empty");
263 <<
" cannot be empty");
268 <<
" cannot be empty");
271 for (
auto const& callback : cb_sets_) {
272 if (callback.name_ == name) {
274 <<
" already exist");
278 cb_sets_.push_back(
CSCallbackSet(name, check_cb, entry_cb, exit_cb));
283 for (
auto it = cb_sets_.begin(); it != cb_sets_.end(); ++it) {
284 if ((*it).name_ == name) {
296const std::list<CSCallbackSet>&
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
Exception thrown when a worker thread is trying to stop or pause the respective thread pool (which wo...
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.
MultiThreadingCriticalSection()
Constructor.
virtual ~MultiThreadingCriticalSection()
Destructor.
void removeAllCriticalSectionCallbacks()
Removes all callbacks in the list of CriticalSection callbacks.
void setMode(bool enabled)
Set the multi-threading mode.
virtual ~MultiThreadingMgr()
Destructor.
MultiThreadingMgr()
Constructor.
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.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.
Embodies a named set of CriticalSection callbacks.
std::function< void()> Callback
Defines a callback as a simple void() functor.
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.
void setMaxQueueSize(size_t max_queue_size)
set maximum number of work items in the queue
void reset()
reset the thread pool stopping threads and clearing the internal queue
void start(uint32_t thread_count)
start all the threads
bool enabled()
return the enable state of the queue
size_t getMaxQueueSize()
get maximum number of work items in the queue
size_t size()
size number of thread pool threads
void resume()
resume threads
void stop()
stop all the threads
void pause(bool wait=true)
pause threads