Skip to content

[Windows][Concurrency] Use the same clock as Dispatch. #76310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion stdlib/public/Concurrency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ set(SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR}/Internal

set(swift_concurrency_private_link_libraries)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
list(APPEND swift_concurrency_private_link_libraries Synchronization)
list(APPEND swift_concurrency_private_link_libraries
Synchronization
mincore.lib # For QueryInterruptTime()
)
endif()

set(swift_concurrency_incorporate_object_libraries_so swiftThreading)
Expand Down
51 changes: 13 additions & 38 deletions stdlib/public/Concurrency/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ void swift_get_time(
#elif (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__))
clock_gettime(CLOCK_MONOTONIC, &continuous);
#elif defined(_WIN32)
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
// Divide count (number of ticks) by frequency (number of ticks per
// second) to get the counter in seconds. We also need to multiply the
// count by 1,000,000,000 to get nanosecond resolution. By multiplying
// first, we maintain high precision. The resulting value is the tick
// count in nanoseconds. Use 128-bit math to avoid overflowing.
auto quadPart = static_cast<unsigned _BitInt(128)>(count.QuadPart);
auto ns = (quadPart * 1'000'000'000) / freq.QuadPart;
continuous.tv_sec = ns / 1'000'000'000;
continuous.tv_nsec = ns % 1'000'000'000;
// This needs to match what swift-corelibs-libdispatch does

// QueryInterruptTimePrecise() outputs a value measured in 100ns
// units. We must divide the output by 10,000,000 to get a value in
// seconds and multiply the remainder by 100 to get nanoseconds.
ULONGLONG interruptTime;
(void)QueryInterruptTimePrecise(&interruptTime);
continuous.tv_sec = interruptTime / 10'000'000;
continuous.tv_nsec = (interruptTime % 10'000'000) * 100;
#else
#error Missing platform continuous time definition
#endif
Expand All @@ -72,32 +68,13 @@ void swift_get_time(
#elif (defined(__OpenBSD__) || defined(__FreeBSD__))
clock_gettime(CLOCK_UPTIME, &suspending);
#elif defined(_WIN32)
// QueryUnbiasedInterruptTimePrecise() was added in Windows 10 and is, as
// the name suggests, more precise than QueryUnbiasedInterruptTime().
// Unfortunately, the symbol is not listed in any .lib file in the SDK and
// must be looked up dynamically at runtime even if our minimum deployment
// target is Windows 10.
typedef decltype(QueryUnbiasedInterruptTimePrecise) *QueryUITP_FP;
static QueryUITP_FP queryUITP = nullptr;
static swift::once_t onceToken;
swift::once(onceToken, [] {
if (HMODULE hKernelBase = GetModuleHandleW(L"KernelBase.dll")) {
queryUITP = reinterpret_cast<QueryUITP_FP>(
GetProcAddress(hKernelBase, "QueryUnbiasedInterruptTimePrecise")
);
}
});
// This needs to match what swift-corelibs-libdispatch does

// Call whichever API is available. Both output a value measured in 100ns
// QueryUnbiasedInterruptTimePrecise() outputs a value measured in 100ns
// units. We must divide the output by 10,000,000 to get a value in
// seconds and multiply the remainder by 100 to get nanoseconds.
ULONGLONG unbiasedTime;
if (queryUITP) {
(* queryUITP)(&unbiasedTime);
} else {
// Fall back to the older, less precise API.
(void)QueryUnbiasedInterruptTime(&unbiasedTime);
}
(void)QueryUnbiasedInterruptTimePrecise(&unbiasedTime);
suspending.tv_sec = unbiasedTime / 10'000'000;
suspending.tv_nsec = (unbiasedTime % 10'000'000) * 100;
#else
Expand Down Expand Up @@ -128,10 +105,8 @@ switch (clock_id) {
#elif (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__))
clock_getres(CLOCK_MONOTONIC, &continuous);
#elif defined(_WIN32)
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
continuous.tv_sec = 0;
continuous.tv_nsec = 1'000'000'000 / freq.QuadPart;
continuous.tv_nsec = 100;
#else
#error Missing platform continuous time definition
#endif
Expand Down