diff --git a/src/lib/http/client.cc b/src/lib/http/client.cc index b991f31be7..d907187577 100644 --- a/src/lib/http/client.cc +++ b/src/lib/http/client.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -962,24 +963,18 @@ Connection::terminateInternal(const boost::system::error_code& ec, } } - // unlock mutex so that the callback can be safely processed. - if (MultiThreadingMgr::instance().getMode()) { - mutex_.unlock(); - } - try { // The callback should take care of its own exceptions but one // never knows. - current_callback_(ec, response, parsing_error); - + if (MultiThreadingMgr::instance().getMode()) { + UnlockGuard lock(mutex_); + current_callback_(ec, response, parsing_error); + } else { + current_callback_(ec, response, parsing_error); + } } catch (...) { } - // lock mutex so that processing can continue. - if (MultiThreadingMgr::instance().getMode()) { - mutex_.lock(); - } - // If we're not requesting connection persistence, we should close the socket. // We're going to reconnect for the next transaction. if (!current_request_->isPersistent()) { @@ -989,21 +984,16 @@ Connection::terminateInternal(const boost::system::error_code& ec, resetState(); } - // unlock mutex so that the next request can be safely processed. - if (MultiThreadingMgr::instance().getMode()) { - mutex_.unlock(); - } - // Check if there are any requests queued for this connection and start // another transaction if there is at least one. ConnectionPoolPtr conn_pool = conn_pool_.lock(); if (conn_pool) { - conn_pool->processNextRequest(url_); - } - - // lock mutex so that processing can continue. - if (MultiThreadingMgr::instance().getMode()) { - mutex_.lock(); + if (MultiThreadingMgr::instance().getMode()) { + UnlockGuard lock(mutex_); + conn_pool->processNextRequest(url_); + } else { + conn_pool->processNextRequest(url_); + } } } diff --git a/src/lib/util/Makefile.am b/src/lib/util/Makefile.am index fe269446cb..52467deeda 100644 --- a/src/lib/util/Makefile.am +++ b/src/lib/util/Makefile.am @@ -31,6 +31,7 @@ libkea_util_la_SOURCES += stopwatch_impl.cc stopwatch_impl.h libkea_util_la_SOURCES += strutil.h strutil.cc libkea_util_la_SOURCES += thread_pool.h libkea_util_la_SOURCES += time_utilities.h time_utilities.cc +libkea_util_la_SOURCES += unlock_guard.h libkea_util_la_SOURCES += versioned_csv_file.h versioned_csv_file.cc libkea_util_la_SOURCES += watch_socket.cc watch_socket.h libkea_util_la_SOURCES += watched_thread.cc watched_thread.h @@ -77,6 +78,7 @@ libkea_util_include_HEADERS = \ strutil.h \ thread_pool.h \ time_utilities.h \ + unlock_guard.h \ versioned_csv_file.h \ watch_socket.h \ watched_thread.h