mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-08-06 02:34:00 +00:00
[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:
@@ -25,6 +25,13 @@
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
|
||||
void windowsSetTimerResolution(void);
|
||||
NTSYSCALLAPI NTSTATUS NTAPI NtDelayExecution(
|
||||
_In_ BOOLEAN Alertable,
|
||||
_In_opt_ PLARGE_INTEGER DelayInterval
|
||||
);
|
||||
|
||||
#else
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
@@ -49,6 +56,21 @@ static inline uint64_t microtime(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void nsleep(uint64_t ns)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
LARGE_INTEGER interval = { .QuadPart = -(int64_t)(ns / 100LL) };
|
||||
NtDelayExecution(FALSE, &interval);
|
||||
#else
|
||||
const struct timespec ts =
|
||||
{
|
||||
.tv_sec = ns / 1e9,
|
||||
.tv_nsec = ns - ((ns / 1e9) * 1e9)
|
||||
};
|
||||
nanosleep(&ts, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !defined(_WIN32)
|
||||
//FIXME: make win32 versions
|
||||
static inline uint64_t nanotime(void)
|
||||
@@ -58,16 +80,6 @@ static inline uint64_t nanotime(void)
|
||||
return ((uint64_t)time.tv_sec * 1000000000LL) + time.tv_nsec;
|
||||
}
|
||||
|
||||
static inline void nsleep(uint64_t ns)
|
||||
{
|
||||
const struct timespec ts =
|
||||
{
|
||||
.tv_sec = ns / 1e9,
|
||||
.tv_nsec = ns - ((ns / 1e9) * 1e9)
|
||||
};
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
static inline void tsDiff(struct timespec *diff, const struct timespec *left,
|
||||
const struct timespec *right)
|
||||
{
|
||||
|
@@ -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)
|
||||
|
7
common/src/platform/windows/ntdll.def
Normal file
7
common/src/platform/windows/ntdll.def
Normal 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
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user