Skip to content

Commit

Permalink
Make clock_nanosleep() cancel faster
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 19, 2023
1 parent 0c89516 commit e4dea37
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 1 addition & 4 deletions libc/calls/checkcancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,5 +30,3 @@ textwindows int _check_cancel(void) {
}
return 0;
}

#endif /* __x86_64__ */
16 changes: 13 additions & 3 deletions libc/calls/clock_nanosleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit e4dea37

Please sign in to comment.