Commit 5d2fe61d authored by Michal Nowikowski's avatar Michal Nowikowski

changes after review of perfdhcp changes:

- replaced asserts with exceptions
- replaced std::thread with util::thread::Thread
- fixed handling run_flag_
parent 9b225b86
......@@ -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.hpp>],
[boost::atomic_flag run_flag;])],
[AC_MSG_RESULT([yes])
break],
[AC_MSG_RESULT([no])
continue])
done
])dnl AX_ISC_RPATH
......@@ -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()) {
......
......@@ -6,9 +6,12 @@
#include <perfdhcp/receiver.h>
#include <perfdhcp/command_options.h>
#include <util/threads/thread.h>
#include <dhcp/iface_mgr.h>
#include <boost/bind.hpp>
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);
......
......@@ -11,6 +11,7 @@
#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>
#include <util/threads/thread.h>
#include <queue>
#include <thread>
......@@ -43,7 +44,7 @@ private:
boost::atomic_flag run_flag_;
/// \brief Thread for receiving packets.
std::unique_ptr<std::thread> recv_thread_;
std::unique_ptr<util::thread::Thread> recv_thread_;
/// \brief Queue for passing packets from receiver thread to main thread.
std::queue<PktPtr> pkt_queue_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment