Skip to content

Commit 661a8cf

Browse files
peffgitster
authored andcommitted
run-command: provide in_async query function
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3235983 commit 661a8cf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

run-command.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ static NORETURN void die_async(const char *err, va_list params)
608608
{
609609
vreportf("fatal: ", err, params);
610610

611-
if (!pthread_equal(main_thread, pthread_self())) {
611+
if (in_async()) {
612612
struct async *async = pthread_getspecific(async_key);
613613
if (async->proc_in >= 0)
614614
close(async->proc_in);
@@ -627,6 +627,13 @@ static int async_die_is_recursing(void)
627627
return ret != NULL;
628628
}
629629

630+
int in_async(void)
631+
{
632+
if (!main_thread_set)
633+
return 0; /* no asyncs started yet */
634+
return !pthread_equal(main_thread, pthread_self());
635+
}
636+
630637
#else
631638

632639
static struct {
@@ -666,6 +673,12 @@ int git_atexit(void (*handler)(void))
666673
}
667674
#define atexit git_atexit
668675

676+
static int process_is_async;
677+
int in_async(void)
678+
{
679+
return process_is_async;
680+
}
681+
669682
#endif
670683

671684
int start_async(struct async *async)
@@ -725,6 +738,7 @@ int start_async(struct async *async)
725738
if (need_out)
726739
close(fdout[0]);
727740
git_atexit_clear();
741+
process_is_async = 1;
728742
exit(!!async->proc(proc_in, proc_out, async->data));
729743
}
730744

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,6 @@ struct async {
113113

114114
int start_async(struct async *async);
115115
int finish_async(struct async *async);
116+
int in_async(void);
116117

117118
#endif

0 commit comments

Comments
 (0)