Skip to content

Commit 6a48b42

Browse files
avargitster
authored andcommitted
run-command API: make "n" parameter a "size_t"
Make the "n" variable added in c553c72 (run-command: add an asynchronous parallel child processor, 2015-12-15) a "size_t". As we'll see in a subsequent commit we do pass "0" here, but never "jobs < 0". We could have made it an "unsigned int", but as we're having to change this let's not leave another case in the codebase where a size_t and "unsigned int" size differ on some platforms. In this case it's likely to never matter, but it's easier to not need to worry about it. After this and preceding changes: make run-command.o DEVOPTS=extra-all CFLAGS=-Wno-unused-parameter Only has one (and new) -Wsigned-compare warning relevant to a comparison about our "n" or "{nr,max}_processes": About using our "n" (size_t) in the same expression as online_cpus() (int). A subsequent commit will adjust & deal with online_cpus() and that warning. The only users of the "n" parameter are: * builtin/fetch.c: defaults to 1, reads from the "fetch.parallel" config. As seen in the code that parses the config added in d54dea7 (fetch: let --jobs=<n> parallelize --multiple, too, 2019-10-05) will die if the git_config_int() return value is < 0. It will however pass us n = 0, as we'll see in a subsequent commit. * submodule.c: defaults to 1, reads from "submodule.fetchJobs" config. Read via code originally added in a028a19 (fetching submodules: respect `submodule.fetchJobs` config option, 2016-02-29). It now piggy-backs on the the submodule.fetchJobs code and validation added in f20e7c1 (submodule: remove submodule.fetchjobs from submodule-config parsing, 2017-08-02). Like builtin/fetch.c it will die if the git_config_int() return value is < 0, but like builtin/fetch.c it will pass us n = 0. * builtin/submodule--helper.c: defaults to 1. Read via code originally added in 2335b87 (submodule update: expose parallelism to the user, 2016-02-29). Since f20e7c1 (submodule: remove submodule.fetchjobs from submodule-config parsing, 2017-08-02) it shares a config parser and semantics with the submodule.c caller. * hook.c: hardcoded to 1, see 96e7225 (hook: add 'run' subcommand, 2021-12-22). * t/helper/test-run-command.c: can be -1 after parsing the arguments, but will then be overridden to online_cpus() before passing it to this API. See be5d88e (test-tool run-command: learn to run (parts of) the testsuite, 2019-10-04). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 910e2b3 commit 6a48b42

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

