From 1d5058d2677f74a331c1ef8808932a4494929ea3 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 2 Apr 2020 00:11:47 +0900 Subject: [PATCH] [qwaq] Move progs create and load into the threads While this does answer the question of how I'll go about restarting the target progs (when I get to that point), it was required just to start full-on ruamoko progs because .ctor was getting run in the main thread and blocking due to trace. --- ruamoko/qwaq/builtins/main.c | 20 +++++++++++++------- ruamoko/qwaq/qwaq.h | 4 ++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ruamoko/qwaq/builtins/main.c b/ruamoko/qwaq/builtins/main.c index c78a9f9e5..9077054b2 100644 --- a/ruamoko/qwaq/builtins/main.c +++ b/ruamoko/qwaq/builtins/main.c @@ -140,19 +140,22 @@ init_qf (void) Cvar_Get ("pr_debug", "2", 0, 0, 0); Cvar_Get ("pr_boundscheck", "0", 0, 0, 0); + // Normally, this is done by PR_Init, but PR_Init is not called in the main + // thread. However, PR_Opcode_Init() is idempotent. + PR_Opcode_Init (); } -typedef void (*progsinit_f) (progs_t *pr); - static progs_t * -create_progs (progsinit_f *funcs) +create_progs (qwaq_thread_t *thread) { progs_t *pr = calloc (1, sizeof (*pr)); + progsinit_f *funcs = thread->progsinit; pr->load_file = load_file; pr->allocate_progs_mem = allocate_progs_mem; pr->free_progs_mem = free_progs_mem; pr->no_exec_limit = 1; + pr->hashlink_freelist = &thread->hashlink_freelist; PR_Init_Cvars (); PR_Init (pr); @@ -185,7 +188,7 @@ load_progs (progs_t *pr, const char *name) } static void -spawn_progs (qwaq_thread_t *thread, progsinit_f *funcs) +spawn_progs (qwaq_thread_t *thread) { dfunction_t *dfunc; const char *name = 0; @@ -194,7 +197,7 @@ spawn_progs (qwaq_thread_t *thread, progsinit_f *funcs) progs_t *pr; thread->main_func = 0; - pr = thread->pr = create_progs (funcs); + pr = thread->pr = create_progs (thread); if (thread->args.size) { name = thread->args.a[0]; } @@ -230,6 +233,9 @@ run_progs (void *data) { __auto_type thread = (qwaq_thread_t *) data; + spawn_progs (thread); + Sys_Printf ("starthing thread for %s\n", thread->args.a[0]); + PR_ExecuteProgram (thread->pr, thread->main_func); PR_PopFrame (thread->pr); if (thread->pr->debug_handler) { @@ -452,13 +458,13 @@ main (int argc, char **argv) main_ind = i; app_funcs = main_app; } - spawn_progs (thread, app_funcs); + thread->progsinit = app_funcs; } if (main_ind >= 0) { // threads might start new threads before the end is reached size_t count = thread_data.size; for (size_t i = 0; i < count; i++) { - if (thread_data.a[i]->pr && thread_data.a[i]->main_func) { + if (thread_data.a[i]->progsinit) { start_progs_thread (thread_data.a[i]); } } diff --git a/ruamoko/qwaq/qwaq.h b/ruamoko/qwaq/qwaq.h index da9ec6e98..43efc4700 100644 --- a/ruamoko/qwaq/qwaq.h +++ b/ruamoko/qwaq/qwaq.h @@ -5,12 +5,16 @@ #include "QF/progs.h" #include "QF/sys.h" +typedef void (*progsinit_f) (progs_t *pr); + typedef struct qwaq_thread_s { pthread_t thread_id; int return_code; struct DARRAY_TYPE (const char *) args; sys_printf_t sys_printf; + progsinit_f*progsinit; progs_t *pr; + struct hashlink_s *hashlink_freelist; func_t main_func; void *data; } qwaq_thread_t;