mirror of
https://github.com/git/git.git
synced 2026-04-05 06:20:09 +02:00
Implement thread-specific die() routines; use one in start_async().
This commit is contained in:
committed by
Johannes Sixt
parent
4eb0463971
commit
85763c86da
32
usage.c
32
usage.c
@@ -34,16 +34,38 @@ static void warn_builtin(const char *warn, va_list params)
|
||||
vreport("warning: ", warn, params);
|
||||
}
|
||||
|
||||
typedef void (*die_fn_t)(const char *err, va_list params) NORETURN;
|
||||
|
||||
static DWORD tls_index;
|
||||
|
||||
static void tls_init(void) __attribute__((constructor));
|
||||
static void tls_init(void)
|
||||
{
|
||||
tls_index = TlsAlloc();
|
||||
}
|
||||
|
||||
struct routines {
|
||||
die_fn_t die_routine;
|
||||
};
|
||||
/* If we are in a dlopen()ed .so write to a global variable would segfault
|
||||
* (ugh), so keep things static. */
|
||||
static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
|
||||
static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
|
||||
static void (*error_routine)(const char *err, va_list params) = error_builtin;
|
||||
static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
|
||||
|
||||
void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
|
||||
{
|
||||
die_routine = routine;
|
||||
struct routines *r = TlsGetValue(tls_index);
|
||||
if (r == NULL) {
|
||||
/* avoid die()! */
|
||||
r = calloc(sizeof(*r), 1);
|
||||
if (r == NULL) {
|
||||
fprintf(stderr, "cannot allocate thread-local storage");
|
||||
return;
|
||||
}
|
||||
TlsSetValue(tls_index, r);
|
||||
}
|
||||
r->die_routine = routine;
|
||||
}
|
||||
|
||||
void usage(const char *err)
|
||||
@@ -54,9 +76,13 @@ void usage(const char *err)
|
||||
void die(const char *err, ...)
|
||||
{
|
||||
va_list params;
|
||||
struct routines *r = TlsGetValue(tls_index);
|
||||
|
||||
va_start(params, err);
|
||||
die_routine(err, params);
|
||||
if (r == NULL || r->die_routine == NULL)
|
||||
die_builtin(err, params);
|
||||
else
|
||||
r->die_routine(err, params);
|
||||
va_end(params);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user