+sub check_async_wait ($) {
+ my ($self) = @_;
+ return cat_async_wait($self) if $self->{-bc};
+ my $inflight_c = $self->{inflight_c} or return;
+ check_async_step($self, $inflight_c) while (scalar(@$inflight_c));
+}
+
+sub check_async_begin ($) {
+ my ($self) = @_;
+ die 'BUG: already in async check' if $self->{inflight_c};
+ cleanup($self) if alternates_changed($self);
+ check_git_exe();
+ if ($GIT_VER >= BATCH_CMD_VER) {
+ _bidi_pipe($self, qw(batch-command in out pid err_c));
+ $self->{-bc} = 1;
+ $self->{inflight} = [];
+ } else {
+ _bidi_pipe($self, qw(batch-check in_c out_c pid_c err_c));
+ $self->{inflight_c} = [];
+ }
+}
+
+sub write_all {
+ my ($self, $out, $buf, $read_step, $inflight) = @_;
+ $read_step->($self, $inflight) while @$inflight >= MAX_INFLIGHT;
+ do {
+ my $w = syswrite($out, $buf);
+ if (defined $w) {
+ return if $w == length($buf);
+ substr($buf, 0, $w, ''); # sv_chop
+ } elsif ($! != EAGAIN) {
+ $self->fail("write: $!");
+ }
+ $read_step->($self, $inflight);
+ } while (1);
+}
+
+sub check_async ($$$$) {
+ my ($self, $oid, $cb, $arg) = @_;
+ my $inflight = $self->{-bc} ?
+ ($self->{inflight} // cat_async_begin($self)) :
+ ($self->{inflight_c} // check_async_begin($self));
+ if ($self->{-bc}) {
+ substr($oid, 0, 0) = 'info ';
+ write_all($self, $self->{out}, "$oid\n",
+ \&cat_async_step, $inflight);
+ } else {
+ write_all($self, $self->{out_c}, "$oid\n",
+ \&check_async_step, $inflight);