From: Steinar H. Gunderson Date: Tue, 9 Apr 2013 20:37:55 +0000 (+0200) Subject: Explicitly SIGHUP threads to kill them out of syscalls when we want to join them... X-Git-Tag: 1.0.0~164 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=5ab36b04b0c12058394335e891398c494df513d2 Explicitly SIGHUP threads to kill them out of syscalls when we want to join them. Also fixes some bugs related to EINTR handling, since they became very obvious now. --- diff --git a/input.cpp b/input.cpp index 47c3c01..ccc4bc0 100644 --- a/input.cpp +++ b/input.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -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) { diff --git a/main.cpp b/main.cpp index 2af1996..0564e27 100644 --- 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); diff --git a/server.cpp b/server.cpp index 5c55636..6e1005e 100644 --- a/server.cpp +++ b/server.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -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; + } } }