Replies: 1 comment
-
OK so I looked into things, and I'm proud to announce I managed to bring the accuracy of
So the way this works, is if you call a function like sleep() or usleep() then we'll now use the Take a look at the change. Please test it and report back. Most importantly, enjoy it! |
Beta Was this translation helpful? Give feedback.
-
It says on https://justine.lol/cosmopolitan/functions.html that the windows version of "nanosleep" uses SleepEx, which uses milliseconds.
On Windows, "Waitable Timers" allow you to request due times in units of 100 nanoseconds. API functions for these: "CreateWaitableTimerEx", "SetWaitableTimer", then use "WaitForSingleObjectEx" to actually wait for the timer to expire. Also "CloseHandle" to destroy the timer.
But as for what precision and accuracy you'll actually get from Windows though, that is a mystery. The last time I tried anything with waitable timers, they didn't seem any different than calling sleep(1). But I also wasn't using the functions to change the timer resolution. So while you can use make requests using precise units for your waitable timers, you probably won't actually get anything that accurate.
There is a documented function "timeBeginPeriod" from Winmm.dll to set timer precision in units of milliseconds. I used Ghidra to peek at its implementation in Kernel32.dll. It wraps things in a mutual exclusion, and calls an undocumented function "NtSetTimerResolution" from NTDLL.DLL. Ntinternals.net has documentation on the function. Units are 100ns, so to convert from milliseconds, you multiply by 10000.
Beta Was this translation helpful? Give feedback.
All reactions