1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-03 10:32:53 +00:00

Don't call pthread cancel on normal exits.

Calling pthread_cancel on threads that can exit normally is not needed.
Instead, we immediately call pthread_join if a thread can exit normally.
This commit is contained in:
Calvin Rose 2025-05-18 09:50:27 -05:00
parent e355cb07e0
commit 92e91259c3
2 changed files with 2635 additions and 2631 deletions

View File

@ -617,8 +617,9 @@ static void janet_timeout_stop(int sig_num) {
} }
#endif #endif
static void handle_timeout_worker(JanetTimeout to) { static void handle_timeout_worker(JanetTimeout to, int cancel) {
if (!to.has_worker) return; if (!to.has_worker) return;
if (cancel) {
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
QueueUserAPC(janet_timeout_stop, to.worker, 0); QueueUserAPC(janet_timeout_stop, to.worker, 0);
WaitForSingleObject(to.worker, INFINITE); WaitForSingleObject(to.worker, INFINITE);
@ -630,6 +631,7 @@ static void handle_timeout_worker(JanetTimeout to) {
int ret = pthread_cancel(to.worker); int ret = pthread_cancel(to.worker);
janet_assert(!ret, "pthread_cancel"); janet_assert(!ret, "pthread_cancel");
#endif #endif
}
void *res = NULL; void *res = NULL;
janet_assert(!pthread_join(to.worker, &res), "pthread_join"); janet_assert(!pthread_join(to.worker, &res), "pthread_join");
#endif #endif
@ -639,7 +641,7 @@ static void handle_timeout_worker(JanetTimeout to) {
void janet_ev_deinit_common(void) { void janet_ev_deinit_common(void) {
JanetTimeout to; JanetTimeout to;
while (peek_timeout(&to)) { while (peek_timeout(&to)) {
handle_timeout_worker(to); handle_timeout_worker(to, 1);
pop_timeout(0); pop_timeout(0);
} }
janet_q_deinit(&janet_vm.spawn); janet_q_deinit(&janet_vm.spawn);
@ -1457,7 +1459,6 @@ JanetFiber *janet_loop1(void) {
JanetTimestamp now = ts_now(); JanetTimestamp now = ts_now();
while (peek_timeout(&to) && to.when <= now) { while (peek_timeout(&to) && to.when <= now) {
pop_timeout(0); pop_timeout(0);
handle_timeout_worker(to);
if (to.curr_fiber != NULL) { if (to.curr_fiber != NULL) {
if (janet_fiber_can_resume(to.curr_fiber)) { if (janet_fiber_can_resume(to.curr_fiber)) {
janet_cancel(to.fiber, janet_cstringv("deadline expired")); janet_cancel(to.fiber, janet_cstringv("deadline expired"));
@ -1472,6 +1473,7 @@ JanetFiber *janet_loop1(void) {
} }
} }
} }
handle_timeout_worker(to, 0);
} }
/* Run scheduled fibers unless interrupts need to be handled. */ /* Run scheduled fibers unless interrupts need to be handled. */
@ -1521,12 +1523,12 @@ JanetFiber *janet_loop1(void) {
if (!janet_fiber_can_resume(to.curr_fiber)) { if (!janet_fiber_can_resume(to.curr_fiber)) {
pop_timeout(0); pop_timeout(0);
janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(to.curr_fiber)); janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(to.curr_fiber));
handle_timeout_worker(to); handle_timeout_worker(to, 1);
continue; continue;
} }
} else if (to.fiber->sched_id != to.sched_id) { } else if (to.fiber->sched_id != to.sched_id) {
pop_timeout(0); pop_timeout(0);
handle_timeout_worker(to); handle_timeout_worker(to, 1);
continue; continue;
} }
break; break;

View File

@ -564,12 +564,14 @@
(,ev/deadline ,sec nil ,f true) (,ev/deadline ,sec nil ,f true)
(,resume ,f)))) (,resume ,f))))
(repeat 10 (for i 0 10
# (print "iteration " i)
(assert (= :done (with-deadline2 10 (assert (= :done (with-deadline2 10
(ev/sleep 0.01) (ev/sleep 0.01)
:done)) "deadline with interrupt exits normally")) :done)) "deadline with interrupt exits normally"))
(repeat 10 (for i 0 10
# (print "iteration " i)
(let [f (coro (forever :foo))] (let [f (coro (forever :foo))]
(ev/deadline 0.01 nil f true) (ev/deadline 0.01 nil f true)
(assert-error "deadline expired" (resume f)))) (assert-error "deadline expired" (resume f))))