1490
return pp_child->state == GIT_CP_WORKING;
1491
}
1492
1493
+static int child_is_ready_for_cleanup(const struct parallel_child *pp_child)
1494
+{
1495
+ return child_is_working(pp_child) && !pp_child->process.in;
1496
+}
1497
+
1498
+static int child_is_receiving_input(const struct parallel_child *pp_child)
1499
+{
1500
+ return child_is_working(pp_child) && pp_child->process.in > 0;
1501
+}
1502
+
1503
struct parallel_processes {
1504
size_t nr_processes;
1505
1669
return 0;
1670
}
1671
1672
+static void pp_buffer_stdin(struct parallel_processes *pp,
1673
+ const struct run_process_parallel_opts *opts)
1674
+{
1675
+ /* Buffer stdin for each pipe. */
1676
+ for (size_t i = 0; i < opts->processes; i++) {
1677
+ struct child_process *proc = &pp->children[i].process;
1678
+ int ret;
1679
+
1680
+ if (!child_is_receiving_input(&pp->children[i]))
1681
+ continue;
1682
+
1683
+ /*
1684
+ * child input is provided via path_to_stdin when the feed_pipe cb is
1685
+ * missing, so we just signal an EOF.
1686
+ */
1687
+ if (!opts->feed_pipe) {
1688
+ close(proc->in);
1689
+ proc->in = 0;
1690
+ continue;
1691
+ }
1692
+
1693
+ /**
1694
+ * Feed the pipe:
1695
+ * ret < 0 means error
1696
+ * ret == 0 means there is more data to be fed
1697
+ * ret > 0 means feeding finished
1698
+ */
1699
+ ret = opts->feed_pipe(proc->in, opts->data, pp->children[i].data);
1700
+ if (ret < 0)
1701
+ die_errno("feed_pipe");
1702
+
1703
+ if (ret) {
1704
+ close(proc->in);
1705
+ proc->in = 0;
1706
+ }
1707
+ }
1708
+}
1709
+
1710
static void pp_buffer_stderr(struct parallel_processes *pp,
1711
const struct run_process_parallel_opts *opts,
1712
int output_timeout)
1777
pp->children[i].state = GIT_CP_FREE;
1778
if (pp->pfd)
1779
pp->pfd[i].fd = -1;
1780
+ pp->children[i].process.in = 0;
1781
child_process_init(&pp->children[i].process);
1782
1783
if (opts->ungroup) {
1812
return result;
1813
}
1814
1815
+static void pp_handle_child_IO(struct parallel_processes *pp,
1816
+ const struct run_process_parallel_opts *opts,
1817
+ int output_timeout)
1818
+{
1819
+ /*
1820
+ * First push input, if any (it might no-op), to child tasks to avoid them blocking
1821
+ * after input. This also prevents deadlocks when ungrouping below, if a child blocks
1822
+ * while the parent also waits for them to finish.
1823
+ */
1824
+ pp_buffer_stdin(pp, opts);
1825
+
1826
+ if (opts->ungroup) {
1827
+ for (size_t i = 0; i < opts->processes; i++)
1828
+ if (child_is_ready_for_cleanup(&pp->children[i]))
1829
+ pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1830
+ } else {
1831
+ pp_buffer_stderr(pp, opts, output_timeout);
1832
+ pp_output(pp);
1833
+ }
1834
+}
1835
+
1836
void run_processes_parallel(const struct run_process_parallel_opts *opts)
1837
{
1838
int i, code;
1852
"max:%"PRIuMAX,
1853
(uintmax_t)opts->processes);
1854
1855
+ /*
1856
+ * Child tasks might receive input via stdin, terminating early (or not), so
1857
+ * ignore the default SIGPIPE which gets handled by each feed_pipe_fn which
1858
+ * actually writes the data to children stdin fds.
1859
+ */
1860
+ sigchain_push(SIGPIPE, SIG_IGN);
1861
+
1862
pp_init(&pp, opts, &pp_sig);
1863
while (1) {
1864
for (i = 0;
1876
}
1877
if (!pp.nr_processes)
1878
break;
1802
- if (opts->ungroup) {
1803
- for (size_t i = 0; i < opts->processes; i++)
1804
- pp.children[i].state = GIT_CP_WAIT_CLEANUP;
1805
- } else {
1806
- pp_buffer_stderr(&pp, opts, output_timeout);
1807
- pp_output(&pp);
1808
- }
1879
+ pp_handle_child_IO(&pp, opts, output_timeout);
1880
code = pp_collect_finished(&pp, opts);
1881
if (code) {
1882
pp.shutdown = 1;
1887
1888
pp_cleanup(&pp, opts);
1889
1890
+ sigchain_pop(SIGPIPE);
1891
+
1892
if (do_trace2)
1893
trace2_region_leave(tr2_category, tr2_label, NULL);
1894
}