run-command.c: don't copy *_fn to "struct parallel_processes"
The only remaining reason for copying the callbacks in the "struct run_process_parallel_opts" over to the "struct parallel_processes" was to avoid two if/else statements in case the "start_failure" and "task_finished" callbacks were NULL. Let's handle those cases in pp_start_one() and pp_collect_finished() instead, and avoid the default_* stub functions, and the need to copy this data around. Organizing the code like this made more sense before the "struct run_parallel_parallel_opts" existed, as we'd have needed to pass each of these as a separate parameter. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Oct 12, 2022 at 23:02 UTC
fa93951d796a4837f6d24c54b638b976dbb17a8b
1 file changed
+25
-42
run-command.c
+25
-42
@@ -1502,10 +1502,6 @@ struct parallel_processes {
1502
const size_t max_processes;
1503
size_t nr_processes;
1504
1505
- get_next_task_fn get_next_task;
1506
- start_failure_fn start_failure;
1507
- task_finished_fn task_finished;
1508
-
1505
struct {
1506
enum child_state state;
1507
struct child_process process;
@@ -1525,21 +1521,6 @@ struct parallel_processes {
1521
struct strbuf buffered_output; /* of finished children */
1522
};
1523
1528
-static int default_start_failure(struct strbuf *out,
1529
- void *pp_cb,
1530
- void *pp_task_cb)
1531
-{
1532
- return 0;
1533
-}
1534
-
1535
-static int default_task_finished(int result,
1536
- struct strbuf *out,
1537
- void *pp_cb,
1538
- void *pp_task_cb)
1539
-{
1540
- return 0;
1541
-}
1542
-
1524
static void kill_children(const struct parallel_processes *pp, int signo)
1525
{
1526
for (size_t i = 0; i < pp->max_processes; i++)
@@ -1560,9 +1541,6 @@ static void pp_init(struct parallel_processes *pp,
1541
const struct run_process_parallel_opts *opts)
1542
{
1543
const size_t n = opts->processes;
1563
- get_next_task_fn get_next_task = opts->get_next_task;
1564
- start_failure_fn start_failure = opts->start_failure;
1565
- task_finished_fn task_finished = opts->task_finished;
1544
1545
if (!n)
1546
BUG("you must provide a non-zero number of processes!");
@@ -1570,12 +1548,8 @@ static void pp_init(struct parallel_processes *pp,
1548
trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
1549
(uintmax_t)n);
1550
1573
- if (!get_next_task)
1551
+ if (!opts->get_next_task)
1552
BUG("you need to specify a get_next_task function");
1575
- pp->get_next_task = get_next_task;
1576
-
1577
- pp->start_failure = start_failure ? start_failure : default_start_failure;
1578
- pp->task_finished = task_finished ? task_finished : default_task_finished;
1553
1554
CALLOC_ARRAY(pp->children, n);
1555
if (!pp->ungroup)
@@ -1622,7 +1596,8 @@ static void pp_cleanup(struct parallel_processes *pp)
1596
* <0 no new job was started, user wishes to shutdown early. Use negative code
1597
* to signal the children.
1598
*/
1625
-static int pp_start_one(struct parallel_processes *pp)
1599
+static int pp_start_one(struct parallel_processes *pp,
1600
+ const struct run_process_parallel_opts *opts)
1601
{
1602
size_t i;
1603
int code;
@@ -1633,10 +1608,10 @@ static int pp_start_one(struct parallel_processes *pp)
1608
if (i == pp->max_processes)
1609
BUG("bookkeeping is hard");
1610
1636
- code = pp->get_next_task(&pp->children[i].process,
1637
- pp->ungroup ? NULL : &pp->children[i].err,
1638
- pp->data,
1639
- &pp->children[i].data);
1611
+ code = opts->get_next_task(&pp->children[i].process,
1612
+ pp->ungroup ? NULL : &pp->children[i].err,
1613
+ pp->data,
1614
+ &pp->children[i].data);
1615
if (!code) {
1616
if (!pp->ungroup) {
1617
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
@@ -1651,10 +1626,14 @@ static int pp_start_one(struct parallel_processes *pp)
1626
pp->children[i].process.no_stdin = 1;
1627
1628
if (start_command(&pp->children[i].process)) {
1654
- code = pp->start_failure(pp->ungroup ? NULL :
1655
- &pp->children[i].err,
1656
- pp->data,
1657
- pp->children[i].data);
1629
+ if (opts->start_failure)
1630
+ code = opts->start_failure(pp->ungroup ? NULL :
1631
+ &pp->children[i].err,
1632
+ pp->data,
1633
+ pp->children[i].data);
1634
+ else
1635
+ code = 0;
1636
+
1637
if (!pp->ungroup) {
1638
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1639
strbuf_reset(&pp->children[i].err);
@@ -1709,7 +1688,8 @@ static void pp_output(const struct parallel_processes *pp)
1688
}
1689
}
1690
1712
-static int pp_collect_finished(struct parallel_processes *pp)
1691
+static int pp_collect_finished(struct parallel_processes *pp,
1692
+ const struct run_process_parallel_opts *opts)
1693
{
1694
int code;
1695
size_t i, n = pp->max_processes;
@@ -1724,9 +1704,12 @@ static int pp_collect_finished(struct parallel_processes *pp)
1704
1705
code = finish_command(&pp->children[i].process);
1706
1727
- code = pp->task_finished(code, pp->ungroup ? NULL :
1728
- &pp->children[i].err, pp->data,
1729
- pp->children[i].data);
1707
+ if (opts->task_finished)
1708
+ code = opts->task_finished(code, pp->ungroup ? NULL :
1709
+ &pp->children[i].err, pp->data,
1710
+ pp->children[i].data);
1711
+ else
1712
+ code = 0;
1713
1714
if (code)
1715
result = code;
@@ -1795,7 +1778,7 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
1778
i < spawn_cap && !pp.shutdown &&
1779
pp.nr_processes < pp.max_processes;
1780
i++) {
1798
- code = pp_start_one(&pp);
1781
+ code = pp_start_one(&pp, opts);
1782
if (!code)
1783
continue;
1784
if (code < 0) {
@@ -1813,7 +1796,7 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
1796
pp_buffer_stderr(&pp, output_timeout);
1797
pp_output(&pp);
1798
}
1816
- code = pp_collect_finished(&pp);
1799
+ code = pp_collect_finished(&pp, opts);
1800
if (code) {
1801
pp.shutdown = 1;
1802
if (code < 0)