1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 22:24:49 +00:00

Handle refcounts in posted events.

This commit is contained in:
Calvin Rose 2023-10-08 13:14:36 -05:00
parent 555e0c0b85
commit 7f852b8af4
2 changed files with 3 additions and 4 deletions

View File

@ -1412,6 +1412,7 @@ static void janet_ev_handle_selfpipe(void) {
while (read(janet_vm.selfpipe[0], &response, sizeof(response)) > 0) { while (read(janet_vm.selfpipe[0], &response, sizeof(response)) > 0) {
if (NULL != response.cb) { if (NULL != response.cb) {
response.cb(response.msg); response.cb(response.msg);
janet_ev_dec_refcount();
} }
} }
} }
@ -1472,6 +1473,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
if (NULL != response->cb) { if (NULL != response->cb) {
response->cb(response->msg); response->cb(response->msg);
} }
janet_ev_dec_refcount();
janet_free(response); janet_free(response);
} else { } else {
/* Normal event */ /* Normal event */
@ -1892,6 +1894,7 @@ void janet_ev_deinit(void) {
*/ */
void janet_ev_post_event(JanetVM *vm, JanetCallback cb, JanetEVGenericMessage msg) { void janet_ev_post_event(JanetVM *vm, JanetCallback cb, JanetEVGenericMessage msg) {
vm = vm ? vm : &janet_vm; vm = vm ? vm : &janet_vm;
janet_atomic_inc(&vm->listener_count);
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
JanetHandle iocp = vm->iocp; JanetHandle iocp = vm->iocp;
JanetSelfPipeEvent *event = janet_malloc(sizeof(JanetSelfPipeEvent)); JanetSelfPipeEvent *event = janet_malloc(sizeof(JanetSelfPipeEvent));

View File

@ -517,7 +517,6 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
/* Callback that is called in main thread when subroutine completes. */ /* Callback that is called in main thread when subroutine completes. */
static void janet_proc_wait_cb(JanetEVGenericMessage args) { static void janet_proc_wait_cb(JanetEVGenericMessage args) {
janet_ev_dec_refcount();
JanetProc *proc = (JanetProc *) args.argp; JanetProc *proc = (JanetProc *) args.argp;
if (NULL != proc) { if (NULL != proc) {
int status = args.tag; int status = args.tag;
@ -827,7 +826,6 @@ static void janet_signal_callback(JanetEVGenericMessage msg) {
JanetFunction *handler = janet_unwrap_function(handlerv); JanetFunction *handler = janet_unwrap_function(handlerv);
JanetFiber *fiber = janet_fiber(handler, 64, 0, NULL); JanetFiber *fiber = janet_fiber(handler, 64, 0, NULL);
janet_schedule_soon(fiber, janet_wrap_nil(), JANET_SIGNAL_OK); janet_schedule_soon(fiber, janet_wrap_nil(), JANET_SIGNAL_OK);
janet_ev_dec_refcount();
} }
static void janet_signal_trampoline_no_interrupt(int sig) { static void janet_signal_trampoline_no_interrupt(int sig) {
@ -836,7 +834,6 @@ static void janet_signal_trampoline_no_interrupt(int sig) {
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
msg.tag = sig; msg.tag = sig;
janet_ev_post_event(&janet_vm, janet_signal_callback, msg); janet_ev_post_event(&janet_vm, janet_signal_callback, msg);
janet_ev_inc_refcount();
} }
static void janet_signal_trampoline(int sig) { static void janet_signal_trampoline(int sig) {
@ -847,7 +844,6 @@ static void janet_signal_trampoline(int sig) {
msg.argi = 1; msg.argi = 1;
janet_interpreter_interrupt(NULL); janet_interpreter_interrupt(NULL);
janet_ev_post_event(&janet_vm, janet_signal_callback, msg); janet_ev_post_event(&janet_vm, janet_signal_callback, msg);
janet_ev_inc_refcount();
} }
#endif #endif