[host/common] windows: provide delayExecution via nsleep

This change moves this platform specific sleep to `common` as the OS
agnostic `nsleep` function.

Ref PR #661
This commit is contained in:
Geoffrey McRae
2021-07-26 16:36:56 +10:00
parent 120fe63c0f
commit 5f5f497cbd
9 changed files with 59 additions and 107 deletions

View File

@@ -8,6 +8,25 @@ include_directories(
# allow use of functions for Windows 7 or later
add_compile_definitions(WINVER=0x0601 _WIN32_WINNT=0x0601)
if (MINGW)
# Build our own ntdll.dll import library
# This tricks MinGW into not linking stuff like memcpy from ntdll.dll instead of mscvrt.dll
if(NOT CMAKE_DLLTOOL)
# cmake older than 3.16 doesn't know how to find dlltool
find_program(CMAKE_DLLTOOL NAMES "x86_64-w64-mingw32-dlltool" "dlltool.exe" DOC "dlltool executable")
endif()
add_custom_command(OUTPUT "${PROJECT_BINARY_DIR}/ntdll.a"
COMMAND "${CMAKE_DLLTOOL}" -d "${PROJECT_SOURCE_DIR}/ntdll.def" -l "${PROJECT_BINARY_DIR}/ntdll.a"
MAIN_DEPENDENCY "${PROJECT_SOURCE_DIR}/ntdll.def"
COMMENT "Building import library ntdll.a"
VERBATIM
)
add_custom_target(ntdll_target DEPENDS "${PROJECT_BINARY_DIR}/ntdll.a")
add_library(ntdll STATIC IMPORTED GLOBAL)
add_dependencies(ntdll ntdll_target)
set_target_properties(ntdll PROPERTIES IMPORTED_LOCATION "${PROJECT_BINARY_DIR}/ntdll.a")
endif()
add_library(lg_common_platform_code STATIC
debug.c
crash.c
@@ -22,6 +41,7 @@ add_library(lg_common_platform_code STATIC
target_link_libraries(lg_common_platform_code
lg_common
setupapi
ntdll
)
if (ENABLE_BACKTRACE)

View File

@@ -0,0 +1,7 @@
; This file is used to trick MinGW to not like stuff like memcpy from ntdll.dll.
; See CMakeLists.txt for how this is compiled.
LIBRARY "ntdll.dll"
EXPORTS
NtDelayExecution
NtSetTimerResolution

View File

@@ -71,3 +71,16 @@ void lgTimerDestroy(LGTimer * timer)
free(timer);
}
NTSYSCALLAPI NTSTATUS NTAPI NtSetTimerResolution(
_In_ ULONG DesiredTime,
_In_ BOOLEAN SetResolution,
_Out_ PULONG ActualTime
);
void windowsSetTimerResolution(void)
{
ULONG actualResolution;
NtSetTimerResolution(1, true, &actualResolution);
DEBUG_INFO("System timer resolution: %.1f μs", actualResolution / 10.0);
}