@matheusbsbs / matheus-bagliano / commits / f9bbefc701
Fork 0

git daemon: avoid waking up too often

To avoid waking up unnecessarily, a pipe is set up that is only ever written to by child_handler(), when a child disconnects, as suggested per Junio. This avoids waking up the main process every second to see if a child was disconnected. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 22, 2008 at 23:03 UTC f9bbefc701e319983bb8e13df8d615293c6f386a
1 file changed +11 -14
daemon.c
+11 -14
@@ -16,6 +16,7 @@
16 static int log_syslog;
17 static int verbose;
18 static int reuseaddr;
19 +static int child_handler_pipe[2];
20
21 static const char daemon_usage[] =
22 "git daemon [--verbose] [--syslog] [--export-all]\n"
@@ -788,6 +789,7 @@ static void child_handler(int signo)
789 pid = -pid;
790 dead_child[reaped % MAX_CHILDREN] = pid;
791 children_reaped = reaped + 1;
792 + write(child_handler_pipe[1], &status, 1);
793 continue;
794 }
795 break;
@@ -933,29 +935,24 @@ static int service_loop(int socknum, int *socklist)
935 struct pollfd *pfd;
936 int i;
937
936 - pfd = xcalloc(socknum, sizeof(struct pollfd));
938 + if (pipe(child_handler_pipe) < 0)
939 + die ("Could not set up pipe for child handler");
940 +
941 + pfd = xcalloc(socknum + 1, sizeof(struct pollfd));
942
943 for (i = 0; i < socknum; i++) {
944 pfd[i].fd = socklist[i];
945 pfd[i].events = POLLIN;
946 }
947 + pfd[socknum].fd = child_handler_pipe[0];
948 + pfd[socknum].events = POLLIN;
949
950 signal(SIGCHLD, child_handler);
951
952 for (;;) {
953 int i;
947 - int timeout;
954
949 - /*
950 - * This 1-sec timeout could lead to idly looping but it is
951 - * here so that children culled in child_handler() are reported
952 - * without too much delay. We could probably set up a pipe
953 - * to ourselves that we poll, and write to the fd from child_handler()
954 - * to wake us up (and consume it when the poll() returns...
955 - */
956 - timeout = (children_spawned != children_deleted) ? 1000 : -1;
957 - i = poll(pfd, socknum, timeout);
958 - if (i < 0) {
955 + if (poll(pfd, socknum + 1, -1) < 0) {
956 if (errno != EINTR) {
957 error("poll failed, resuming: %s",
958 strerror(errno));
@@ -963,9 +960,9 @@ static int service_loop(int socknum, int *socklist)
960 }
961 continue;
962 }
966 - if (i == 0) {
963 + if (pfd[socknum].revents & POLLIN) {
964 + read(child_handler_pipe[0], &i, 1);
965 check_dead_children();
968 - continue;
966 }
967
968 for (i = 0; i < socknum; i++) {