Skip to content

Commit c333e6f

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

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

run-command.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,9 +1498,9 @@ enum child_state {
14981498

14991499
int run_processes_parallel_ungroup;
15001500
struct parallel_processes {
1501-
void *data;
1501+
void *const data;
15021502

1503-
size_t max_processes;
1503+
const size_t max_processes;
15041504
size_t nr_processes;
15051505

15061506
get_next_task_fn get_next_task;
@@ -1520,7 +1520,7 @@ struct parallel_processes {
15201520
struct pollfd *pfd;
15211521

15221522
unsigned shutdown : 1;
1523-
unsigned ungroup : 1;
1523+
const unsigned ungroup : 1;
15241524

15251525
size_t output_owner;
15261526
struct strbuf buffered_output; /* of finished children */
@@ -1558,38 +1558,28 @@ static void handle_children_on_signal(int signo)
15581558
}
15591559

15601560
static void pp_init(struct parallel_processes *pp,
1561-
size_t n,
15621561
get_next_task_fn get_next_task,
15631562
start_failure_fn start_failure,
1564-
task_finished_fn task_finished,
1565-
void *data, int ungroup)
1563+
task_finished_fn task_finished)
15661564
{
1565+
const size_t n = pp->max_processes;
1566+
15671567
if (!n)
15681568
BUG("you must provide a non-zero number of processes!");
15691569

1570-
pp->max_processes = n;
1571-
15721570
trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
15731571
(uintmax_t)n);
15741572

1575-
pp->data = data;
15761573
if (!get_next_task)
15771574
BUG("you need to specify a get_next_task function");
15781575
pp->get_next_task = get_next_task;
15791576

15801577
pp->start_failure = start_failure ? start_failure : default_start_failure;
15811578
pp->task_finished = task_finished ? task_finished : default_task_finished;
15821579

1583-
pp->nr_processes = 0;
1584-
pp->output_owner = 0;
1585-
pp->shutdown = 0;
1586-
pp->ungroup = ungroup;
15871580
CALLOC_ARRAY(pp->children, n);
1588-
if (pp->ungroup)
1589-
pp->pfd = NULL;
1590-
else
1581+
if (!pp->ungroup)
15911582
CALLOC_ARRAY(pp->pfd, n);
1592-
strbuf_init(&pp->buffered_output, 0);
15931583

15941584
for (size_t i = 0; i < n; i++) {
15951585
strbuf_init(&pp->children[i].err, 0);
@@ -1789,13 +1779,17 @@ void run_processes_parallel(size_t n,
17891779
int output_timeout = 100;
17901780
int spawn_cap = 4;
17911781
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+
};
17931788

17941789
/* unset for the next API user */
17951790
run_processes_parallel_ungroup = 0;
17961791

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);
17991793
while (1) {
18001794
for (i = 0;
18011795
i < spawn_cap && !pp.shutdown &&

0 commit comments

Comments
 (0)