From 2bfebba7be63518f337663ec9a81a4be0de835f7 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 22 Feb 2023 08:39:54 +0100 Subject: Don't wait for 3 seconds if there were some q_items processed. --- httpd.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'httpd.c') diff --git a/httpd.c b/httpd.c index 8c5e092..1d5b2f0 100644 --- a/httpd.c +++ b/httpd.c @@ -17,6 +17,10 @@ #include // for getrlimit() +#ifdef USE_POLL_FOR_SLEEP +#include +#endif + /* nodeinfo 2.0 template */ const char *nodeinfo_2_0_template = "" @@ -338,6 +342,7 @@ static void *job_thread(void *arg) return NULL; } +#include static void *background_thread(void *arg) /* background thread (queue management and other things) */ @@ -351,6 +356,7 @@ static void *background_thread(void *arg) while (srv_running) { time_t t; + int cnt = 0; { xs *list = user_list(); @@ -362,14 +368,14 @@ static void *background_thread(void *arg) snac snac; if (user_open(&snac, uid)) { - process_user_queue(&snac); + cnt += process_user_queue(&snac); user_free(&snac); } } } /* global queue */ - process_queue(); + cnt += process_queue(); /* time to purge? */ if ((t = time(NULL)) > purge_time) { @@ -381,17 +387,24 @@ static void *background_thread(void *arg) job_post(q_item); } - /* sleep 3 seconds */ - pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; - pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; - struct timespec ts; + if (cnt == 0) { + /* sleep 3 seconds */ + +#ifdef USE_POLL_FOR_SLEEP + poll(NULL, 0, 3 * 1000); +#else + pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; + struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - ts.tv_sec += 3; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 3; - pthread_mutex_lock(&dummy_mutex); - while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0); - pthread_mutex_unlock(&dummy_mutex); + pthread_mutex_lock(&dummy_mutex); + while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0); + pthread_mutex_unlock(&dummy_mutex); +#endif + } } srv_log(xs_fmt("background thread stopped")); -- cgit v1.2.3