|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License Version 2.0 with LLVM Exceptions |
| 5 | + * (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://llvm.org/LICENSE.txt |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include <chrono> |
| 19 | +#include <cstdint> |
| 20 | + |
| 21 | +#include <windows.h> |
| 22 | + |
| 23 | +namespace exec::__win32 { |
| 24 | + |
| 25 | +class filetime_clock { |
| 26 | +public: |
| 27 | + using rep = std::int64_t; |
| 28 | + using ratio = std::ratio<1, 10'000'000>; // 100ns |
| 29 | + using duration = std::chrono::duration<rep, ratio>; |
| 30 | + |
| 31 | + static constexpr bool is_steady = false; |
| 32 | + |
| 33 | + class time_point { |
| 34 | + public: |
| 35 | + using duration = filetime_clock::duration; |
| 36 | + |
| 37 | + constexpr time_point() noexcept = default; |
| 38 | + |
| 39 | + constexpr time_point(const time_point &) noexcept = default; |
| 40 | + |
| 41 | + auto operator=(const time_point &) noexcept -> time_point & = default; |
| 42 | + |
| 43 | + [[nodiscard]] auto get_ticks() const noexcept -> std::uint64_t { |
| 44 | + return ticks_; |
| 45 | + } |
| 46 | + |
| 47 | + static constexpr auto(max)() noexcept -> time_point { |
| 48 | + time_point tp; |
| 49 | + tp.ticks_ = (std::numeric_limits<std::int64_t>::max)(); |
| 50 | + return tp; |
| 51 | + } |
| 52 | + |
| 53 | + static constexpr auto(min)() noexcept -> time_point { return time_point{}; } |
| 54 | + |
| 55 | + static auto from_ticks(std::uint64_t ticks) noexcept -> time_point { |
| 56 | + time_point tp; |
| 57 | + tp.ticks_ = ticks; |
| 58 | + return tp; |
| 59 | + } |
| 60 | + |
| 61 | + template <typename Rep, typename Ratio> |
| 62 | + auto operator+=(const std::chrono::duration<Rep, Ratio> &d) noexcept |
| 63 | + -> time_point & { |
| 64 | + ticks_ += std::chrono::duration_cast<duration>(d).count(); |
| 65 | + return *this; |
| 66 | + } |
| 67 | + |
| 68 | + template <typename Rep, typename Ratio> |
| 69 | + auto operator-=(const std::chrono::duration<Rep, Ratio> &d) noexcept |
| 70 | + -> time_point & { |
| 71 | + ticks_ -= std::chrono::duration_cast<duration>(d).count(); |
| 72 | + return *this; |
| 73 | + } |
| 74 | + |
| 75 | + friend auto operator-(time_point a, time_point b) noexcept -> duration { |
| 76 | + return duration{a.ticks_} - duration{b.ticks_}; |
| 77 | + } |
| 78 | + |
| 79 | + template <typename Rep, typename Ratio> |
| 80 | + friend auto operator-(time_point t, |
| 81 | + std::chrono::duration<Rep, Ratio> d) noexcept |
| 82 | + -> time_point { |
| 83 | + time_point tp = t; |
| 84 | + tp -= d; |
| 85 | + return tp; |
| 86 | + } |
| 87 | + |
| 88 | + template <typename Rep, typename Ratio> |
| 89 | + friend auto operator+(time_point t, |
| 90 | + std::chrono::duration<Rep, Ratio> d) noexcept |
| 91 | + -> time_point { |
| 92 | + time_point tp = t; |
| 93 | + tp += d; |
| 94 | + return tp; |
| 95 | + } |
| 96 | + |
| 97 | + template <typename Rep, typename Ratio> |
| 98 | + friend auto operator+(std::chrono::duration<Rep, Ratio> d, time_point t) noexcept |
| 99 | + -> time_point { |
| 100 | + return t + d; |
| 101 | + } |
| 102 | + |
| 103 | + friend auto operator==(time_point a, time_point b) noexcept -> bool { |
| 104 | + return a.ticks_ == b.ticks_; |
| 105 | + } |
| 106 | + |
| 107 | + friend auto operator!=(time_point a, time_point b) noexcept -> bool { |
| 108 | + return a.ticks_ != b.ticks_; |
| 109 | + } |
| 110 | + |
| 111 | + friend auto operator<(time_point a, time_point b) noexcept -> bool { |
| 112 | + return a.ticks_ < b.ticks_; |
| 113 | + } |
| 114 | + |
| 115 | + friend auto operator>(time_point a, time_point b) noexcept -> bool { |
| 116 | + return a.ticks_ > b.ticks_; |
| 117 | + } |
| 118 | + |
| 119 | + friend auto operator<=(time_point a, time_point b) noexcept -> bool { |
| 120 | + return a.ticks_ <= b.ticks_; |
| 121 | + } |
| 122 | + |
| 123 | + friend auto operator>=(time_point a, time_point b) noexcept -> bool { |
| 124 | + return a.ticks_ >= b.ticks_; |
| 125 | + } |
| 126 | + |
| 127 | + private: |
| 128 | + // Ticks since Jan 1, 1601 (UTC) |
| 129 | + std::uint64_t ticks_{}; |
| 130 | + }; |
| 131 | + |
| 132 | + static auto now() noexcept -> time_point { |
| 133 | + FILETIME filetime; |
| 134 | + ::GetSystemTimeAsFileTime(&filetime); |
| 135 | + |
| 136 | + ULARGE_INTEGER ticks; |
| 137 | + ticks.HighPart = filetime.dwHighDateTime; |
| 138 | + ticks.LowPart = filetime.dwLowDateTime; |
| 139 | + |
| 140 | + return time_point::from_ticks(ticks.QuadPart); |
| 141 | + } |
| 142 | +}; |
| 143 | + |
| 144 | +} // namespace exec::__win32 |
0 commit comments