Improve Windows sleep accuracy from 15ms to 15µs

This commit is contained in:
Justine Tunney 2024-12-06 23:00:07 -08:00
parent b40140e6c5
commit b490e23d63
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 189 additions and 67 deletions

View file

@ -28,6 +28,7 @@ TOOL_VIZ_DIRECTDEPS = \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_COMDLG32 \
LIBC_NT_NTDLL \
LIBC_NT_GDI32 \
LIBC_NT_KERNEL32 \
LIBC_NT_USER32 \

View file

@ -20,8 +20,17 @@
#include <stdio.h>
#include <time.h>
#include "libc/assert.h"
#include "libc/dce.h"
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/enum/threadpriority.h"
#include "libc/nt/ntdll.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thread.h"
#include "libc/nt/windows.h"
#define MAXIMUM 1e9
#define MAXIMUM 1e8
#define ITERATIONS 10
const char *MyDescribeClockName(int clock) {
@ -29,6 +38,8 @@ const char *MyDescribeClockName(int clock) {
return "CLOCK_REALTIME";
if (clock == CLOCK_MONOTONIC)
return "CLOCK_MONOTONIC";
if (clock == CLOCK_BOOTTIME)
return "CLOCK_BOOTTIME";
if (clock == CLOCK_REALTIME_COARSE)
return "CLOCK_REALTIME_COARSE";
if (clock == CLOCK_MONOTONIC_COARSE)
@ -40,7 +51,7 @@ void TestSleepRelative(int clock) {
printf("\n");
printf("testing: clock_nanosleep(%s) with relative timeout\n",
MyDescribeClockName(clock));
for (long nanos = 1; nanos < (long)MAXIMUM; nanos *= 2) {
for (long nanos = 1; nanos < (long)MAXIMUM; nanos *= 4) {
struct timespec t1, t2, wf;
wf = timespec_fromnanos(nanos);
if (clock_gettime(clock, &t1))
@ -57,7 +68,8 @@ void TestSleepRelative(int clock) {
int main(int argc, char *argv[]) {
TestSleepRelative(CLOCK_REALTIME);
TestSleepRelative(CLOCK_MONOTONIC);
TestSleepRelative(CLOCK_REALTIME_COARSE);
TestSleepRelative(CLOCK_MONOTONIC);
TestSleepRelative(CLOCK_BOOTTIME);
TestSleepRelative(CLOCK_MONOTONIC_COARSE);
}