X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fccanlint%2Fasync.c;h=d867079d0782c4f341e9af443517494f52f83c78;hp=32a67d29add913cbc2c4ae3f557b91534d87456b;hb=79ac0049672b1f27b1a7121d06cc38721f76b8e1;hpb=0ce66a11534211efcddc6f7f1be0ccad38d2258f diff --git a/tools/ccanlint/async.c b/tools/ccanlint/async.c index 32a67d29..d867079d 100644 --- a/tools/ccanlint/async.c +++ b/tools/ccanlint/async.c @@ -12,9 +12,7 @@ #include #include #include -#include #include -#include static struct lbalance *lb; TLIST_TYPE(command, struct command); @@ -47,10 +45,12 @@ static void run_more(void) while (num_running < lbalance_target(lb)) { int p[2]; - c = tlist_top(&pending, struct command, list); + + c = tlist_top(&pending, list); if (!c) break; + fflush(stdout); if (pipe(p) != 0) err(1, "Pipe failed"); c->pid = fork(); @@ -68,7 +68,7 @@ static void run_more(void) signal(SIGALRM, killme); itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0; - itim.it_value = time_from_msec(c->time_ms); + itim.it_value = timespec_to_timeval(time_from_msec(c->time_ms).ts); setitimer(ITIMER_REAL, &itim, NULL); c->status = system(c->command); @@ -90,7 +90,7 @@ static void run_more(void) } } -static int destroy_command(struct command *command) +static void destroy_command(struct command *command) { if (!command->done && command->pid) { kill(-command->pid, SIGKILL); @@ -99,7 +99,6 @@ static int destroy_command(struct command *command) } tlist_del(command, list); - return 0; } void run_command_async(const void *ctx, unsigned int time_ms, @@ -113,17 +112,18 @@ void run_command_async(const void *ctx, unsigned int time_ms, if (!lb) lb = lbalance_new(); - command = talloc(ctx, struct command); + command = tal(ctx, struct command); command->ctx = ctx; command->time_ms = time_ms; command->pid = 0; - command->output = talloc_strdup(command, ""); + /* We want to track length, so don't use tal_strdup */ + command->output = tal_arrz(command, char, 1); va_start(ap, fmt); - command->command = talloc_vasprintf(command, fmt, ap); + command->command = tal_vfmt(command, fmt, ap); va_end(ap); tlist_add_tail(&pending, command, list); command->done = false; - talloc_set_destructor(command, destroy_command); + tal_add_destructor(command, destroy_command); run_more(); } @@ -148,14 +148,14 @@ static void reap_output(void) tlist_for_each_safe(&running, c, next, list) { if (FD_ISSET(c->output_fd, &in)) { int old_len, len; - old_len = talloc_array_length(c->output); - c->output = talloc_realloc(c, c->output, char, - old_len + 1024); - len = read(c->output_fd, c->output + old_len, 1024); + /* This length includes nul terminator! */ + old_len = tal_count(c->output); + tal_resize(&c->output, old_len + 1024); + len = read(c->output_fd, c->output + old_len - 1, 1024); if (len < 0) err(1, "Reading from async command"); - c->output = talloc_realloc(c, c->output, char, - old_len + len); + tal_resize(&c->output, old_len + len); + c->output[old_len + len - 1] = '\0'; if (len == 0) { struct rusage ru; wait4(c->pid, &c->status, 0, &ru); @@ -185,7 +185,7 @@ void *collect_command(bool *ok, char **output) struct command *c; const void *ctx; - while ((c = tlist_top(&done, struct command, list)) == NULL) { + while ((c = tlist_top(&done, list)) == NULL) { if (tlist_empty(&pending) && tlist_empty(&running)) return NULL; reap_output(); @@ -194,7 +194,22 @@ void *collect_command(bool *ok, char **output) *ok = (WIFEXITED(c->status) && WEXITSTATUS(c->status) == 0); ctx = c->ctx; - *output = talloc_steal(ctx, c->output); - talloc_free(c); + *output = tal_steal(ctx, c->output); + tal_free(c); return (void *)ctx; } + +/* Compile and link single C file, with object files, async. */ +void compile_and_link_async(const void *ctx, unsigned int time_ms, + const char *cfile, const char *ccandir, + const char *objs, const char *compiler, + const char *cflags, + const char *libs, const char *outfile) +{ + if (compile_verbose) + printf("Compiling and linking (async) %s\n", outfile); + run_command_async(ctx, time_ms, + "%s %s -I%s -o %s %s %s %s", + compiler, cflags, + ccandir, outfile, cfile, objs, libs); +}