run-command.c: use designated init for pp_init(), add "const"

Use a designated initializer to initialize those parts of pp_init() that don't need any conditionals for their initialization, this sets us on a path to pp_init() itself into mostly a validation and allocation function. Since we're doing that we can add "const" to some of the members of the "struct parallel_processes", which helps to clarify and self-document this code. E.g. we never alter the "data" pointer we pass t user callbacks, nor (after the preceding change to stop invoking online_cpus()) do we change "max_processes", the same goes for the "ungroup" option. We can also do away with a call to strbuf_init() in favor of macro initialization, and to rely on other fields being NULL'd or zero'd. Making members of a struct "const" rather that the pointer to the struct itself is usually painful, as e.g. it precludes us from incrementally setting up the structure. In this case we only set it up with the assignment in run_process_parallel() and pp_init(), and don't pass the struct pointer around as "const", so making individual members "const" is worth the potential hassle for extra safety. 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 c333e6f3a86140c39797f8dedf6d078aec453dc3
1 file changed +14 -20
run-command.c
+14 -20
@@ -1498,9 +1498,9 @@ enum child_state {
1498
1499 int run_processes_parallel_ungroup;
1500 struct parallel_processes {
1501 - void *data;
1501 + void *const data;
1502
1503 - size_t max_processes;
1503 + const size_t max_processes;
1504 size_t nr_processes;
1505
1506 get_next_task_fn get_next_task;
@@ -1520,7 +1520,7 @@ struct parallel_processes {
1520 struct pollfd *pfd;
1521
1522 unsigned shutdown : 1;
1523 - unsigned ungroup : 1;
1523 + const unsigned ungroup : 1;
1524
1525 size_t output_owner;
1526 struct strbuf buffered_output; /* of finished children */
@@ -1558,21 +1558,18 @@ static void handle_children_on_signal(int signo)
1558 }
1559
1560 static void pp_init(struct parallel_processes *pp,
1561 - size_t n,
1561 get_next_task_fn get_next_task,
1562 start_failure_fn start_failure,
1564 - task_finished_fn task_finished,
1565 - void *data, int ungroup)
1563 + task_finished_fn task_finished)
1564 {
1565 + const size_t n = pp->max_processes;
1566 +
1567 if (!n)
1568 BUG("you must provide a non-zero number of processes!");
1569
1570 - pp->max_processes = n;
1571 -
1570 trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
1571 (uintmax_t)n);
1572
1575 - pp->data = data;
1573 if (!get_next_task)
1574 BUG("you need to specify a get_next_task function");
1575 pp->get_next_task = get_next_task;
@@ -1580,16 +1577,9 @@ static void pp_init(struct parallel_processes *pp,
1577 pp->start_failure = start_failure ? start_failure : default_start_failure;
1578 pp->task_finished = task_finished ? task_finished : default_task_finished;
1579
1583 - pp->nr_processes = 0;
1584 - pp->output_owner = 0;
1585 - pp->shutdown = 0;
1586 - pp->ungroup = ungroup;
1580 CALLOC_ARRAY(pp->children, n);
1588 - if (pp->ungroup)
1589 - pp->pfd = NULL;
1590 - else
1581 + if (!pp->ungroup)
1582 CALLOC_ARRAY(pp->pfd, n);
1592 - strbuf_init(&pp->buffered_output, 0);
1583
1584 for (size_t i = 0; i < n; i++) {
1585 strbuf_init(&pp->children[i].err, 0);
@@ -1789,13 +1779,17 @@ void run_processes_parallel(size_t n,
1779 int output_timeout = 100;
1780 int spawn_cap = 4;
1781 int ungroup = run_processes_parallel_ungroup;
1792 - struct parallel_processes pp;
1782 + struct parallel_processes pp = {
1783 + .max_processes = n,
1784 + .data = pp_cb,
1785 + .buffered_output = STRBUF_INIT,
1786 + .ungroup = ungroup,
1787 + };
1788
1789 /* unset for the next API user */
1790 run_processes_parallel_ungroup = 0;
1791
1797 - pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb,
1798 - ungroup);
1792 + pp_init(&pp, get_next_task, start_failure, task_finished);
1793 while (1) {
1794 for (i = 0;
1795 i < spawn_cap && !pp.shutdown &&