[host] windows: add delayExecution function for more accurate sleeps

This change not only exposes and allows use of NtDelayExecution, but
also moves the code to set the system timer resolution.
This commit is contained in:
Geoffrey McRae
2021-07-17 14:55:22 +10:00
parent e228165ff9
commit 411a6b1e49
6 changed files with 105 additions and 14 deletions

View File

@@ -20,6 +20,7 @@
#include "platform.h"
#include "service.h"
#include "windows/delay.h"
#include "windows/mousehook.h"
#include <windows.h>
@@ -61,10 +62,6 @@ struct AppState
static struct AppState app = {0};
HWND MessageHWND;
// undocumented API to adjust the system timer resolution (yes, its a nasty hack)
typedef NTSTATUS (__stdcall *ZwSetTimerResolution_t)(ULONG RequestedResolution, BOOLEAN Set, PULONG ActualResolution);
static ZwSetTimerResolution_t ZwSetTimerResolution = NULL;
// linux mingw64 is missing this
#ifndef MSGFLT_RESET
#define MSGFLT_RESET (0)
@@ -504,7 +501,7 @@ void boostPriority(void)
bool app_init(void)
{
const char * logFile = option_get_string("os", "logFile" );
const char * logFile = option_get_string("os", "logFile");
// redirect stderr to a file
if (logFile && strcmp(logFile, "stderr") != 0)
@@ -513,14 +510,7 @@ bool app_init(void)
// always flush stderr
setbuf(stderr, NULL);
// Increase the timer resolution
ZwSetTimerResolution = (ZwSetTimerResolution_t)GetProcAddress(GetModuleHandle("ntdll.dll"), "ZwSetTimerResolution");
if (ZwSetTimerResolution)
{
ULONG actualResolution;
ZwSetTimerResolution(1, true, &actualResolution);
DEBUG_INFO("System timer resolution: %lu ns", actualResolution * 100);
}
delayInit();
// get the performance frequency for spinlocks
QueryPerformanceFrequency(&app.perfFreq);