From 5f177f4e31de89e47c0ada66b4d59050cfff3be4 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 12 Oct 2009 12:20:11 +0200 Subject: [PATCH] Use select(2) to consume as little CPU as possible --- src/main.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 67491e5..6a8cf49 100644 --- a/src/main.c +++ b/src/main.c @@ -31,8 +31,9 @@ #include #include #include -#include "util.h" +#include #include +#include "util.h" #ifdef UNUSED #elif defined(__GNUC__) @@ -465,13 +466,36 @@ int main(int UNUSED(argc), char ** UNUSED(argv)) NULL, /* Access policy handler */ NULL, /* Data to access policy handler */ handle_request, /* default handler for all URIs */ - NULL, /* Data for default handler */ - MHD_OPTION_END); + NULL, /* Data for default handler */ + MHD_OPTION_END); if (d == NULL) { fprintf(stderr, "Could not start daemon\n"); exit(1); } while (1) { + fd_set rs, ws, es; + int max_fd = 0; + unsigned long long timeout; + struct timeval tv; + + FD_ZERO(&rs); + FD_ZERO(&ws); + FD_ZERO(&es); + + if (MHD_get_fdset(d, &rs, &ws, &es, &max_fd) == MHD_NO) { + MHD_stop_daemon(d); + exit(1); + } + if (MHD_get_timeout(d, &timeout) == MHD_NO) { + timeout = 0; + } + tv.tv_usec = (timeout % 1000) * 1000; + tv.tv_sec = timeout / 1000; + if (timeout == 0) { + select(max_fd+1, &rs, &ws, &es, NULL); + } else { + select(max_fd+1, &rs, &ws, &es, &tv); + } if (MHD_run(d) == MHD_NO) { MHD_stop_daemon(d); exit(1); -- 2.39.5