From 9169064cf5beca557cb834265b0f1389060ae8be Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sun, 21 Aug 2016 18:05:51 -0400 Subject: [PATCH] Move keepalive_thread to cache_timer_thread. --- src/pwmd.c | 90 ++++++++++++++------------------------------------------------ 1 file changed, 20 insertions(+), 70 deletions(-) diff --git a/src/pwmd.c b/src/pwmd.c index 886b48a5..30142c8c 100644 --- a/src/pwmd.c +++ b/src/pwmd.c @@ -99,7 +99,6 @@ static jmp_buf jmp; static int nofork; static pthread_cond_t quit_cond; static pthread_mutex_t quit_mutex; -static pthread_t keepalive_tid; static int log_fd; static unsigned assuan_level; @@ -1495,41 +1494,6 @@ init_client (int fd, const char *addr) return rc; } -static void* -keepalive_thread (void *arg) -{ -#ifndef HAVE_PTHREAD_CANCEL - int *n = xmalloc (sizeof (int)); - - *n = 0; - pthread_setspecific (signal_thread_key, n); - INIT_THREAD_SIGNAL; -#endif - -#ifdef HAVE_PR_SET_NAME - prctl (PR_SET_NAME, "keepalive"); -#endif - pthread_setspecific (thread_name_key, str_asprintf ("!%s", __FUNCTION__)); - - for (;;) - { - int n = config_get_integer ("global", "keepalive_interval"); - struct timeval tv = { n, 0 }; -#ifndef HAVE_PTHREAD_CANCEL - int *sigusr2; - - sigusr2 = (int *) pthread_getspecific (signal_thread_key); - if (*sigusr2) - break; -#endif - - send_status_all (STATUS_KEEPALIVE, NULL); - select (0, NULL, NULL, NULL, &tv); - } - - return NULL; -} - #ifdef WITH_GNUTLS /* From Beej's Guide to Network Programming. It's a good tutorial. */ static void * @@ -1930,6 +1894,7 @@ accept_thread (void *arg) static void * cache_timer_thread (void *arg) { + unsigned k = 0; #ifndef HAVE_PTHREAD_CANCEL int *n = xmalloc (sizeof (int)); @@ -1939,13 +1904,14 @@ cache_timer_thread (void *arg) #endif #ifdef HAVE_PR_SET_NAME - prctl (PR_SET_NAME, "cache timer"); + prctl (PR_SET_NAME, "timer"); #endif pthread_setspecific (thread_name_key, str_asprintf ("!%s", __FUNCTION__)); for (;;) { struct timeval tv = { 1, 0 }; + unsigned keepalive = config_get_integer ("global", "keepalive_interval"); #ifndef HAVE_PTHREAD_CANCEL int *n; @@ -1956,6 +1922,12 @@ cache_timer_thread (void *arg) select (0, NULL, NULL, NULL, &tv); cache_adjust_timeout (); + + if (++k >= keepalive) + { + send_status_all (STATUS_KEEPALIVE, NULL); + k = 0; + } } return NULL; @@ -2068,7 +2040,6 @@ server_loop (int sockfd, char **socketpath) pthread_t accept_tid; pthread_t cache_timeout_tid; int cancel_timeout_thread = 0, cancel_accept_thread = 0; - int cancel_keepalive_thread = 0; sigset_t sigset; int n; int segv = 0; @@ -2137,15 +2108,6 @@ server_loop (int sockfd, char **socketpath) log_write (_("Listening on %s"), *socketpath); #endif - rc = create_thread (keepalive_thread, NULL, &keepalive_tid, 0); - if (rc) - { - log_write ("%s(%i): pthread_create(): %s", __FILE__, __LINE__, - pwmd_strerror (rc)); - goto done; - } - - cancel_keepalive_thread = 1; rc = create_thread (reload_rcfile_thread, NULL, &rcfile_tid, 0); if (rc) { @@ -2195,6 +2157,17 @@ done: pthread_join (accept_tid, NULL); } + if (cancel_timeout_thread) + { +#ifdef HAVE_PTHREAD_CANCEL + n = pthread_cancel (cache_timeout_tid); +#else + n = pthread_kill (cache_timeout_tid, SIGUSR2); +#endif + if (!n) + pthread_join (cache_timeout_tid, NULL); + } + #ifdef WITH_GNUTLS start_stop_tls (1); #endif @@ -2230,30 +2203,7 @@ done: pwmd_strerror (rc)); } - if (cancel_timeout_thread) - { -#ifdef HAVE_PTHREAD_CANCEL - pthread_cancel (cache_timeout_tid); -#else - pthread_kill (cache_timeout_tid, SIGUSR2); -#endif - pthread_join (cache_timeout_tid, NULL); - } - - if (cancel_keepalive_thread) - { -#ifdef HAVE_PTHREAD_CANCEL - pthread_cancel (keepalive_tid); -#else - pthread_kill (keepalive_tid, SIGUSR2); -#endif - pthread_join (keepalive_tid, NULL); - } - cleanup_all_clients (0); -#ifdef WITH_GNUTLS - start_stop_tls (1); -#endif deinit_commands (); pthread_cond_destroy (&quit_cond); pthread_mutex_destroy (&quit_mutex); -- 2.11.4.GIT