Skip to content

Commit

Permalink
run-command: provide in_async query function
Browse files Browse the repository at this point in the history
It's not easy for arbitrary code to find out whether it is
running in an async process or not. A top-level function
which is fed to start_async() can know (you just pass down
an argument saying "you are async"). But that function may
call other global functions, and we would not want to have
to pass the information all the way through the call stack.

Nor can we simply set a global variable, as those may be
shared between async threads and the main thread (if the
platform supports pthreads). We need pthread tricks _or_ a
global variable, depending on how start_async is
implemented.

The callers don't have enough information to do this right,
so let's provide a simple query function that does.
Fortunately we can reuse the existing infrastructure to make
the pthread case simple (and even simplify die_async() by
using our new function).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Sep 1, 2015
1 parent 3235983 commit 661a8cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion run-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ static NORETURN void die_async(const char *err, va_list params)
{
vreportf("fatal: ", err, params);

if (!pthread_equal(main_thread, pthread_self())) {
if (in_async()) {
struct async *async = pthread_getspecific(async_key);
if (async->proc_in >= 0)
close(async->proc_in);
Expand All @@ -627,6 +627,13 @@ static int async_die_is_recursing(void)
return ret != NULL;
}

int in_async(void)
{
if (!main_thread_set)
return 0; /* no asyncs started yet */
return !pthread_equal(main_thread, pthread_self());
}

#else

static struct {
Expand Down Expand Up @@ -666,6 +673,12 @@ int git_atexit(void (*handler)(void))
}
#define atexit git_atexit

static int process_is_async;
int in_async(void)
{
return process_is_async;
}

#endif

int start_async(struct async *async)
Expand Down Expand Up @@ -725,6 +738,7 @@ int start_async(struct async *async)
if (need_out)
close(fdout[0]);
git_atexit_clear();
process_is_async = 1;
exit(!!async->proc(proc_in, proc_out, async->data));
}

Expand Down
1 change: 1 addition & 0 deletions run-command.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,6 @@ struct async {

int start_async(struct async *async);
int finish_async(struct async *async);
int in_async(void);

#endif

0 comments on commit 661a8cf

Please sign in to comment.