Kea  2.1.7-git
isc::util::MultiThreadingMgr Class Reference

Multi Threading Manager. More...

#include <multi_threading_mgr.h>

+ Inheritance diagram for isc::util::MultiThreadingMgr:

Public Member Functions

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. More...
 
void apply (bool enabled, uint32_t thread_count, uint32_t queue_size)
 Apply the multi-threading related settings. More...
 
void enterCriticalSection ()
 Enter critical section. More...
 
void exitCriticalSection ()
 Exit critical section. More...
 
bool getMode () const
 Get the multi-threading mode. More...
 
uint32_t getPacketQueueSize ()
 Get the configured dhcp packet queue size. More...
 
ThreadPool< std::function< void()> > & getThreadPool ()
 Get the dhcp thread pool. More...
 
uint32_t getThreadPoolSize () const
 Get the configured dhcp thread pool size. More...
 
bool isInCriticalSection ()
 Is in critical section flag. More...
 
void removeAllCriticalSectionCallbacks ()
 Removes all callbacks in the list of CriticalSection callbacks. More...
 
void removeCriticalSectionCallbacks (const std::string &name)
 Removes the set of callbacks associated with a given name from the list of CriticalSection callbacks. More...
 
void setMode (bool enabled)
 Set the multi-threading mode. More...
 
void setPacketQueueSize (uint32_t size)
 Set the configured dhcp packet queue size. More...
 
void setThreadPoolSize (uint32_t size)
 Set the configured dhcp thread pool size. More...
 

Static Public Member Functions

static uint32_t detectThreadCount ()
 The system current detected hardware concurrency thread count. More...
 
static MultiThreadingMgrinstance ()
 Returns a single instance of Multi Threading Manager. More...
 

Protected Member Functions

 MultiThreadingMgr ()
 Constructor. More...
 
virtual ~MultiThreadingMgr ()
 Destructor. More...
 

Detailed Description

Multi Threading Manager.

This singleton class holds the multi-threading mode.

The standard way to use it is:

multi-threaded code
} else {
single-threaded code
}

For instance for a class protected by its mutex:

namespace locked {
void foo() { ... }
} // end of locked namespace
void foo() {
lock_guard<mutex> lock(mutex_);
locked::foo();
} else {
locked::foo();
}
}

Definition at line 135 of file multi_threading_mgr.h.

Constructor & Destructor Documentation

◆ MultiThreadingMgr()

isc::util::MultiThreadingMgr::MultiThreadingMgr ( )
protected

Constructor.

Definition at line 14 of file multi_threading_mgr.cc.

◆ ~MultiThreadingMgr()

isc::util::MultiThreadingMgr::~MultiThreadingMgr ( )
protectedvirtual

Destructor.

Definition at line 18 of file multi_threading_mgr.cc.

Member Function Documentation

◆ addCriticalSectionCallbacks()

void isc::util::MultiThreadingMgr::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.

Note
Callbacks must be exception-safe, handling any errors themselves.
Parameters
nameName of the set of callbacks. This value is used by the callback owner to add and remove them. Duplicates are not allowed.
check_cbCallback to check current thread permissions to call the CriticalSection entry and exit callbacks.
entry_cbCallback to invoke upon CriticalSection entry. Cannot be empty.
exit_cbCallback to invoke upon CriticalSection exit. Cannot be empty.

Definition at line 205 of file multi_threading_mgr.cc.

References isc::util::CSCallbackSetList::addCallbackSet().

+ Here is the call graph for this function:

◆ apply()

void isc::util::MultiThreadingMgr::apply ( bool  enabled,
uint32_t  thread_count,
uint32_t  queue_size 
)

Apply the multi-threading related settings.

This function should usually be called after constructing a MultiThreadingCriticalSection so that all thread pool parameters can be safely applied.

Parameters
enabledThe enabled flag: true if multi-threading is enabled, false otherwise.
thread_countThe desired number of threads: non 0 if explicitly configured, 0 if auto scaling is desired
queue_sizeThe desired thread queue size: non 0 if explicitly configured, 0 for unlimited size

Definition at line 113 of file multi_threading_mgr.cc.

References detectThreadCount(), isc::util::CSCallbackSetList::getCallbackSets(), getMode(), isInCriticalSection(), removeAllCriticalSectionCallbacks(), isc::util::ThreadPool< WorkItem, Container >::reset(), setMode(), setPacketQueueSize(), setThreadPoolSize(), isc::util::ThreadPool< WorkItem, Container >::size(), isc::util::ThreadPool< WorkItem, Container >::start(), and isc::util::ThreadPool< WorkItem, Container >::stop().

+ Here is the call graph for this function:

◆ detectThreadCount()

uint32_t isc::util::MultiThreadingMgr::detectThreadCount ( )
static

The system current detected hardware concurrency thread count.

This function will return 0 if the value can not be determined.

Returns
The thread count.

Definition at line 108 of file multi_threading_mgr.cc.

Referenced by apply().

◆ enterCriticalSection()

void isc::util::MultiThreadingMgr::enterCriticalSection ( )

Enter critical section.

When entering MultiThreadingCriticalSection, increment internal counter so that any configuration change that might start the packet thread pool is delayed until exiting the respective section. If the internal counter is 0, then stop the thread pool.

