void
MultiThreadingMgr::enterCriticalSection() {
+ std::lock_guard<std::mutex> lk(mutex_);
stopProcessing();
++critical_section_count_;
}
void
MultiThreadingMgr::exitCriticalSection() {
+ std::lock_guard<std::mutex> lk(mutex_);
if (critical_section_count_ == 0) {
isc_throw(InvalidOperation, "invalid negative value for override");
}
bool
MultiThreadingMgr::isInCriticalSection() {
+ std::lock_guard<std::mutex> lk(mutex_);
+ return (isInCriticalSectionInternal());
+}
+
+bool
+MultiThreadingMgr::isInCriticalSectionInternal() {
return (critical_section_count_ != 0);
}
void
MultiThreadingMgr::stopProcessing() {
- if (getMode() && !isInCriticalSection()) {
+ if (getMode() && !isInCriticalSectionInternal()) {
if (getThreadPoolSize()) {
thread_pool_.stop();
}
void
MultiThreadingMgr::startProcessing() {
- if (getMode() && !isInCriticalSection()) {
+ if (getMode() && !isInCriticalSectionInternal()) {
if (getThreadPoolSize()) {
thread_pool_.start(getThreadPoolSize());
}
/// @brief Apply the multi-threading related settings.
///
+ /// This function should usually be called after constructing a
+ /// @ref MultiThreadingCriticalSection so that all thread pool parameters
+ /// can be safely applied.
+ ///
/// @param enabled The enabled flag: true if multi-threading is enabled,
/// false otherwise.
/// @param thread_count The desired number of threads: non 0 if explicitly
private:
+ /// @brief Is in critical section flag.
+ ///
+ /// Should be called in a thread safe context.
+ ///
+ /// @return The critical section flag.
+ bool isInCriticalSectionInternal();
+
/// @brief Class method stops non-critical processing.
///
/// Stops the DHCP thread pool if it's running and invokes
/// @brief List of CriticalSection entry and exit callback pairs.
CSCallbackPairList cs_callbacks_;
+
+ /// @brief Mutex to protect the internal state.
+ std::mutex mutex_;
};
/// @note: everything here MUST be used ONLY from the main thread.