Explicitly SIGHUP threads to kill them out of syscalls when we want to join them...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 9 Apr 2013 20:37:55 +0000 (22:37 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 9 Apr 2013 20:37:55 +0000 (22:37 +0200)
input.cpp
main.cpp
server.cpp

index 47c3c01..ccc4bc0 100644 (file)
--- a/input.cpp
+++ b/input.cpp
@@ -11,6 +11,7 @@
 #include <sys/socket.h>
 #include <netdb.h>
 #include <poll.h>
+#include <signal.h>
 #include <errno.h>
 #include <vector>
 #include <string>
@@ -128,6 +129,7 @@ void Input::stop()
 {
        should_stop = true;
 
+       pthread_kill(worker_thread, SIGHUP);
        if (pthread_join(worker_thread, NULL) == -1) {
                perror("pthread_join");
                exit(1);
@@ -269,7 +271,7 @@ void Input::do_work()
                        pfd.events |= POLLRDHUP;
 
                        int nfds = poll(&pfd, 1, 50);
-                       if (nfds == 0 || (nfds == -1 && errno == EAGAIN)) {
+                       if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
                                continue;
                        }
                        if (nfds == -1) {
index 2af1996..0564e27 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -94,7 +94,7 @@ void *acceptor_thread_run(void *arg)
                pfd.events = POLLIN;
 
                int nfds = poll(&pfd, 1, 50);
-               if (nfds == 0 || (nfds == -1 && errno == EAGAIN)) {
+               if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
                        continue;
                }
                if (nfds == -1) {
@@ -187,7 +187,7 @@ sleep:
                int left_to_sleep = parms->stats_interval;
                do {
                        left_to_sleep = sleep(left_to_sleep);
-               } while (left_to_sleep > 0);
+               } while (left_to_sleep > 0 && !hupped);
        }
        return NULL;
 }
@@ -433,11 +433,13 @@ int main(int argc, char **argv)
        gettimeofday(&serialize_start, NULL);
 
        if (!stats_file.empty()) {
+               pthread_kill(stats_thread, SIGHUP);
                if (pthread_join(stats_thread, NULL) == -1) {
                        perror("pthread_join");
                        exit(1);
                }
        }
+       pthread_kill(acceptor_thread, SIGHUP);
        if (pthread_join(acceptor_thread, NULL) == -1) {
                perror("pthread_join");
                exit(1);
index 5c55636..6e1005e 100644 (file)
@@ -9,6 +9,7 @@
 #include <sys/ioctl.h>
 #include <sys/epoll.h>
 #include <time.h>
+#include <signal.h>
 #include <errno.h>
 #include <vector>
 #include <string>
@@ -180,6 +181,7 @@ void Server::stop()
                should_stop = true;
        }
 
+       pthread_kill(worker_thread, SIGHUP);
        if (pthread_join(worker_thread, NULL) == -1) {
                perror("pthread_join");
                exit(1);
@@ -211,6 +213,9 @@ void Server::do_work()
        for ( ;; ) {
                int nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT_MS);
                if (nfds == -1 && errno == EINTR) {
+                       if (should_stop) {
+                               return;
+                       }
                        continue;
                }
                if (nfds == -1) {
@@ -220,10 +225,6 @@ void Server::do_work()
 
                MutexLock lock(&mutex);  // We release the mutex between iterations.
        
-               if (should_stop) {
-                       return;
-               }
-
                process_queued_data();
 
                for (int i = 0; i < nfds; ++i) {
@@ -248,6 +249,10 @@ void Server::do_work()
                                process_client(to_process[i]);
                        }
                }
+
+               if (should_stop) {
+                       return;
+               }
        }
 }