Skip to content

Commit a93620c

Browse files
committed
Speed the catharsis up
1 parent d2aed69 commit a93620c

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

lib/children.c

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -666,29 +666,48 @@ supervisor_children_disable_obsolete(void)
666666
return count;
667667
}
668668

669+
static inline int
670+
_is_dead(const pid_t needle, pid_t *pincushion, register const int max)
671+
{
672+
for (register int i=0; i<max ;++i) {
673+
if (needle == pincushion[i])
674+
return 1;
675+
}
676+
return 0;
677+
}
678+
669679
guint
670680
supervisor_children_catharsis(void *udata, supervisor_cb_f cb)
671681
{
672-
pid_t pid_dead;
673-
guint count;
674682
struct child_s *sd;
675-
struct child_info_s ci;
683+
guint count = 0;
684+
int pids_idx = 0;
685+
pid_t pid;
686+
pid_t pids[1024];
676687

677-
count = 0;
678-
while ((pid_dead = waitpid(-1, NULL, WNOHANG)) > 0) {
679-
FOREACH_CHILD(sd) {
680-
if (sd->pid == pid_dead) {
681-
count++;
682-
_child_notify_death(sd);
683-
if (cb) {
684-
_child_get_info(sd, &ci);
685-
cb(udata, &ci);
686-
}
687-
sd->pid = -1;
688-
break;
689-
}
690-
}
688+
g_assert_nonnull(cb);
689+
690+
/* Consume a batch of dead children */
691+
while (pids_idx < 1024 && (pid = waitpid(-1, NULL, WNOHANG)) > 0)
692+
pids[pids_idx++] = pid;
693+
if (!pids_idx)
694+
return 0;
695+
696+
/* Locate the concerned structures, for each dead child */
697+
FOREACH_CHILD(sd) {
698+
if (!_is_dead(sd->pid, pids, pids_idx))
699+
continue;
700+
701+
count++;
702+
_child_notify_death(sd);
703+
704+
struct child_info_s ci = {};
705+
_child_get_info(sd, &ci);
706+
cb(udata, &ci);
707+
708+
sd->pid = -1;
691709
}
710+
692711
return count;
693712
}
694713

0 commit comments

Comments
 (0)