run-command.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,8 +1500,8 @@ int run_processes_parallel_ungroup;
15001500
struct parallel_processes {
15011501
void *data;
15021502

1503-
int max_processes;
1504-
int nr_processes;
1503+
size_t max_processes;
1504+
size_t nr_processes;
15051505

15061506
get_next_task_fn get_next_task;
15071507
start_failure_fn start_failure;
@@ -1522,7 +1522,7 @@ struct parallel_processes {
15221522
unsigned shutdown : 1;
15231523
unsigned ungroup : 1;
15241524

1525-
int output_owner;
1525+
size_t output_owner;
15261526
struct strbuf buffered_output; /* of finished children */
15271527
};
15281528

@@ -1543,9 +1543,7 @@ static int default_task_finished(int result,
15431543

15441544
static void kill_children(struct parallel_processes *pp, int signo)
15451545
{
1546-
int i, n = pp->max_processes;
1547-
1548-
for (i = 0; i < n; i++)
1546+
for (size_t i = 0; i < pp->max_processes; i++)
15491547
if (pp->children[i].state == GIT_CP_WORKING)
15501548
kill(pp->children[i].process.pid, signo);
15511549
}
@@ -1560,20 +1558,19 @@ static void handle_children_on_signal(int signo)
15601558
}
15611559

15621560
static void pp_init(struct parallel_processes *pp,
1563-
int n,
1561+
size_t n,
15641562
get_next_task_fn get_next_task,
15651563
start_failure_fn start_failure,
15661564
task_finished_fn task_finished,
15671565
void *data, int ungroup)
15681566
{
1569-
int i;
1570-
15711567
if (n < 1)
15721568
n = online_cpus();
15731569

15741570
pp->max_processes = n;
15751571

1576-
trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1572+
trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
1573+
(uintmax_t)n);
15771574

15781575
pp->data = data;
15791576
if (!get_next_task)
@@ -1594,7 +1591,7 @@ static void pp_init(struct parallel_processes *pp,
15941591
CALLOC_ARRAY(pp->pfd, n);
15951592
strbuf_init(&pp->buffered_output, 0);
15961593

1597-
for (i = 0; i < n; i++) {
1594+
for (size_t i = 0; i < n; i++) {
15981595
strbuf_init(&pp->children[i].err, 0);
15991596
child_process_init(&pp->children[i].process);
16001597
if (pp->pfd) {
@@ -1609,10 +1606,8 @@ static void pp_init(struct parallel_processes *pp,
16091606

16101607
static void pp_cleanup(struct parallel_processes *pp)
16111608
{
1612-
int i;
1613-
16141609
trace_printf("run_processes_parallel: done");
1615-
for (i = 0; i < pp->max_processes; i++) {
1610+
for (size_t i = 0; i < pp->max_processes; i++) {
16161611
strbuf_release(&pp->children[i].err);
16171612
child_process_clear(&pp->children[i].process);
16181613
}
@@ -1639,7 +1634,8 @@ static void pp_cleanup(struct parallel_processes *pp)
16391634
*/
16401635
static int pp_start_one(struct parallel_processes *pp)
16411636
{
1642-
int i, code;
1637+
size_t i;
1638+
int code;
16431639

16441640
for (i = 0; i < pp->max_processes; i++)
16451641
if (pp->children[i].state == GIT_CP_FREE)
@@ -1697,7 +1693,7 @@ static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
16971693
}
16981694

16991695
/* Buffer output from all pipes. */
1700-
for (i = 0; i < pp->max_processes; i++) {
1696+
for (size_t i = 0; i < pp->max_processes; i++) {
17011697
if (pp->children[i].state == GIT_CP_WORKING &&
17021698
pp->pfd[i].revents & (POLLIN | POLLHUP)) {
17031699
int n = strbuf_read_once(&pp->children[i].err,
@@ -1714,7 +1710,7 @@ static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
17141710

17151711
static void pp_output(struct parallel_processes *pp)
17161712
{
1717-
int i = pp->output_owner;
1713+
size_t i = pp->output_owner;
17181714

17191715
if (pp->children[i].state == GIT_CP_WORKING &&
17201716
pp->children[i].err.len) {
@@ -1725,8 +1721,8 @@ static void pp_output(struct parallel_processes *pp)
17251721

17261722
static int pp_collect_finished(struct parallel_processes *pp)
17271723
{
1728-
int i, code;
1729-
int n = pp->max_processes;
1724+
int code;
1725+
size_t i, n = pp->max_processes;
17301726
int result = 0;
17311727

17321728
while (pp->nr_processes > 0) {
@@ -1783,7 +1779,7 @@ static int pp_collect_finished(struct parallel_processes *pp)
17831779
return result;
17841780
}
17851781

1786-
void run_processes_parallel(int n,
1782+
void run_processes_parallel(size_t n,
17871783
get_next_task_fn get_next_task,
17881784
start_failure_fn start_failure,
17891785
task_finished_fn task_finished,
@@ -1817,9 +1813,7 @@ void run_processes_parallel(int n,
18171813
if (!pp.nr_processes)
18181814
break;
18191815
if (ungroup) {
1820-
int i;
1821-
1822-
for (i = 0; i < pp.max_processes; i++)
1816+
for (size_t i = 0; i < pp.max_processes; i++)
18231817
pp.children[i].state = GIT_CP_WAIT_CLEANUP;
18241818
} else {
18251819
pp_buffer_stderr(&pp, output_timeout);
@@ -1836,7 +1830,7 @@ void run_processes_parallel(int n,
18361830
pp_cleanup(&pp);
18371831
}
18381832

1839-
void run_processes_parallel_tr2(int n, get_next_task_fn get_next_task,
1833+
void run_processes_parallel_tr2(size_t n, get_next_task_fn get_next_task,
18401834
start_failure_fn start_failure,
18411835
task_finished_fn task_finished, void *pp_cb,
18421836
const char *tr2_category, const char *tr2_label)

run-command.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,12 @@ typedef int (*task_finished_fn)(int result,
485485
* API reads that setting.
486486
*/
487487
extern int run_processes_parallel_ungroup;
488-
void run_processes_parallel(int n,
488+
void run_processes_parallel(size_t n,
489489
get_next_task_fn,
490490
start_failure_fn,
491491
task_finished_fn,
492492
void *pp_cb);
493-
void run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn,
493+
void run_processes_parallel_tr2(size_t n, get_next_task_fn, start_failure_fn,
494494
task_finished_fn, void *pp_cb,
495495
const char *tr2_category, const char *tr2_label);
496496

0 commit comments

Comments
 (0)