From 77c972e56b55deabe003680ea82a541bf84bebae Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 28 Jan 2023 11:30:32 +0000 Subject: [PATCH] Fix crash after ctrl-C on Windows --- src/thread.c | 15 +++++++++++---- src/thread.h | 1 + src/util.c | 4 +++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/thread.c b/src/thread.c index afb4d911..0aafdeb9 100644 --- a/src/thread.c +++ b/src/thread.c @@ -72,12 +72,14 @@ typedef struct { STATIC_ASSERT(sizeof(lock_stats_t) == 64) #ifdef DEBUG -#define LOCK_EVENT(what, n) do { \ - lock_stats[my_thread->id].what += (n); \ +#define LOCK_EVENT(what, n) do { \ + if (likely(my_thread != NULL)) \ + lock_stats[my_thread->id].what += (n); \ } while (0) -#define WORKQ_EVENT(what, n) do { \ - workq_stats[my_thread->id].what += (n); \ +#define WORKQ_EVENT(what, n) do { \ + if (likely(my_thread != NULL)) \ + workq_stats[my_thread->id].what += (n); \ } while (0) #else #define LOCK_EVENT(what, n) @@ -423,6 +425,11 @@ int thread_id(void) return my_thread->id; } +bool thread_attached(void) +{ + return my_thread != NULL; +} + void thread_sleep(int usec) { usleep(usec); diff --git a/src/thread.h b/src/thread.h index 87d320fe..28c1c1c3 100644 --- a/src/thread.h +++ b/src/thread.h @@ -51,6 +51,7 @@ typedef struct _nvc_thread nvc_thread_t; void thread_init(void); int thread_id(void); +bool thread_attached(void); void thread_sleep(int usec); typedef void *(*thread_fn_t)(void *); diff --git a/src/util.c b/src/util.c index f3f64aba..49a07b84 100644 --- a/src/util.c +++ b/src/util.c @@ -461,7 +461,9 @@ bool color_terminal(void) void fatal_exit(int status) { - if (atomic_load(&crashing) != SIG_ATOMIC_MAX || thread_id() != 0) + if (atomic_load(&crashing) != SIG_ATOMIC_MAX) + _exit(status); // Exit during crash + else if (!thread_attached() || thread_id() != 0) _exit(status); else exit(status); -- 2.39.2