From: Michal Nowikowski Date: Tue, 11 Dec 2018 16:06:42 +0000 (+0100) Subject: changes after review of perfdhcp changes: X-Git-Tag: 421-create-config-backend-for-dhcpv6-base_base~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d2fe61d87075dc05b9d82b5599de4a8c8c757e3;p=thirdparty%2Fkea.git changes after review of perfdhcp changes: - replaced asserts with exceptions - replaced std::thread with util::thread::Thread - fixed handling run_flag_ --- diff --git a/m4macros/ax_cpp11.m4 b/m4macros/ax_cpp11.m4 index 17fbda1809..da2b042d8d 100644 --- a/m4macros/ax_cpp11.m4 +++ b/m4macros/ax_cpp11.m4 @@ -177,6 +177,17 @@ for retry in "none" "--std=c++11" "--std=c++0x" "--std=c++1x" "fail"; do break], [AC_MSG_RESULT([no]) continue]) + + AC_MSG_CHECKING(boost atomic_flag support) + feature="boost atomic_flag" + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [#include ], + [boost::atomic_flag run_flag;])], + [AC_MSG_RESULT([yes]) + break], + [AC_MSG_RESULT([no]) + continue]) done ])dnl AX_ISC_RPATH diff --git a/src/bin/perfdhcp/command_options.cc b/src/bin/perfdhcp/command_options.cc index 7c5237195b..87add9da68 100644 --- a/src/bin/perfdhcp/command_options.cc +++ b/src/bin/perfdhcp/command_options.cc @@ -583,6 +583,10 @@ CommandOptions::initialize(int argc, char** argv, bool print_cmd_line) { std::cout << "Running: " << stream.str() << std::endl; } + if (!isSingleThreaded()) { + std::cout << "Multi-thread mode enabled." << std::endl; + } + // Handle the local '-l' address/interface if (!localname_.empty()) { if (server_name_.empty()) { diff --git a/src/bin/perfdhcp/receiver.cc b/src/bin/perfdhcp/receiver.cc index 0473e58c88..1eb5d9f284 100644 --- a/src/bin/perfdhcp/receiver.cc +++ b/src/bin/perfdhcp/receiver.cc @@ -6,9 +6,12 @@ #include #include +#include #include +#include + using namespace std; using namespace isc::dhcp; @@ -21,8 +24,11 @@ Receiver::start() { if (single_threaded_) { return; } - assert(run_flag_.test_and_set() == false); - recv_thread_.reset(new thread{&Receiver::run, this}); + if (run_flag_.test_and_set()) { + run_flag_.clear(); + isc_throw(isc::Unexpected, "run_flag_ should be false."); + } + recv_thread_.reset(new util::thread::Thread(boost::bind(&Receiver::run, this))); } void @@ -30,12 +36,12 @@ Receiver::stop() { if (single_threaded_) { return; } - // Clear flags to order the thread to stop its main loop. - run_flag_.clear(); - // To be sure first check if thread is running and ready to be joined. - if (recv_thread_->joinable()) { - recv_thread_->join(); + // If thread is running then... + if (run_flag_.test_and_set()) { + // Clear flags to order the thread to stop its main loop. + run_flag_.clear(); + recv_thread_->wait(); } } @@ -70,12 +76,17 @@ Receiver::getPkt() { void Receiver::run() { - assert(single_threaded_ == false); + if (single_threaded_) { + isc_throw(isc::Unexpected, "run should not be invoked in single-thread mode."); + } try { // If the flag is still true receive packets. while (run_flag_.test_and_set()) { receivePackets(); } + + // Clear run flag so that subsequent call to stop will not try to stop again. + run_flag_.clear(); } catch (const exception& e) { cerr << "Something went wrong: " << e.what() << endl; usleep(1000); diff --git a/src/bin/perfdhcp/receiver.h b/src/bin/perfdhcp/receiver.h index bda6a9dced..04d7bcd2d0 100644 --- a/src/bin/perfdhcp/receiver.h +++ b/src/bin/perfdhcp/receiver.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -43,7 +44,7 @@ private: boost::atomic_flag run_flag_; /// \brief Thread for receiving packets. - std::unique_ptr recv_thread_; + std::unique_ptr recv_thread_; /// \brief Queue for passing packets from receiver thread to main thread. std::queue pkt_queue_;