Add JANET_ASYNC_EVENT_CANCEL

also fix bug that could cause event loop to hang.
This commit is contained in:
Calvin Rose 2020-11-16 18:46:59 -06:00
parent f9d0eb47b7
commit cff52ded58
3 changed files with 18 additions and 4 deletions

View File

@ -403,7 +403,10 @@ void janet_schedule(JanetFiber *fiber, Janet value) {
void janet_fiber_did_resume(JanetFiber *fiber) {
/* Cancel any pending fibers */
if (fiber->waiting) janet_unlisten(fiber->waiting);
if (fiber->waiting) {
fiber->waiting->machine(fiber->waiting, JANET_ASYNC_EVENT_CANCEL);
janet_unlisten(fiber->waiting);
}
}
/* Mark all pending tasks */
@ -786,8 +789,10 @@ void janet_loop1(void) {
while ((has_timeout = peek_timeout(&to)) && to.fiber->sched_id != to.sched_id) {
pop_timeout(0);
}
/* Run polling implementation */
janet_loop1_impl(has_timeout, to.when);
/* Run polling implementation only if pending timeouts or pending events */
if (janet_vm_tq_count || janet_vm_listener_count) {
janet_loop1_impl(has_timeout, to.when);
}
}
}

View File

@ -517,7 +517,7 @@ typedef enum {
JANET_ASYNC_EVENT_HUP,
JANET_ASYNC_EVENT_READ,
JANET_ASYNC_EVENT_WRITE,
JANET_ASYNC_EVENT_TIMEOUT,
JANET_ASYNC_EVENT_CANCEL,
JANET_ASYNC_EVENT_COMPLETE, /* Used on windows for IOCP */
JANET_ASYNC_EVENT_USER
} JanetAsyncEvent;

View File

@ -65,4 +65,13 @@
(ev/close writer)
(ev/take chan))
(var result nil)
(def fiber
(ev/spawn
(set result (protect (ev/sleep 0.4)))
(assert (= result '(false "boop")) "ev/cancel 1")))
(ev/sleep 0.1)
(ev/cancel fiber "boop")
(ev/sleep 0.1)
(end-suite)