Skip to content

Commit 57b99cb

Browse files
committed
attempt to fix timer on windows
1 parent 30c688d commit 57b99cb

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/cpu/clock.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "cpu/clock.h"
2929
#include "cpu/interrupts.h"
3030
#include "cp/cp.h"
31+
#include "utils/compat_time.h"
3132

3233
#include "log.h"
3334

@@ -44,15 +45,29 @@ static void * clock_thread(void *ptr)
4445
struct timespec ts;
4546
unsigned clock_tick_nsec = clock_period * 1000000;
4647
unsigned new_nsec;
47-
clock_gettime(CLOCK_REALTIME , &ts);
48+
#ifdef _WIN32
49+
clock_gettime(CLOCK_MONOTONIC, &ts);
50+
#else
51+
clock_gettime(CLOCK_REALTIME, &ts);
52+
#endif
4853

4954
while (1) {
5055
new_nsec = ts.tv_nsec + clock_tick_nsec;
5156
ts.tv_sec += new_nsec / 1000000000L;
5257
ts.tv_nsec = new_nsec % 1000000000L;
58+
#ifdef _WIN32
59+
// winpthreads sem_timedwait only resolves to the scheduler tick, which
60+
// makes the deadline slip and fire INT_CLOCK in catch-up bursts. Sleep
61+
// on the high-res waitable timer instead and poll quit non-blocking.
62+
compat_sleep_until(&ts);
63+
if (!sem_trywait(&clock_quit)) {
64+
break;
65+
}
66+
#else
5367
if (!sem_timedwait(&clock_quit, &ts)) {
5468
break;
5569
}
70+
#endif
5671
if (cp_clock_get()) {
5772
int_set(atomic_load_explicit(&clock_int, memory_order_relaxed));
5873
}

0 commit comments

Comments
 (0)