+# We can't use waitpid(-1) safely here since it can hit ``, system(),
+# and other things. So we scan the $wait_pids list, which is hopefully
+# not too big. We keep $wait_pids small by not calling dwaitpid()
+# until we've hit EOF when reading the stdout of the child.
+sub reap_pids {
+ my $tmp = $wait_pids or return;
+ $wait_pids = $reap_timer = undef;
+ foreach my $ary (@$tmp) {
+ my ($pid, $cb, $arg) = @$ary;
+ my $ret = waitpid($pid, WNOHANG);
+ if ($ret == 0) {
+ push @$wait_pids, $ary; # autovivifies @$wait_pids
+ } elsif ($cb) {
+ eval { $cb->($arg, $pid) };
+ }
+ }
+ # we may not be done, yet, and could've missed/masked a SIGCHLD:
+ $reap_timer = add_timer(1, \&reap_pids) if $wait_pids;
+}
+
+# reentrant SIGCHLD handler (since reap_pids is not reentrant)
+sub enqueue_reap ($) { push @$nextq, \&reap_pids }; # autovivifies
+
+sub in_loop () { $in_loop }
+
+# Internal function: run the post-event callback, send read events
+# for pushed-back data, and close pending connections. returns 1
+# if event loop should continue, or 0 to shut it all down.
+sub PostEventLoop () {
+ # now we can close sockets that wanted to close during our event
+ # processing. (we didn't want to close them during the loop, as we
+ # didn't want fd numbers being reused and confused during the event
+ # loop)
+ if (my $close_now = $ToClose) {
+ $ToClose = undef; # will be autovivified on push
+ @$close_now = map { fileno($_) } @$close_now;
+
+ # order matters, destroy expiry times, first:
+ delete @$EXPMAP{@$close_now};
+
+ # ->DESTROY methods may populate ToClose
+ delete @DescriptorMap{@$close_now};
+ }
+
+ # by default we keep running, unless a postloop callback cancels it
+ $PostLoopCallback ? $PostLoopCallback->(\%DescriptorMap) : 1;
+}
+