From 9f6d27fb3ca782d68aa03edfc29180bbd5d5348b Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 6 Jun 2025 21:26:29 -0500 Subject: [PATCH] fix multiple main thread actors not working --- prosperon/prosperon.ce | 6 ++++- prosperon/sdl_video.ce | 2 ++ scripts/engine.cm | 3 +++ source/cell.c | 58 ++++++++++++++++++++++-------------------- source/qjs_actor.c | 2 ++ source/qjs_sdl_video.c | 2 ++ 6 files changed, 45 insertions(+), 28 deletions(-) diff --git a/prosperon/prosperon.ce b/prosperon/prosperon.ce index 62e20738..30446c7b 100644 --- a/prosperon/prosperon.ce +++ b/prosperon/prosperon.ce @@ -27,6 +27,8 @@ $_.start(e => { height:500 }) +log.console('starting...') + var input = use('input') var geometry = use('geometry') @@ -297,4 +299,6 @@ $_.receiver(e => { if (e.d_pos) e.d_pos.y *= -1 } -}) \ No newline at end of file +}) + +log.console("main prosperon end") \ No newline at end of file diff --git a/prosperon/sdl_video.ce b/prosperon/sdl_video.ce index 43024386..4f1aa319 100644 --- a/prosperon/sdl_video.ce +++ b/prosperon/sdl_video.ce @@ -1,5 +1,7 @@ var video = use('sdl_video'); +log.console("STATED VIDE") + // SDL Video Actor // This actor runs on the main thread and handles all SDL video operations var surface = use('surface'); diff --git a/scripts/engine.cm b/scripts/engine.cm index 95e2e286..f8e83e05 100644 --- a/scripts/engine.cm +++ b/scripts/engine.cm @@ -820,10 +820,13 @@ var prog_script = `(function ${cell.args.program.name()}_start($_, arg) { var ar var startfn = js.eval(cell.args.program, prog_script); $_.clock(_ => { + log.console(`actor %{cell.id} is now running its program.`) var val = startfn($_, cell.args.arg); if (val) throw new Error('Program must not return anything'); }) +log.console("end. set a clock."); + })() \ No newline at end of file diff --git a/source/cell.c b/source/cell.c index b76ac3f6..fd0c273a 100644 --- a/source/cell.c +++ b/source/cell.c @@ -11,13 +11,6 @@ #include #include -#include // pipe(), read(), write() -#include // fcntl(), O_NONBLOCK -#include // pselect(), fd_set, FD_* -#include // errno -#include // perror(), printf() -#include // exit() - #ifdef __APPLE__ #include #include @@ -517,9 +510,7 @@ void actor_turn(cell_rt *actor) } // If there are no waiting threads, bail. otherwise, try for another turn - if (SDL_GetAtomicInt(&waiting_threads) == 0) - goto ENDTURN; - else + if (SDL_GetAtomicInt(&waiting_threads) > 0) goto TAKETURN; ENDTURN: @@ -704,6 +695,7 @@ void script_startup(cell_rt *prt) JSValue v = JS_Eval(js, data, (size_t)stat.filesize, ENGINE, JS_EVAL_FLAG_STRICT); uncaught_exception(js, v); prt->state = ACTOR_IDLE; + set_actor_state(prt); } int uncaught_exception(JSContext *js, JSValue v) @@ -731,9 +723,17 @@ static int actor_runner(void *data) while (1) { SDL_LockMutex(queue_mutex); cell_rt *actor = NULL; - if (arrlen(ready_queue) > 0) { - actor = ready_queue[0]; - arrdel(ready_queue, 0); + for (int i = 0; i < arrlen(ready_queue); i++) { + if (!ready_queue[i]->main_thread_only) { + actor = ready_queue[i]; + arrdel(ready_queue,i); + break; + } + } + + if (actor) { + SDL_UnlockMutex(queue_mutex); + actor_turn(actor); } else { SDL_AddAtomicInt(&waiting_threads, 1); SDL_WaitCondition(queue_cond, queue_mutex); @@ -741,9 +741,6 @@ static int actor_runner(void *data) SDL_UnlockMutex(queue_mutex); continue; } - SDL_UnlockMutex(queue_mutex); - - if (actor) actor_turn(actor); } return 0; } @@ -778,24 +775,26 @@ static void add_runners(int n) static void loop() { -/* Initialize synchronization primitives */ -queue_mutex = SDL_CreateMutex(); -queue_cond = SDL_CreateCondition(); -actors_mutex = SDL_CreateMutex(); -SDL_SetAtomicInt(&waiting_threads, 0); - -timer_init(); - -add_runners(SDL_GetNumLogicalCPUCores()-1); + /* Initialize synchronization primitives */ + queue_mutex = SDL_CreateMutex(); + queue_cond = SDL_CreateCondition(); + actors_mutex = SDL_CreateMutex(); + SDL_SetAtomicInt(&waiting_threads, 0); + + timer_init(); + + add_runners(SDL_GetNumLogicalCPUCores()-1); while (1) { process_due_timers(); SDL_LockMutex(queue_mutex); cell_rt *actor = NULL; + printf("queue check? %d\n", arrlen(ready_queue)); for (int i = 0; i < arrlen(ready_queue); i++) { if (ready_queue[i]->main_thread_only) { actor = ready_queue[i]; + printf("picking up actor %s\n", actor->id); arrdel(ready_queue,i); break; } @@ -803,6 +802,8 @@ add_runners(SDL_GetNumLogicalCPUCores()-1); SDL_UnlockMutex(queue_mutex); if (actor) { + printf("running %s\n", actor->id); + printf("message is %d\n", actor->letters[0].type); actor_turn(actor); continue; } @@ -813,11 +814,12 @@ add_runners(SDL_GetNumLogicalCPUCores()-1); // No more timers - hence, no more actors ... exit if single threaded. } + printf("waiting for a timeout ...\n"); SDL_LockMutex(queue_mutex); - SDL_WaitConditionTimeout(queue_cond, queue_mutex, to_ns/1000000); + SDL_WaitConditionTimeout(queue_cond, queue_mutex, 100); SDL_UnlockMutex(queue_mutex); + printf("something happened ...\n"); } - } int main(int argc, char **argv) @@ -835,6 +837,8 @@ int main(int argc, char **argv) exit(1); } + printf("main thread %d\n", SDL_GetThreadID(NULL)); + #ifdef TRACY_ENABLE tracy_profiling_enabled = profile_enabled; #endif diff --git a/source/qjs_actor.c b/source/qjs_actor.c index 0299af6d..3e665f65 100644 --- a/source/qjs_actor.c +++ b/source/qjs_actor.c @@ -174,6 +174,8 @@ JSC_CCALL(actor_clock, l.callback = JS_DupValue(js, argv[0]); arrput(actor->letters, l); SDL_UnlockMutex(actor->msg_mutex); + printf("actor %s clocked\n", actor->id); + set_actor_state(actor); ) static const JSCFunctionListEntry js_actor_funcs[] = { diff --git a/source/qjs_sdl_video.c b/source/qjs_sdl_video.c index 1f7bf330..9a22c9c8 100644 --- a/source/qjs_sdl_video.c +++ b/source/qjs_sdl_video.c @@ -1747,6 +1747,8 @@ JSC_CCALL(sdl_createWindowAndRenderer, #include "qjs_wota.h" JSValue js_sdl_video_use(JSContext *js) { + printf("initing on thread %d\n", SDL_GetThreadID(NULL)); + if (!SDL_Init(SDL_INIT_VIDEO)) return JS_ThrowInternalError(js, "Unable to initialize video subsystem: %s", SDL_GetError());