InitializeTimerResolution fix slowdown on Windows 11, drop obsolete workaround for Windows NT

This commit is contained in:
Peter Berendi 2025-04-21 17:58:47 +02:00 committed by Adam Honse
parent c02da4066c
commit 4e5fd41be7

View file

@ -43,30 +43,25 @@ using namespace std::chrono_literals;
* * * *
\******************************************************************************************/ \******************************************************************************************/
#ifdef _WIN32 #ifdef _WIN32
typedef unsigned int NTSTATUS;
typedef NTSTATUS (*NTSETTIMERRESOLUTION)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution);
void InitializeTimerResolution() void InitializeTimerResolution()
{ {
NTSETTIMERRESOLUTION NtSetTimerResolution; HMODULE ntdll = LoadLibrary("ntdll.dll");
HMODULE NtDllHandle; if(ntdll != NULL)
ULONG CurrentResolution;
NtDllHandle = LoadLibrary("ntdll.dll");
NtSetTimerResolution = (NTSETTIMERRESOLUTION)GetProcAddress(NtDllHandle, "NtSetTimerResolution");
NtSetTimerResolution(5000, TRUE, &CurrentResolution);
}
void InitializeTimerResolutionThreadFunction()
{
while(1)
{ {
InitializeTimerResolution(); typedef LONG (*NTSETTIMERRESOLUTION)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution);
ULONG CurrentResolution;
std::this_thread::sleep_for(500ms); NTSETTIMERRESOLUTION NtSetTimerResolution = (NTSETTIMERRESOLUTION)GetProcAddress(ntdll, "NtSetTimerResolution");
if(NtSetTimerResolution != NULL)
{
NtSetTimerResolution(1000, TRUE, &CurrentResolution);
}
} }
// PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION = 4, isn't defined in Win10 headers
PROCESS_POWER_THROTTLING_STATE PowerThrottlingState { PROCESS_POWER_THROTTLING_CURRENT_VERSION, 4, 0 };
// take care of the slowdown on Windows 11
SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &PowerThrottlingState, sizeof(PowerThrottlingState));
} }
#endif #endif
@ -181,9 +176,7 @@ int main(int argc, char* argv[])
/*---------------------------------------------------------*\ /*---------------------------------------------------------*\
| Windows only - Start timer resolution correction thread | | Windows only - Start timer resolution correction thread |
\*---------------------------------------------------------*/ \*---------------------------------------------------------*/
std::thread * InitializeTimerResolutionThread; InitializeTimerResolution();
InitializeTimerResolutionThread = new std::thread(InitializeTimerResolutionThreadFunction);
InitializeTimerResolutionThread->detach();
/*---------------------------------------------------------*\ /*---------------------------------------------------------*\
| Windows only - Install SMBus Driver WinRing0 | | Windows only - Install SMBus Driver WinRing0 |