Skip to content

Commit fa93951

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e39c9de commit fa93951

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

run-command.c

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,6 @@ struct parallel_processes {
15021502
const size_t max_processes;
15031503
size_t nr_processes;
15041504

1505-
get_next_task_fn get_next_task;
1506-
start_failure_fn start_failure;
1507-
task_finished_fn task_finished;
1508-
15091505
struct {
15101506
enum child_state state;
15111507
struct child_process process;
@@ -1525,21 +1521,6 @@ struct parallel_processes {
15251521
struct strbuf buffered_output; /* of finished children */
15261522
};
15271523

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-
15431524
static void kill_children(const struct parallel_processes *pp, int signo)
15441525
{
15451526
for (size_t i = 0; i < pp->max_processes; i++)
@@ -1560,22 +1541,15 @@ static void pp_init(struct parallel_processes *pp,
15601541
const struct run_process_parallel_opts *opts)
15611542
{
15621543
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;
15661544

15671545
if (!n)
15681546
BUG("you must provide a non-zero number of processes!");
15691547

15701548
trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
15711549
(uintmax_t)n);
15721550

1573-
if (!get_next_task)
1551+
if (!opts->get_next_task)
15741552
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;
15791553

15801554
CALLOC_ARRAY(pp->children, n);
15811555
if (!pp->ungroup)
@@ -1622,7 +1596,8 @@ static void pp_cleanup(struct parallel_processes *pp)
16221596
* <0 no new job was started, user wishes to shutdown early. Use negative code
16231597
* to signal the children.
16241598
*/
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)
16261601
{
16271602
size_t i;
16281603
int code;
@@ -1633,10 +1608,10 @@ static int pp_start_one(struct parallel_processes *pp)
16331608
if (i == pp->max_processes)
16341609
BUG("bookkeeping is hard");
16351610

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);
16401615
if (!code) {
16411616
if (!pp->ungroup) {
16421617
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
@@ -1651,10 +1626,14 @@ static int pp_start_one(struct parallel_processes *pp)
16511626
pp->children[i].process.no_stdin = 1;
16521627

16531628
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+
16581637
if (!pp->ungroup) {
16591638
strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
16601639
strbuf_reset(&pp->children[i].err);
@@ -1709,7 +1688,8 @@ static void pp_output(const struct parallel_processes *pp)
17091688
}
17101689
}
17111690

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)
17131693
{
17141694
int code;
17151695
size_t i, n = pp->max_processes;
@@ -1724,9 +1704,12 @@ static int pp_collect_finished(struct parallel_processes *pp)
17241704

17251705
code = finish_command(&pp->children[i].process);
17261706

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;
17301713

17311714
if (code)
17321715
result = code;
@@ -1795,7 +1778,7 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
17951778
i < spawn_cap && !pp.shutdown &&
17961779
pp.nr_processes < pp.max_processes;
17971780
i++) {
1798-
code = pp_start_one(&pp);
1781+
code = pp_start_one(&pp, opts);
17991782
if (!code)
18001783
continue;
18011784
if (code < 0) {
@@ -1813,7 +1796,7 @@ void run_processes_parallel(const struct run_process_parallel_opts *opts)
18131796
pp_buffer_stderr(&pp, output_timeout);
18141797
pp_output(&pp);
18151798
}
1816-
code = pp_collect_finished(&pp);
1799+
code = pp_collect_finished(&pp, opts);
18171800
if (code) {
18181801
pp.shutdown = 1;
18191802
if (code < 0)

0 commit comments

Comments
 (0)