From e4dea37b8ed6ec2727d48e13bf545556ff63f882 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 18 Nov 2023 18:12:09 -0800 Subject: [PATCH] Make clock_nanosleep() cancel faster --- libc/calls/checkcancel.c | 5 +---- libc/calls/clock_nanosleep.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/libc/calls/checkcancel.c b/libc/calls/checkcancel.c index ba066a0ed89..dabe9edae05 100644 --- a/libc/calls/checkcancel.c +++ b/libc/calls/checkcancel.c @@ -20,9 +20,8 @@ #include "libc/intrin/atomic.h" #include "libc/intrin/weaken.h" #include "libc/thread/posixthread.internal.h" -#ifdef __x86_64__ -textwindows int _check_cancel(void) { +int _check_cancel(void) { if (_weaken(_pthread_cancel_ack) && // _pthread_self() && !(_pthread_self()->pt_flags & PT_NOCANCEL) && atomic_load_explicit(&_pthread_self()->pt_canceled, @@ -31,5 +30,3 @@ textwindows int _check_cancel(void) { } return 0; } - -#endif /* __x86_64__ */ diff --git a/libc/calls/clock_nanosleep.c b/libc/calls/clock_nanosleep.c index 987d0bbdb3d..9dd7633e97d 100644 --- a/libc/calls/clock_nanosleep.c +++ b/libc/calls/clock_nanosleep.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/calls/cp.internal.h" +#include "libc/calls/internal.h" #include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timespec.internal.h" #include "libc/dce.h" @@ -27,6 +28,7 @@ #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/runtime/clktck.h" +#include "libc/runtime/runtime.h" #include "libc/sysv/consts/clock.h" #include "libc/sysv/consts/timer.h" #include "libc/sysv/errfuns.h" @@ -108,9 +110,17 @@ static int cosmo_clock_nanosleep(int clock, int flags, } // spin through final scheduling quantum - do unassert(!clock_gettime(time_clock, &now)); - while (timespec_cmp(now, deadline) < 0); - return 0; + int rc = 0; + ftrace_enabled(-1); + do { + if (_check_cancel()) { + rc = -1; + break; + } + unassert(!clock_gettime(time_clock, &now)); + } while (timespec_cmp(now, deadline) < 0); + ftrace_enabled(+1); + return rc; } /**