From 5705b2f6c7891142e177c0e23713e751b22b02ac Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Wed, 9 Apr 2025 10:00:14 +0900 Subject: [PATCH 1/2] Fix absence of pthread_cancel in Android --- src/core/ev.c | 21 +++++++++++++++++++++ src/include/janet.h | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/src/core/ev.c b/src/core/ev.c index c6e99d6b4..8f97b8ee0 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -652,6 +652,12 @@ static VOID CALLBACK janet_timeout_stop(ULONG_PTR ptr) { UNREFERENCED_PARAMETER(ptr); ExitThread(0); } +#elif JANET_ANDROID +static void janet_timeout_stop(int sig_num) { + if(sig_num == SIGUSR1) { + pthread_exit(0); + } +} #endif static void janet_timeout_cb(JanetEVGenericMessage msg) { @@ -673,6 +679,14 @@ static DWORD WINAPI janet_timeout_body(LPVOID ptr) { } #else static void *janet_timeout_body(void *ptr) { +#ifdef JANET_ANDROID + struct sigaction action; + memset(&action, 0, sizeof(action)); + sigemptyset(&action.sa_mask); + action.sa_flags = 0; + action.sa_handler = &janet_timeout_stop; + sigaction(SIGUSR1, &action, NULL); +#endif JanetThreadedTimeout tto = *(JanetThreadedTimeout *)ptr; janet_free(ptr); struct timespec ts; @@ -1489,8 +1503,12 @@ JanetFiber *janet_loop1(void) { QueueUserAPC(janet_timeout_stop, to.worker, 0); WaitForSingleObject(to.worker, INFINITE); CloseHandle(to.worker); +#else +#ifdef JANET_ANDROID + pthread_kill(to.worker, SIGUSR1); #else pthread_cancel(to.worker); +#endif void *res; pthread_join(to.worker, &res); #endif @@ -3188,6 +3206,9 @@ JANET_CORE_FN(cfun_ev_deadline, to.is_error = 0; to.sched_id = to.fiber->sched_id; if (use_interrupt) { +#ifdef JANET_ANDROID + janet_sandbox_assert(JANET_SANDBOX_SIGNAL); +#endif JanetThreadedTimeout *tto = janet_malloc(sizeof(JanetThreadedTimeout)); if (NULL == tto) { JANET_OUT_OF_MEMORY; diff --git a/src/include/janet.h b/src/include/janet.h index 6aa912df7..1954af7b2 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -67,6 +67,11 @@ extern "C" { #define JANET_LINUX 1 #endif +/* Check for Android */ +#ifdef __ANDROID__ +#define JANET_ANDROID 1 +#endif + /* Check for Cygwin */ #if defined(__CYGWIN__) #define JANET_CYGWIN 1 From 6254fffad08d61b0d1fa7ac1e295eb161c1d45db Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Wed, 9 Apr 2025 10:38:59 +0900 Subject: [PATCH 2/2] Update changelog to include change to `ev/deadline` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0417c1420..d55b9d41d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. - Make `ffi/write` append to a buffer instead of insert at 0 by default. - Add `os/getpid` to get the current process id. - Add `:out` option to `os/spawn` to be able to redirect stderr to stdout with pipes. + Add `interrupt?` argument to `ev/deadline` to use VM interruptions. ## 1.38.0 - 2025-03-18 - Add `bundle/replace`