From 3f1d714e7bf188b179d5b9da4e0fd4af201c9ac7 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 15 Apr 2024 20:59:42 -0500 Subject: [PATCH] sleep thread after panicking --- src/log.c | 3 ++- src/sys_win32.c | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/log.c b/src/log.c index a201f0eb..d078a830 100644 --- a/src/log.c +++ b/src/log.c @@ -109,8 +109,9 @@ void _log_panic(struct string msg) if (!atomic_i32_eval(&G.initialized)) { return; } if (G.file_valid) { - sys_file_write(G.file, BUFFER_FROM_STRING(STR("** PANICKING **\n"))); + sys_file_write(G.file, BUFFER_FROM_STRING(STR("******** PANICKING ********\n"))); sys_file_write(G.file, BUFFER_FROM_STRING(msg)); + sys_file_write(G.file, BUFFER_FROM_STRING(STR("\n***************************\n"))); } } diff --git a/src/sys_win32.c b/src/sys_win32.c index 49c754a8..d7cecc31 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -101,6 +101,7 @@ GLOBAL struct { LARGE_INTEGER timer_start; i32 scheduler_period_ms; DWORD thread_tls_index; + u32 main_thread_id; /* Panic */ struct atomic_i32 panicking; @@ -1859,6 +1860,11 @@ void sys_panic(struct string msg) WRITE_BARRIER(); SetEvent(G.panic_event); + + /* Wait for thread to be terminated */ + if (GetCurrentThreadId() != G.main_thread_id) { + Sleep(INFINITE); + } } } @@ -1995,6 +2001,7 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, * Win32 setup * ========================== */ + G.main_thread_id = GetCurrentThreadId(); SetThreadDescription(GetCurrentThread(), L"Main thread"); /* Set up panic event */ @@ -2085,8 +2092,10 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, /* Call app thread and wait for return */ { + /* Start app thread */ struct sys_thread app_thread = sys_thread_alloc(&app_thread_entry_point, NULL, STR("[P9] App thread")); + /* Get app thread handle */ HANDLE app_thread_handle = 0; sys_rw_mutex_lock_shared(&G.threads_rw_mutex); { @@ -2095,7 +2104,7 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, } sys_rw_mutex_unlock_shared(&G.threads_rw_mutex); - /* Wait for thread exit or panic */ + /* Wait for either app thread exit or panic */ if (app_thread_handle) { HANDLE wait_handles[] = { app_thread_handle,