Skip to content

Commit e4dea37

Browse files
committed
Make clock_nanosleep() cancel faster
1 parent 0c89516 commit e4dea37

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

libc/calls/checkcancel.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
#include "libc/intrin/atomic.h"
2121
#include "libc/intrin/weaken.h"
2222
#include "libc/thread/posixthread.internal.h"
23-
#ifdef __x86_64__
2423

25-
textwindows int _check_cancel(void) {
24+
int _check_cancel(void) {
2625
if (_weaken(_pthread_cancel_ack) && //
2726
_pthread_self() && !(_pthread_self()->pt_flags & PT_NOCANCEL) &&
2827
atomic_load_explicit(&_pthread_self()->pt_canceled,
@@ -31,5 +30,3 @@ textwindows int _check_cancel(void) {
3130
}
3231
return 0;
3332
}
34-
35-
#endif /* __x86_64__ */

libc/calls/clock_nanosleep.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/assert.h"
2020
#include "libc/calls/cp.internal.h"
21+
#include "libc/calls/internal.h"
2122
#include "libc/calls/struct/timespec.h"
2223
#include "libc/calls/struct/timespec.internal.h"
2324
#include "libc/dce.h"
@@ -27,6 +28,7 @@
2728
#include "libc/intrin/strace.internal.h"
2829
#include "libc/intrin/weaken.h"
2930
#include "libc/runtime/clktck.h"
31+
#include "libc/runtime/runtime.h"
3032
#include "libc/sysv/consts/clock.h"
3133
#include "libc/sysv/consts/timer.h"
3234
#include "libc/sysv/errfuns.h"
@@ -108,9 +110,17 @@ static int cosmo_clock_nanosleep(int clock, int flags,
108110
}
109111

110112
// spin through final scheduling quantum
111-
do unassert(!clock_gettime(time_clock, &now));
112-
while (timespec_cmp(now, deadline) < 0);
113-
return 0;
113+
int rc = 0;
114+
ftrace_enabled(-1);
115+
do {
116+
if (_check_cancel()) {
117+
rc = -1;
118+
break;
119+
}
120+
unassert(!clock_gettime(time_clock, &now));
121+
} while (timespec_cmp(now, deadline) < 0);
122+
ftrace_enabled(+1);
123+
return rc;
114124
}
115125

116126
/**

0 commit comments

Comments
 (0)