mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-30 16:28:30 +00:00
Improve time/sleep accuracy on Windows
It's now almost as good as Linux thanks to a Windows 8+ API.
This commit is contained in:
parent
72ac5f18d9
commit
8caf1b48a9
9 changed files with 76 additions and 31 deletions
|
@ -40,7 +40,11 @@ static dontinline int __clk_tck_init(void) {
|
|||
size_t len;
|
||||
struct clockinfo_netbsd clock;
|
||||
if (IsWindows()) {
|
||||
x = 1000;
|
||||
// MSVC defines CLK_TCK as 1000 but 1ms is obviously not the
|
||||
// scheduling quantum Windows actually uses. If we define it
|
||||
// as 30 rather than 1000, then clock_nanosleep is much more
|
||||
// accurately able to predict the duration of its busy waits
|
||||
x = 30;
|
||||
} else if (IsXnu() || IsOpenbsd()) {
|
||||
x = 100;
|
||||
} else if (IsFreebsd()) {
|
||||
|
|
|
@ -25,14 +25,20 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
static int sys_clock_getres_poly(int clock, struct timespec *ts, int64_t real) {
|
||||
static int sys_clock_getres_poly(int clock, struct timespec *ts, int64_t real,
|
||||
int64_t real_coarse, int64_t boot) {
|
||||
ts->tv_sec = 0;
|
||||
if (clock == CLOCK_REALTIME) {
|
||||
ts->tv_sec = 0;
|
||||
ts->tv_nsec = real;
|
||||
return 0;
|
||||
} else if (clock == CLOCK_REALTIME_COARSE) {
|
||||
ts->tv_nsec = real_coarse;
|
||||
return 0;
|
||||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
ts->tv_sec = 0;
|
||||
ts->tv_nsec = 1;
|
||||
ts->tv_nsec = 10;
|
||||
return 0;
|
||||
} else if (clock == CLOCK_BOOTTIME) {
|
||||
ts->tv_nsec = boot;
|
||||
return 0;
|
||||
} else {
|
||||
return einval();
|
||||
|
@ -40,11 +46,11 @@ static int sys_clock_getres_poly(int clock, struct timespec *ts, int64_t real) {
|
|||
}
|
||||
|
||||
static int sys_clock_getres_nt(int clock, struct timespec *ts) {
|
||||
return sys_clock_getres_poly(clock, ts, 100);
|
||||
return sys_clock_getres_poly(clock, ts, 100, 1000000, 1000000);
|
||||
}
|
||||
|
||||
static int sys_clock_getres_xnu(int clock, struct timespec *ts) {
|
||||
return sys_clock_getres_poly(clock, ts, 1000);
|
||||
return sys_clock_getres_poly(clock, ts, 1000, 1000, 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,26 +18,61 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
|
||||
#define _CLOCK_REALTIME 0
|
||||
#define _CLOCK_MONOTONIC 1
|
||||
#define _CLOCK_REALTIME_COARSE 2
|
||||
#define _CLOCK_BOOTTIME 3
|
||||
|
||||
static struct {
|
||||
uint64_t base;
|
||||
uint64_t freq;
|
||||
} g_winclock;
|
||||
|
||||
textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
|
||||
uint64_t t;
|
||||
struct NtFileTime ft;
|
||||
if (clock == CLOCK_REALTIME) {
|
||||
if (!ts) return 0;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
*ts = FileTimeToTimeSpec(ft);
|
||||
return 0;
|
||||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
if (!ts) return 0;
|
||||
return sys_clock_gettime_mono(ts);
|
||||
} else if (clock == CLOCK_BOOTTIME) {
|
||||
if (ts) *ts = timespec_frommillis(GetTickCount64());
|
||||
return 0;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
switch (clock) {
|
||||
case _CLOCK_REALTIME:
|
||||
if (ts) {
|
||||
GetSystemTimePreciseAsFileTime(&ft);
|
||||
*ts = FileTimeToTimeSpec(ft);
|
||||
}
|
||||
return 0;
|
||||
case _CLOCK_REALTIME_COARSE:
|
||||
if (ts) {
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
*ts = FileTimeToTimeSpec(ft);
|
||||
}
|
||||
return 0;
|
||||
case _CLOCK_MONOTONIC:
|
||||
if (ts) {
|
||||
QueryPerformanceCounter(&t);
|
||||
t = ((t - g_winclock.base) * 1000000000) / g_winclock.freq;
|
||||
*ts = timespec_fromnanos(t);
|
||||
}
|
||||
return 0;
|
||||
case _CLOCK_BOOTTIME:
|
||||
if (ts) {
|
||||
*ts = timespec_frommillis(GetTickCount64());
|
||||
}
|
||||
return 0;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static textstartup void winclock_init() {
|
||||
if (IsWindows()) {
|
||||
QueryPerformanceCounter(&g_winclock.base);
|
||||
QueryPerformanceFrequency(&g_winclock.freq);
|
||||
}
|
||||
}
|
||||
|
||||
const void *const winclock_ctor[] initarray = {
|
||||
winclock_init,
|
||||
};
|
||||
|
|
|
@ -65,7 +65,7 @@ static textwindows int sys_utimensat_nt_impl(int dirfd, const char *path,
|
|||
}
|
||||
|
||||
if (!ts || ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW) {
|
||||
GetSystemTimeAsFileTime(ft);
|
||||
GetSystemTimePreciseAsFileTime(ft);
|
||||
}
|
||||
if (ts) {
|
||||
for (i = 0; i < 2; ++i) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue