| 53 | } |
| 54 | |
| 55 | WasiExpect<void> Clock::clockTimeGet(__wasi_clockid_t Id, |
| 56 | __wasi_timestamp_t Precision |
| 57 | [[maybe_unused]], |
| 58 | __wasi_timestamp_t &Time) noexcept { |
| 59 | switch (Id) { |
| 60 | case __WASI_CLOCKID_REALTIME: { |
| 61 | FILETIME_ SysNow; |
| 62 | #if NTDDI_VERSION >= NTDDI_WIN8 |
| 63 | GetSystemTimePreciseAsFileTime(&SysNow); |
| 64 | #else |
| 65 | GetSystemTimeAsFileTime(&SysNow); |
| 66 | #endif |
| 67 | Time = detail::fromFiletime(SysNow); |
| 68 | return {}; |
| 69 | } |
| 70 | case __WASI_CLOCKID_MONOTONIC: { |
| 71 | uint64_t Nanoseconds; |
| 72 | const auto Counter = counter(); |
| 73 | if (likely(std::nano::den % kFrequency == 0)) { |
| 74 | Nanoseconds = Counter * (std::nano::den / kFrequency); |
| 75 | } else { |
| 76 | const auto Seconds = Counter / kFrequency; |
| 77 | const auto Fractions = Counter % kFrequency; |
| 78 | Nanoseconds = |
| 79 | Seconds * std::nano::den + (Fractions * std::nano::den) / kFrequency; |
| 80 | } |
| 81 | Time = static_cast<__wasi_timestamp_t>(Nanoseconds); |
| 82 | return {}; |
| 83 | } |
| 84 | case __WASI_CLOCKID_PROCESS_CPUTIME_ID: { |
| 85 | FILETIME_ CreationTime; |
| 86 | FILETIME_ ExitTime; |
| 87 | FILETIME_ KernelTime; |
| 88 | FILETIME_ UserTime; |
| 89 | if (unlikely(!GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, |
| 90 | &KernelTime, &UserTime))) { |
| 91 | return WasiUnexpect(detail::fromLastError(GetLastError())); |
| 92 | } |
| 93 | |
| 94 | Time = detail::fromFiletime(KernelTime) + detail::fromFiletime(UserTime); |
| 95 | |
| 96 | return {}; |
| 97 | } |
| 98 | case __WASI_CLOCKID_THREAD_CPUTIME_ID: { |
| 99 | FILETIME_ CreationTime; |
| 100 | FILETIME_ ExitTime; |
| 101 | FILETIME_ KernelTime; |
| 102 | FILETIME_ UserTime; |
| 103 | if (unlikely(!GetThreadTimes(GetCurrentThread(), &CreationTime, &ExitTime, |
| 104 | &KernelTime, &UserTime))) { |
| 105 | return WasiUnexpect(detail::fromLastError(GetLastError())); |
| 106 | } |
| 107 | |
| 108 | Time = detail::fromFiletime(KernelTime) + detail::fromFiletime(UserTime); |
| 109 | |
| 110 | return {}; |
| 111 | } |
| 112 | default: |
no test coverage detected