fix multiple main thread actors not working

This commit is contained in:
2025-06-06 21:26:29 -05:00
parent 1a61ae6f77
commit 9f6d27fb3c
6 changed files with 45 additions and 28 deletions

View File

@@ -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
}
})
})
log.console("main prosperon end")

View File

@@ -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');

View File

@@ -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.");
})()

View File

@@ -11,13 +11,6 @@
#include <sys/stat.h>
#include <time.h>
#include <unistd.h> // pipe(), read(), write()
#include <fcntl.h> // fcntl(), O_NONBLOCK
#include <sys/select.h> // pselect(), fd_set, FD_*
#include <errno.h> // errno
#include <stdio.h> // perror(), printf()
#include <stdlib.h> // exit()
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
@@ -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

View File

@@ -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[] = {

View File

@@ -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());