mirror of
https://github.com/git/git.git
synced 2026-01-11 13:23:12 +09:00
run-command: add first helper for pp child states
There is a recurring pattern of testing parallel process child states and file descriptors to determine if a child is running, receiving any input or if it's ready for cleanup. Name the pp_child structure and introduce a first helper to make these checks more readable. Next commits will add more helpers and checks. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
9a2fb147f2
commit
56cef1e504
@ -1478,15 +1478,22 @@ enum child_state {
|
|||||||
GIT_CP_WAIT_CLEANUP,
|
GIT_CP_WAIT_CLEANUP,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct parallel_child {
|
||||||
|
enum child_state state;
|
||||||
|
struct child_process process;
|
||||||
|
struct strbuf err;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int child_is_working(const struct parallel_child *pp_child)
|
||||||
|
{
|
||||||
|
return pp_child->state == GIT_CP_WORKING;
|
||||||
|
}
|
||||||
|
|
||||||
struct parallel_processes {
|
struct parallel_processes {
|
||||||
size_t nr_processes;
|
size_t nr_processes;
|
||||||
|
|
||||||
struct {
|
struct parallel_child *children;
|
||||||
enum child_state state;
|
|
||||||
struct child_process process;
|
|
||||||
struct strbuf err;
|
|
||||||
void *data;
|
|
||||||
} *children;
|
|
||||||
/*
|
/*
|
||||||
* The struct pollfd is logically part of *children,
|
* The struct pollfd is logically part of *children,
|
||||||
* but the system call expects it as its own array.
|
* but the system call expects it as its own array.
|
||||||
@ -1509,7 +1516,7 @@ static void kill_children(const struct parallel_processes *pp,
|
|||||||
int signo)
|
int signo)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < opts->processes; i++)
|
for (size_t i = 0; i < opts->processes; i++)
|
||||||
if (pp->children[i].state == GIT_CP_WORKING)
|
if (child_is_working(&pp->children[i]))
|
||||||
kill(pp->children[i].process.pid, signo);
|
kill(pp->children[i].process.pid, signo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1665,7 +1672,7 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
|
|||||||
|
|
||||||
/* Buffer output from all pipes. */
|
/* Buffer output from all pipes. */
|
||||||
for (size_t i = 0; i < opts->processes; i++) {
|
for (size_t i = 0; i < opts->processes; i++) {
|
||||||
if (pp->children[i].state == GIT_CP_WORKING &&
|
if (child_is_working(&pp->children[i]) &&
|
||||||
pp->pfd[i].revents & (POLLIN | POLLHUP)) {
|
pp->pfd[i].revents & (POLLIN | POLLHUP)) {
|
||||||
int n = strbuf_read_once(&pp->children[i].err,
|
int n = strbuf_read_once(&pp->children[i].err,
|
||||||
pp->children[i].process.err, 0);
|
pp->children[i].process.err, 0);
|
||||||
@ -1683,7 +1690,7 @@ static void pp_output(const struct parallel_processes *pp)
|
|||||||
{
|
{
|
||||||
size_t i = pp->output_owner;
|
size_t i = pp->output_owner;
|
||||||
|
|
||||||
if (pp->children[i].state == GIT_CP_WORKING &&
|
if (child_is_working(&pp->children[i]) &&
|
||||||
pp->children[i].err.len) {
|
pp->children[i].err.len) {
|
||||||
strbuf_write(&pp->children[i].err, stderr);
|
strbuf_write(&pp->children[i].err, stderr);
|
||||||
strbuf_reset(&pp->children[i].err);
|
strbuf_reset(&pp->children[i].err);
|
||||||
@ -1748,7 +1755,7 @@ static int pp_collect_finished(struct parallel_processes *pp,
|
|||||||
* running process time.
|
* running process time.
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
|
if (child_is_working(&pp->children[(pp->output_owner + i) % n]))
|
||||||
break;
|
break;
|
||||||
pp->output_owner = (pp->output_owner + i) % n;
|
pp->output_owner = (pp->output_owner + i) % n;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user