]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
changes after review of perfdhcp changes:
authorMichal Nowikowski <godfryd@isc.org>
Tue, 11 Dec 2018 16:06:42 +0000 (17:06 +0100)
committerMichal Nowikowski <godfryd@isc.org>
Fri, 18 Jan 2019 09:05:12 +0000 (10:05 +0100)
- replaced asserts with exceptions
- replaced std::thread with util::thread::Thread
- fixed handling run_flag_

m4macros/ax_cpp11.m4
src/bin/perfdhcp/command_options.cc
src/bin/perfdhcp/receiver.cc
src/bin/perfdhcp/receiver.h

index 17fbda1809df1a836889db02ae2227afb795ac31..da2b042d8dc4ce9019e02573002da63ef1838151 100644 (file)
@@ -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
index 7c5237195bd2dd948fa077013faac826f0fbd8cf..87add9da685f2c23323abeb8860f8c8b396098da 100644 (file)
@@ -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()) {
index 0473e58c88a89eaef2034ea6d76f364d862c92be..1eb5d9f28418d048865e3be887c4bd964ef85733 100644 (file)
@@ -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);
index bda6a9dced51065204307c45e28e2b16d2de7f59..04d7bcd2d0bf8707092443851f4943fa037c82bd 100644 (file)
@@ -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_;