From d908016606b3b67d389db144b3395fd7e367f2df Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 5 Dec 2023 18:54:57 +0900 Subject: [PATCH] [sys] Use a jump buffer to exit to main This allows tracy to clean up properly. However, Sys_Quit will use the jump buffer (sys_exit_jmpbuf) only if it has been set, so the use of Sys_setjmp is optional. --- include/QF/sys.h | 2 ++ libs/util/sys.c | 5 +++++ nq/source/sys_unix.c | 4 ++++ nq/source/sys_unixd.c | 4 ++++ nq/source/sys_win.c | 4 ++++ nq/source/sys_wind.c | 4 ++++ qw/source/cl_sys_unix.c | 4 ++++ qw/source/cl_sys_win.c | 4 ++++ qw/source/sv_sys_unix.c | 4 ++++ qw/source/sv_sys_win.c | 4 ++++ 10 files changed, 39 insertions(+) diff --git a/include/QF/sys.h b/include/QF/sys.h index 5a71b7ae7..ee804245d 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -206,6 +206,8 @@ typedef intptr_t sys_jmpbuf[5]; #define Sys_setjmp(jmpbuf) __builtin_setjmp(jmpbuf) void Sys_longjmp (sys_jmpbuf jmpbuf) __attribute__((noreturn)); +extern sys_jmpbuf sys_exit_jmpbuf; + ///@} #endif//__QF_sys_h diff --git a/libs/util/sys.c b/libs/util/sys.c index 7643c75c3..fe12c1d2b 100644 --- a/libs/util/sys.c +++ b/libs/util/sys.c @@ -540,11 +540,16 @@ Sys_Init_Cvars (void) Cvar_Register (&sys_sleep_cvar, 0, 0); } +static sys_jmpbuf sys_exit_cmpbuf; +sys_jmpbuf sys_exit_jmpbuf; VISIBLE void Sys_Quit (void) { Sys_Shutdown (); + if (memcmp (sys_exit_cmpbuf, sys_exit_jmpbuf, sizeof (sys_exit_cmpbuf))) { + Sys_longjmp (sys_exit_jmpbuf); + } exit (0); } diff --git a/nq/source/sys_unix.c b/nq/source/sys_unix.c index 0b575a14e..91244e71d 100644 --- a/nq/source/sys_unix.c +++ b/nq/source/sys_unix.c @@ -68,6 +68,10 @@ main (int argc, const char **argv) { double time, oldtime, newtime; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + memset (&host_parms, 0, sizeof (host_parms)); COM_InitArgv (argc, argv); diff --git a/nq/source/sys_unixd.c b/nq/source/sys_unixd.c index e23a713a0..52f3820b8 100644 --- a/nq/source/sys_unixd.c +++ b/nq/source/sys_unixd.c @@ -66,6 +66,10 @@ main (int argc, const char **argv) double time, oldtime, newtime; int i; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + memset (&host_parms, 0, sizeof (host_parms)); // dedicated server ONLY! diff --git a/nq/source/sys_win.c b/nq/source/sys_win.c index 4151b069c..5aa17f38b 100644 --- a/nq/source/sys_win.c +++ b/nq/source/sys_win.c @@ -163,6 +163,10 @@ WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, if (hPrevInstance) return 0; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + startup (); global_hInstance = hInstance; diff --git a/nq/source/sys_wind.c b/nq/source/sys_wind.c index 8ab700325..9ef056841 100644 --- a/nq/source/sys_wind.c +++ b/nq/source/sys_wind.c @@ -49,6 +49,10 @@ main (int argc, const char **argv) double time, oldtime, newtime; int i; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + memset (&host_parms, 0, sizeof (host_parms)); // dedicated server ONLY! diff --git a/qw/source/cl_sys_unix.c b/qw/source/cl_sys_unix.c index 956be135e..1f53d39f4 100644 --- a/qw/source/cl_sys_unix.c +++ b/qw/source/cl_sys_unix.c @@ -65,6 +65,10 @@ main (int argc, const char **argv) { double time, oldtime, newtime; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + memset (&host_parms, 0, sizeof (host_parms)); COM_InitArgv (argc, argv); diff --git a/qw/source/cl_sys_win.c b/qw/source/cl_sys_win.c index 959a3b58a..cd22eb429 100644 --- a/qw/source/cl_sys_win.c +++ b/qw/source/cl_sys_win.c @@ -127,6 +127,10 @@ WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, if (hPrevInstance) return 0; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + startup (); global_hInstance = hInstance; diff --git a/qw/source/sv_sys_unix.c b/qw/source/sv_sys_unix.c index 96b0c67b9..ee61caffc 100644 --- a/qw/source/sv_sys_unix.c +++ b/qw/source/sv_sys_unix.c @@ -112,6 +112,10 @@ main (int argc, const char **argv) { double time, oldtime, newtime; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + startup (); memset (&host_parms, 0, sizeof (host_parms)); diff --git a/qw/source/sv_sys_win.c b/qw/source/sv_sys_win.c index ed907dee9..a24048085 100644 --- a/qw/source/sv_sys_win.c +++ b/qw/source/sv_sys_win.c @@ -70,6 +70,10 @@ main (int argc, const char **argv) { double time, oldtime, newtime; + if (Sys_setjmp (sys_exit_jmpbuf)) { + exit (0); + } + startup (); memset (&host_parms, 0, sizeof (host_parms));