Skip to content
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

Fixes to precise_time_ns #10403

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 11 additions & 9 deletions src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ get_time(int64_t *sec, int32_t *nsec) {
}
#endif

const uint64_t ns_per_s = 1000000000LL;
const int64_t ns_per_s = 1000000000LL;

extern "C" CDECL void
precise_time_ns(uint64_t *ns) {
Expand All @@ -217,18 +217,20 @@ precise_time_ns(uint64_t *ns) {
uint64_t time_nano = time * (info.numer / info.denom);
*ns = time_nano;
#elif __WIN32__
uint64_t ticks_per_s;
QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_s);
if (ticks_per_s == 0LL) {
ticks_per_s = 1LL;
LARGE_INTEGER ticks_per_s;
BOOL query_result = QueryPerformanceFrequency(&ticks_per_s);
assert(query_result);
if (ticks_per_s.QuadPart == 0LL) {
ticks_per_s.QuadPart = 1LL;
}
uint64_t ticks;
QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
*ns = ((ticks * ns_per_s) / ticks_per_s);
LARGE_INTEGER ticks;
query_result = QueryPerformanceCounter(&ticks);
assert(query_result);
*ns = (uint64_t)((ticks.QuadPart * ns_per_s) / ticks_per_s.QuadPart);
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
*ns = (ts.tv_sec * ns_per_s + ts.tv_nsec);
*ns = (uint64_t)(ts.tv_sec * ns_per_s + ts.tv_nsec);
#endif
}

Expand Down