diff --git a/source/cell.c b/source/cell.c index b04df5a4..c4bb13ed 100644 --- a/source/cell.c +++ b/source/cell.c @@ -66,7 +66,8 @@ static unsigned char *zip_buffer_global = NULL; static char *prosperon = NULL; cell_rt *root_cell = NULL; -SDL_AtomicInt waiting_threads; +static SDL_AtomicInt waiting_threads; +static SDL_AtomicInt shutting_down; static SDL_Thread **runners = NULL; @@ -77,8 +78,13 @@ static inline uint64_t now_ns() static void exit_handler(void) { + SDL_LockMutex(queue_mutex); /* 1. take the lock */ + SDL_SetAtomicInt(&shutting_down, 1); /* 2. store *inside* CS */ + SDL_BroadcastCondition(queue_cond); /* 3. wake the waiters */ + SDL_UnlockMutex(queue_mutex); /* 4. release – H-B created */ + SDL_SetAtomicInt(&shutting_down, 1); + int status; - SDL_BroadcastCondition(queue_cond); for (int i = 0; i < arrlen(runners); i++) SDL_WaitThread(runners[i], &status); @@ -720,7 +726,7 @@ int uncaught_exception(JSContext *js, JSValue v) static int actor_runner(void *data) { - while (1) { + while (!SDL_GetAtomicInt(&shutting_down)) { SDL_LockMutex(queue_mutex); cell_rt *actor = NULL; for (int i = 0; i < arrlen(ready_queue); i++) { @@ -739,7 +745,7 @@ static int actor_runner(void *data) SDL_WaitCondition(queue_cond, queue_mutex); SDL_AddAtomicInt(&waiting_threads, -1); SDL_UnlockMutex(queue_mutex); - continue; + if (SDL_GetAtomicInt(&shutting_down)) return 0; } } return 0; @@ -747,7 +753,6 @@ static int actor_runner(void *data) static void signal_handler(int sig) { - exit_handler(); const char *str = NULL; switch (sig) { case SIGABRT: str = "SIGABRT"; break; @@ -780,12 +785,13 @@ static void loop() queue_cond = SDL_CreateCondition(); actors_mutex = SDL_CreateMutex(); SDL_SetAtomicInt(&waiting_threads, 0); + SDL_SetAtomicInt(&shutting_down, 0); timer_init(); add_runners(SDL_GetNumLogicalCPUCores()-1); - while (1) { + while (!SDL_GetAtomicInt(&shutting_down)) { process_due_timers(); SDL_LockMutex(queue_mutex); @@ -814,7 +820,7 @@ static void loop() } SDL_LockMutex(queue_mutex); - SDL_WaitConditionTimeout(queue_cond, queue_mutex, 100); + SDL_WaitConditionTimeout(queue_cond, queue_mutex, to_ns); SDL_UnlockMutex(queue_mutex); } } @@ -936,3 +942,12 @@ int actor_exists(const char *id) else return 1; } + +int JS_ArrayLength(JSContext *js, JSValue a) +{ + JSValue length = JS_GetPropertyStr(js, a, "length"); + int len; + JS_ToInt32(js,&len,length); + JS_FreeValue(js,length); + return len; +} \ No newline at end of file