Invokes all CriticalSection entry callbacks. Has no effect in single-threaded mode.

Note
This function swallows exceptions thrown by all entry callbacks without logging to avoid breaking the CS chain.

Definition at line 38 of file multi_threading_mgr.cc.

References getMode(), getThreadPoolSize(), isInCriticalSection(), and isc::util::ThreadPool< WorkItem, Container >::stop().

Referenced by isc::util::MultiThreadingCriticalSection::MultiThreadingCriticalSection().

+ Here is the call graph for this function:

◆ exitCriticalSection()

void isc::util::MultiThreadingMgr::exitCriticalSection ( )

Exit critical section.

When exiting MultiThreadingCriticalSection, decrement internal counter so that the dhcp thread pool can be started according to the new configuration. If the internal counter is 0, then start the thread pool.

Invokes all CriticalSection exit callbacks. Has no effect in single-threaded mode.

Note
This function swallows exceptions thrown by all exit callbacks without logging to avoid breaking the CS chain.

Definition at line 56 of file multi_threading_mgr.cc.

References getMode(), getThreadPoolSize(), isc_throw, isInCriticalSection(), and isc::util::ThreadPool< WorkItem, Container >::start().

Referenced by isc::util::MultiThreadingCriticalSection::~MultiThreadingCriticalSection().

+ Here is the call graph for this function:

◆ getMode()

bool isc::util::MultiThreadingMgr::getMode ( ) const

Get the multi-threading mode.

Returns
The current multi-threading mode: true if multi-threading is enabled, false otherwise.

Definition at line 28 of file multi_threading_mgr.cc.

Referenced by apply(), enterCriticalSection(), exitCriticalSection(), isc::dhcp::ClientHandler::tryLock(), and isc::dhcp::ClientHandler::~ClientHandler().

◆ getPacketQueueSize()

uint32_t isc::util::MultiThreadingMgr::getPacketQueueSize ( )

Get the configured dhcp packet queue size.

Returns
The dhcp packet queue size.

Definition at line 98 of file multi_threading_mgr.cc.

References isc::util::ThreadPool< WorkItem, Container >::getMaxQueueSize().

+ Here is the call graph for this function:

◆ getThreadPool()

ThreadPool< std::function< void()> > & isc::util::MultiThreadingMgr::getThreadPool ( )

Get the dhcp thread pool.

Returns
The dhcp thread pool.

Definition at line 83 of file multi_threading_mgr.cc.

Referenced by isc::dhcp::ClientHandler::tryLock(), and isc::dhcp::ClientHandler::~ClientHandler().

◆ getThreadPoolSize()

uint32_t isc::util::MultiThreadingMgr::getThreadPoolSize ( ) const

Get the configured dhcp thread pool size.

Returns
The dhcp thread pool size.

Definition at line 88 of file multi_threading_mgr.cc.

Referenced by enterCriticalSection(), and exitCriticalSection().

◆ instance()

◆ isInCriticalSection()

bool isc::util::MultiThreadingMgr::isInCriticalSection ( )

Is in critical section flag.

Returns
The critical section flag.

Definition at line 78 of file multi_threading_mgr.cc.

Referenced by apply(), enterCriticalSection(), and exitCriticalSection().

◆ removeAllCriticalSectionCallbacks()

void isc::util::MultiThreadingMgr::removeAllCriticalSectionCallbacks ( )

Removes all callbacks in the list of CriticalSection callbacks.

Definition at line 218 of file multi_threading_mgr.cc.

References isc::util::CSCallbackSetList::removeAll().

Referenced by apply().

+ Here is the call graph for this function:

◆ removeCriticalSectionCallbacks()

void isc::util::MultiThreadingMgr::removeCriticalSectionCallbacks ( const std::string &  name)

Removes the set of callbacks associated with a given name from the list of CriticalSection callbacks.

If the name is not found in the list, it simply returns, otherwise both callbacks registered under the name are removed.

Parameters
nameName of the set of callbacks to remove.

Definition at line 213 of file multi_threading_mgr.cc.

References isc::util::CSCallbackSetList::removeCallbackSet().

+ Here is the call graph for this function:

◆ setMode()

void isc::util::MultiThreadingMgr::setMode ( bool  enabled)

Set the multi-threading mode.

Parameters
enabledThe new mode.

Definition at line 33 of file multi_threading_mgr.cc.

Referenced by apply(), isc::test::MultiThreadingTest::MultiThreadingTest(), and isc::test::MultiThreadingTest::~MultiThreadingTest().

◆ setPacketQueueSize()

void isc::util::MultiThreadingMgr::setPacketQueueSize ( uint32_t  size)

Set the configured dhcp packet queue size.

Parameters
sizeThe dhcp packet queue size.

Definition at line 103 of file multi_threading_mgr.cc.

References isc::util::ThreadPool< WorkItem, Container >::setMaxQueueSize().

Referenced by apply().

+ Here is the call graph for this function:

◆ setThreadPoolSize()

void isc::util::MultiThreadingMgr::setThreadPoolSize ( uint32_t  size)

Set the configured dhcp thread pool size.

Parameters
sizeThe dhcp thread pool size.

Definition at line 93 of file multi_threading_mgr.cc.

Referenced by apply().


The documentation for this class was generated from the following files: