From 800151891e0bb7221e17fd68bb7f11f50b5e7526 Mon Sep 17 00:00:00 2001 From: hackbunny Date: Sun, 1 Sep 2019 20:35:56 +0200 Subject: [PATCH 01/11] std::chrono wrappers for Windows times and timestamps --- include/wil/chrono.h | 377 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 377 insertions(+) create mode 100644 include/wil/chrono.h diff --git a/include/wil/chrono.h b/include/wil/chrono.h new file mode 100644 index 000000000..642e22410 --- /dev/null +++ b/include/wil/chrono.h @@ -0,0 +1,377 @@ +//********************************************************* +// +// Copyright (c) Microsoft. All rights reserved. +// This code is licensed under the MIT License. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +// PARTICULAR PURPOSE AND NONINFRINGEMENT. +// +//********************************************************* +#ifndef __WIL_CHRONO_INCLUDED +#define __WIL_CHRONO_INCLUDED + +#include "common.h" +#include "result.h" + +#ifdef WIL_KERNEL_MODE +#error This header is not supported in kernel-mode. +#endif + +#if __WI_CPLUSPLUS < 201103L +#error This header requires C++11 or later +#endif + +#include + +#include // FILETIME, etc. +#include // GetTickCount[64], GetSystemTime[Precise]AsFileTime +#include // Query[Unbiased]InterruptTime[Precise] + +namespace wil +{ +#pragma region std::chrono wrappers for GetTickCount[64] + namespace impl + { + template + struct tick_count_clock_impl + { + using rep = ResultT; + using period = std::milli; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool const is_steady = true; + + WI_NODISCARD static time_point now() WI_NOEXCEPT + { + return time_point{duration{ GetTickCount() }}; + } + }; + } + + struct tick_count_clock : impl::tick_count_clock_impl {}; +#if _WIN32_WINNT >= 0x0600 + struct tick_count64_clock : impl::tick_count_clock_impl {}; +#endif // _WIN32_WINNT >= 0x0600 +#pragma endregion + + namespace impl + { + constexpr DWORD64 filetime_to_int(const FILETIME& ft) WI_NOEXCEPT + { + return (static_cast(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + } + + constexpr FILETIME filetime_from_int(DWORD64 t) WI_NOEXCEPT + { + return { + static_cast(t >> 32), + static_cast(t) + }; + } + } + +#pragma region std::chrono wrappers for GetSystemTime[Precise]AsFileTime + namespace impl + { + template + struct system_time_clock_impl + { + public: + using rep = LONGLONG; + using period = std::ratio_multiply; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool const is_steady = false; + + WI_NODISCARD static time_point now() WI_NOEXCEPT + { + alignas(rep) FILETIME ft; + GetSystemTime(&ft); + return from_filetime(ft); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 FILETIME to_filetime(const time_point& t) WI_NOEXCEPT + { + return filetime_from_int(static_cast(t.time_since_epoch().count())); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_filetime(const FILETIME& ft) WI_NOEXCEPT + { + return time_point{duration{ static_cast(filetime_to_int(ft)) }}; + } + +#if __WI_LIBCPP_STD_VER > 11 + static constexpr time_point unix_epoch{duration{116444736000000000LL}}; +#else + static time_point const unix_epoch; +#endif + + private: + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 Duration to_unix_epoch(const time_point& t) WI_NOEXCEPT + { + return std::chrono::duration_cast(t - unix_epoch); + } + + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_unix_epoch(const Duration& t) WI_NOEXCEPT + { + return std::chrono::time_point_cast(unix_epoch + t); + } + + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 Rep to_crt_time(const time_point& t) WI_NOEXCEPT + { + return to_unix_epoch>(t).count(); + } + + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_crt_time(Rep t) WI_NOEXCEPT + { + return from_unix_epoch(std::chrono::duration{t}); + } + + public: + template + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 std::chrono::time_point to_system_clock(const time_point& t) WI_NOEXCEPT + { + return std::chrono::time_point{to_unix_epoch(t)}; + } + + template + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_system_clock(const std::chrono::time_point& t) WI_NOEXCEPT + { + return from_unix_epoch(t.time_since_epoch()); + } + +#ifndef _CRT_NO_TIME_T + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::time_t to_time_t(const time_point& t) WI_NOEXCEPT + { + return to_crt_time<::time_t>(t); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time_t(::time_t t) WI_NOEXCEPT + { + return from_crt_time(t); + } +#endif + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time32_t to_time32_t(const time_point& t) WI_NOEXCEPT + { + return to_crt_time<::__time32_t>(t); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time32_t(::__time32_t t) WI_NOEXCEPT + { + return from_crt_time(t); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time64_t to_time64_t(const time_point& t) WI_NOEXCEPT + { + return to_crt_time<::__time64_t>(t); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time64_t(::__time64_t t) WI_NOEXCEPT + { + return from_crt_time(t); + } + }; + +#if __WI_LIBCPP_STD_VER <= 11 + template + typename system_time_clock_impl::time_point const system_time_clock_impl::unix_epoch{duration{116444736000000000LL}}; +#endif + } + + struct system_time_clock : impl::system_time_clock_impl<&::GetSystemTimeAsFileTime, system_time_clock> {}; + +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 + struct precise_system_time_clock : impl::system_time_clock_impl<&::GetSystemTimePreciseAsFileTime, system_time_clock> {}; + using high_precision_system_time_clock = precise_system_time_clock; +#else // _WIN32_WINNT < _WIN32_WINNT_WIN8 + using high_precision_system_time_clock = system_time_clock; +#endif // _WIN32_WINNT < _WIN32_WINNT_WIN8 +#pragma endregion + +#pragma region std::chrono wrappers for Query[Unbiased]InterruptTime[Precise] + namespace impl + { + template + struct interrupt_time_clock_impl + { + using rep = LONGLONG; + using period = std::ratio_multiply; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool const is_steady = true; + + WI_NODISCARD static time_point now() WI_NOEXCEPT + { + ULONGLONG t; + QueryInterruptTime(&t); + return time_point{duration{ static_cast(t) }}; + } + }; + +#if _WIN32_WINNT >= 0x0601 + VOID WINAPI QueryUnbiasedInterruptTime(_Out_ PULONGLONG UnbiasedTime) WI_NOEXCEPT + { + ::QueryUnbiasedInterruptTime(UnbiasedTime); + } +#endif + } + +#if _WIN32_WINNT >= 0x0601 + struct unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&impl::QueryUnbiasedInterruptTime, unbiased_interrupt_time_clock> {}; +#if defined(NTDDI_WIN10) + struct interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTime, interrupt_time_clock> {}; + struct precise_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTimePrecise, interrupt_time_clock> {}; + struct precise_unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryUnbiasedInterruptTimePrecise, unbiased_interrupt_time_clock> {}; +#endif +#endif +#pragma endregion + + using cpu_time_duration = std::chrono::duration>; + + enum class cpu_time + { + total, + kernel, + user, + }; + + struct execution_times + { + system_time_clock::time_point creation_time; + system_time_clock::time_point exit_time; + cpu_time_duration kernel_time; + cpu_time_duration user_time; + }; + + namespace impl + { + inline execution_times __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 execution_times_from_filetimes(FILETIME creation_time, FILETIME exit_time, FILETIME kernel_time, FILETIME user_time) WI_NOEXCEPT + { + return { + system_time_clock::from_filetime(creation_time), + system_time_clock::from_filetime(exit_time), + cpu_time_duration{ static_cast(impl::filetime_to_int(kernel_time)) }, + cpu_time_duration{ static_cast(impl::filetime_to_int(user_time)) } + }; + } + + inline cpu_time_duration __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 get_cpu_time(const execution_times& times, cpu_time kind) WI_NOEXCEPT + { + switch (kind) + { + case cpu_time::total: return times.kernel_time + times.user_time; + case cpu_time::kernel: return times.kernel_time; + case cpu_time::user: return times.user_time; + DEFAULT_UNREACHABLE; + } + } + } + + using thread_times = execution_times; + + template + WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) + { + FILETIME creation_time; + FILETIME exit_time; + FILETIME kernel_time; + FILETIME user_time; + + ErrorPolicy::Win32BOOL(::GetThreadTimes(thread, &creation_time, &exit_time, &kernel_time, &user_time)); + + return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); + } + + WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) + { + return get_thread_times(thread); + } + + template + WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) + { + return impl::get_cpu_time(get_thread_times(thread), kind); + } + + WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) + { + return get_thread_cpu_time(thread, kind); + } + + using process_times = execution_times; + + template + WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) + { + FILETIME creation_time; + FILETIME exit_time; + FILETIME kernel_time; + FILETIME user_time; + + ErrorPolicy::Win32BOOL(::GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time)); + + return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); + } + + WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) + { + return get_process_times(process); + } + + template + WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) + { + return impl::get_cpu_time(get_process_times(process), kind); + } + + WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) + { + return get_process_cpu_time(process, kind); + } + + struct current_thread_cpu_time_clock + { + using rep = cpu_time_duration::rep; + using period = cpu_time_duration::period; + using duration = cpu_time_duration; + using time_point = std::chrono::time_point; + + template + WI_NODISCARD static time_point now() + { + return time_point{ get_thread_cpu_time() }; + } + + WI_NODISCARD static time_point now() + { + return time_point{ now() }; + } + }; + + struct current_process_cpu_time_clock + { + using rep = cpu_time_duration::rep; + using period = cpu_time_duration::period; + using duration = cpu_time_duration; + using time_point = std::chrono::time_point; + + template + WI_NODISCARD static time_point now() + { + return time_point{ get_process_cpu_time() }; + } + + WI_NODISCARD static time_point now() + { + return time_point{ now() }; + } + }; +} + +#endif // __WIL_CHRONO_INCLUDED From 6274ca178bbb43b7565b496f93afe2cf9f856b00 Mon Sep 17 00:00:00 2001 From: hackbunny Date: Mon, 2 Sep 2019 21:26:10 +0200 Subject: [PATCH 02/11] Indentation: tab -> 4 spaces [As requested in the comments to PR #89](https://github.com/microsoft/wil/pull/89#discussion_r319776659) --- include/wil/chrono.h | 604 +++++++++++++++++++++---------------------- 1 file changed, 302 insertions(+), 302 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 642e22410..8bb97bfaf 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -31,347 +31,347 @@ namespace wil { #pragma region std::chrono wrappers for GetTickCount[64] - namespace impl - { - template - struct tick_count_clock_impl - { - using rep = ResultT; - using period = std::milli; - using duration = std::chrono::duration; - using time_point = std::chrono::time_point; - static constexpr bool const is_steady = true; - - WI_NODISCARD static time_point now() WI_NOEXCEPT - { - return time_point{duration{ GetTickCount() }}; - } - }; - } - - struct tick_count_clock : impl::tick_count_clock_impl {}; + namespace impl + { + template + struct tick_count_clock_impl + { + using rep = ResultT; + using period = std::milli; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool const is_steady = true; + + WI_NODISCARD static time_point now() WI_NOEXCEPT + { + return time_point{duration{ GetTickCount() }}; + } + }; + } + + struct tick_count_clock : impl::tick_count_clock_impl {}; #if _WIN32_WINNT >= 0x0600 - struct tick_count64_clock : impl::tick_count_clock_impl {}; + struct tick_count64_clock : impl::tick_count_clock_impl {}; #endif // _WIN32_WINNT >= 0x0600 #pragma endregion - namespace impl - { - constexpr DWORD64 filetime_to_int(const FILETIME& ft) WI_NOEXCEPT - { - return (static_cast(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; - } - - constexpr FILETIME filetime_from_int(DWORD64 t) WI_NOEXCEPT - { - return { - static_cast(t >> 32), - static_cast(t) - }; - } - } + namespace impl + { + constexpr DWORD64 filetime_to_int(const FILETIME& ft) WI_NOEXCEPT + { + return (static_cast(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + } + + constexpr FILETIME filetime_from_int(DWORD64 t) WI_NOEXCEPT + { + return { + static_cast(t >> 32), + static_cast(t) + }; + } + } #pragma region std::chrono wrappers for GetSystemTime[Precise]AsFileTime - namespace impl - { - template - struct system_time_clock_impl - { - public: - using rep = LONGLONG; - using period = std::ratio_multiply; - using duration = std::chrono::duration; - using time_point = std::chrono::time_point; - static constexpr bool const is_steady = false; - - WI_NODISCARD static time_point now() WI_NOEXCEPT - { - alignas(rep) FILETIME ft; - GetSystemTime(&ft); - return from_filetime(ft); - } - - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 FILETIME to_filetime(const time_point& t) WI_NOEXCEPT - { - return filetime_from_int(static_cast(t.time_since_epoch().count())); - } - - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_filetime(const FILETIME& ft) WI_NOEXCEPT - { - return time_point{duration{ static_cast(filetime_to_int(ft)) }}; - } + namespace impl + { + template + struct system_time_clock_impl + { + public: + using rep = LONGLONG; + using period = std::ratio_multiply; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool const is_steady = false; + + WI_NODISCARD static time_point now() WI_NOEXCEPT + { + alignas(rep) FILETIME ft; + GetSystemTime(&ft); + return from_filetime(ft); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 FILETIME to_filetime(const time_point& t) WI_NOEXCEPT + { + return filetime_from_int(static_cast(t.time_since_epoch().count())); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_filetime(const FILETIME& ft) WI_NOEXCEPT + { + return time_point{duration{ static_cast(filetime_to_int(ft)) }}; + } #if __WI_LIBCPP_STD_VER > 11 - static constexpr time_point unix_epoch{duration{116444736000000000LL}}; + static constexpr time_point unix_epoch{duration{116444736000000000LL}}; #else - static time_point const unix_epoch; + static time_point const unix_epoch; #endif - private: - template - static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 Duration to_unix_epoch(const time_point& t) WI_NOEXCEPT - { - return std::chrono::duration_cast(t - unix_epoch); - } - - template - static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_unix_epoch(const Duration& t) WI_NOEXCEPT - { - return std::chrono::time_point_cast(unix_epoch + t); - } - - template - static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 Rep to_crt_time(const time_point& t) WI_NOEXCEPT - { - return to_unix_epoch>(t).count(); - } - - template - static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_crt_time(Rep t) WI_NOEXCEPT - { - return from_unix_epoch(std::chrono::duration{t}); - } - - public: - template - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 std::chrono::time_point to_system_clock(const time_point& t) WI_NOEXCEPT - { - return std::chrono::time_point{to_unix_epoch(t)}; - } - - template - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_system_clock(const std::chrono::time_point& t) WI_NOEXCEPT - { - return from_unix_epoch(t.time_since_epoch()); - } + private: + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 Duration to_unix_epoch(const time_point& t) WI_NOEXCEPT + { + return std::chrono::duration_cast(t - unix_epoch); + } + + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_unix_epoch(const Duration& t) WI_NOEXCEPT + { + return std::chrono::time_point_cast(unix_epoch + t); + } + + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 Rep to_crt_time(const time_point& t) WI_NOEXCEPT + { + return to_unix_epoch>(t).count(); + } + + template + static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_crt_time(Rep t) WI_NOEXCEPT + { + return from_unix_epoch(std::chrono::duration{t}); + } + + public: + template + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 std::chrono::time_point to_system_clock(const time_point& t) WI_NOEXCEPT + { + return std::chrono::time_point{to_unix_epoch(t)}; + } + + template + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_system_clock(const std::chrono::time_point& t) WI_NOEXCEPT + { + return from_unix_epoch(t.time_since_epoch()); + } #ifndef _CRT_NO_TIME_T - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::time_t to_time_t(const time_point& t) WI_NOEXCEPT - { - return to_crt_time<::time_t>(t); - } - - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time_t(::time_t t) WI_NOEXCEPT - { - return from_crt_time(t); - } + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::time_t to_time_t(const time_point& t) WI_NOEXCEPT + { + return to_crt_time<::time_t>(t); + } + + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time_t(::time_t t) WI_NOEXCEPT + { + return from_crt_time(t); + } #endif - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time32_t to_time32_t(const time_point& t) WI_NOEXCEPT - { - return to_crt_time<::__time32_t>(t); - } + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time32_t to_time32_t(const time_point& t) WI_NOEXCEPT + { + return to_crt_time<::__time32_t>(t); + } - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time32_t(::__time32_t t) WI_NOEXCEPT - { - return from_crt_time(t); - } + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time32_t(::__time32_t t) WI_NOEXCEPT + { + return from_crt_time(t); + } - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time64_t to_time64_t(const time_point& t) WI_NOEXCEPT - { - return to_crt_time<::__time64_t>(t); - } + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time64_t to_time64_t(const time_point& t) WI_NOEXCEPT + { + return to_crt_time<::__time64_t>(t); + } - WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time64_t(::__time64_t t) WI_NOEXCEPT - { - return from_crt_time(t); - } - }; + WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 time_point from_time64_t(::__time64_t t) WI_NOEXCEPT + { + return from_crt_time(t); + } + }; #if __WI_LIBCPP_STD_VER <= 11 - template - typename system_time_clock_impl::time_point const system_time_clock_impl::unix_epoch{duration{116444736000000000LL}}; + template + typename system_time_clock_impl::time_point const system_time_clock_impl::unix_epoch{duration{116444736000000000LL}}; #endif - } + } - struct system_time_clock : impl::system_time_clock_impl<&::GetSystemTimeAsFileTime, system_time_clock> {}; + struct system_time_clock : impl::system_time_clock_impl<&::GetSystemTimeAsFileTime, system_time_clock> {}; #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - struct precise_system_time_clock : impl::system_time_clock_impl<&::GetSystemTimePreciseAsFileTime, system_time_clock> {}; - using high_precision_system_time_clock = precise_system_time_clock; + struct precise_system_time_clock : impl::system_time_clock_impl<&::GetSystemTimePreciseAsFileTime, system_time_clock> {}; + using high_precision_system_time_clock = precise_system_time_clock; #else // _WIN32_WINNT < _WIN32_WINNT_WIN8 - using high_precision_system_time_clock = system_time_clock; + using high_precision_system_time_clock = system_time_clock; #endif // _WIN32_WINNT < _WIN32_WINNT_WIN8 #pragma endregion #pragma region std::chrono wrappers for Query[Unbiased]InterruptTime[Precise] - namespace impl - { - template - struct interrupt_time_clock_impl - { - using rep = LONGLONG; - using period = std::ratio_multiply; - using duration = std::chrono::duration; - using time_point = std::chrono::time_point; - static constexpr bool const is_steady = true; - - WI_NODISCARD static time_point now() WI_NOEXCEPT - { - ULONGLONG t; - QueryInterruptTime(&t); - return time_point{duration{ static_cast(t) }}; - } - }; + namespace impl + { + template + struct interrupt_time_clock_impl + { + using rep = LONGLONG; + using period = std::ratio_multiply; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool const is_steady = true; + + WI_NODISCARD static time_point now() WI_NOEXCEPT + { + ULONGLONG t; + QueryInterruptTime(&t); + return time_point{duration{ static_cast(t) }}; + } + }; #if _WIN32_WINNT >= 0x0601 - VOID WINAPI QueryUnbiasedInterruptTime(_Out_ PULONGLONG UnbiasedTime) WI_NOEXCEPT - { - ::QueryUnbiasedInterruptTime(UnbiasedTime); - } + VOID WINAPI QueryUnbiasedInterruptTime(_Out_ PULONGLONG UnbiasedTime) WI_NOEXCEPT + { + ::QueryUnbiasedInterruptTime(UnbiasedTime); + } #endif - } + } #if _WIN32_WINNT >= 0x0601 - struct unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&impl::QueryUnbiasedInterruptTime, unbiased_interrupt_time_clock> {}; + struct unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&impl::QueryUnbiasedInterruptTime, unbiased_interrupt_time_clock> {}; #if defined(NTDDI_WIN10) - struct interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTime, interrupt_time_clock> {}; - struct precise_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTimePrecise, interrupt_time_clock> {}; - struct precise_unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryUnbiasedInterruptTimePrecise, unbiased_interrupt_time_clock> {}; + struct interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTime, interrupt_time_clock> {}; + struct precise_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTimePrecise, interrupt_time_clock> {}; + struct precise_unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryUnbiasedInterruptTimePrecise, unbiased_interrupt_time_clock> {}; #endif #endif #pragma endregion - using cpu_time_duration = std::chrono::duration>; - - enum class cpu_time - { - total, - kernel, - user, - }; - - struct execution_times - { - system_time_clock::time_point creation_time; - system_time_clock::time_point exit_time; - cpu_time_duration kernel_time; - cpu_time_duration user_time; - }; - - namespace impl - { - inline execution_times __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 execution_times_from_filetimes(FILETIME creation_time, FILETIME exit_time, FILETIME kernel_time, FILETIME user_time) WI_NOEXCEPT - { - return { - system_time_clock::from_filetime(creation_time), - system_time_clock::from_filetime(exit_time), - cpu_time_duration{ static_cast(impl::filetime_to_int(kernel_time)) }, - cpu_time_duration{ static_cast(impl::filetime_to_int(user_time)) } - }; - } - - inline cpu_time_duration __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 get_cpu_time(const execution_times& times, cpu_time kind) WI_NOEXCEPT - { - switch (kind) - { - case cpu_time::total: return times.kernel_time + times.user_time; - case cpu_time::kernel: return times.kernel_time; - case cpu_time::user: return times.user_time; - DEFAULT_UNREACHABLE; - } - } - } - - using thread_times = execution_times; - - template - WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) - { - FILETIME creation_time; - FILETIME exit_time; - FILETIME kernel_time; - FILETIME user_time; - - ErrorPolicy::Win32BOOL(::GetThreadTimes(thread, &creation_time, &exit_time, &kernel_time, &user_time)); - - return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); - } - - WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) - { - return get_thread_times(thread); - } - - template - WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) - { - return impl::get_cpu_time(get_thread_times(thread), kind); - } - - WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) - { - return get_thread_cpu_time(thread, kind); - } - - using process_times = execution_times; - - template - WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) - { - FILETIME creation_time; - FILETIME exit_time; - FILETIME kernel_time; - FILETIME user_time; - - ErrorPolicy::Win32BOOL(::GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time)); - - return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); - } - - WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) - { - return get_process_times(process); - } - - template - WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) - { - return impl::get_cpu_time(get_process_times(process), kind); - } - - WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) - { - return get_process_cpu_time(process, kind); - } - - struct current_thread_cpu_time_clock - { - using rep = cpu_time_duration::rep; - using period = cpu_time_duration::period; - using duration = cpu_time_duration; - using time_point = std::chrono::time_point; - - template - WI_NODISCARD static time_point now() - { - return time_point{ get_thread_cpu_time() }; - } - - WI_NODISCARD static time_point now() - { - return time_point{ now() }; - } - }; - - struct current_process_cpu_time_clock - { - using rep = cpu_time_duration::rep; - using period = cpu_time_duration::period; - using duration = cpu_time_duration; - using time_point = std::chrono::time_point; - - template - WI_NODISCARD static time_point now() - { - return time_point{ get_process_cpu_time() }; - } - - WI_NODISCARD static time_point now() - { - return time_point{ now() }; - } - }; + using cpu_time_duration = std::chrono::duration>; + + enum class cpu_time + { + total, + kernel, + user, + }; + + struct execution_times + { + system_time_clock::time_point creation_time; + system_time_clock::time_point exit_time; + cpu_time_duration kernel_time; + cpu_time_duration user_time; + }; + + namespace impl + { + inline execution_times __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 execution_times_from_filetimes(FILETIME creation_time, FILETIME exit_time, FILETIME kernel_time, FILETIME user_time) WI_NOEXCEPT + { + return { + system_time_clock::from_filetime(creation_time), + system_time_clock::from_filetime(exit_time), + cpu_time_duration{ static_cast(impl::filetime_to_int(kernel_time)) }, + cpu_time_duration{ static_cast(impl::filetime_to_int(user_time)) } + }; + } + + inline cpu_time_duration __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 get_cpu_time(const execution_times& times, cpu_time kind) WI_NOEXCEPT + { + switch (kind) + { + case cpu_time::total: return times.kernel_time + times.user_time; + case cpu_time::kernel: return times.kernel_time; + case cpu_time::user: return times.user_time; + DEFAULT_UNREACHABLE; + } + } + } + + using thread_times = execution_times; + + template + WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) + { + FILETIME creation_time; + FILETIME exit_time; + FILETIME kernel_time; + FILETIME user_time; + + ErrorPolicy::Win32BOOL(::GetThreadTimes(thread, &creation_time, &exit_time, &kernel_time, &user_time)); + + return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); + } + + WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) + { + return get_thread_times(thread); + } + + template + WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) + { + return impl::get_cpu_time(get_thread_times(thread), kind); + } + + WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) + { + return get_thread_cpu_time(thread, kind); + } + + using process_times = execution_times; + + template + WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) + { + FILETIME creation_time; + FILETIME exit_time; + FILETIME kernel_time; + FILETIME user_time; + + ErrorPolicy::Win32BOOL(::GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time)); + + return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); + } + + WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) + { + return get_process_times(process); + } + + template + WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) + { + return impl::get_cpu_time(get_process_times(process), kind); + } + + WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) + { + return get_process_cpu_time(process, kind); + } + + struct current_thread_cpu_time_clock + { + using rep = cpu_time_duration::rep; + using period = cpu_time_duration::period; + using duration = cpu_time_duration; + using time_point = std::chrono::time_point; + + template + WI_NODISCARD static time_point now() + { + return time_point{ get_thread_cpu_time() }; + } + + WI_NODISCARD static time_point now() + { + return time_point{ now() }; + } + }; + + struct current_process_cpu_time_clock + { + using rep = cpu_time_duration::rep; + using period = cpu_time_duration::period; + using duration = cpu_time_duration; + using time_point = std::chrono::time_point; + + template + WI_NODISCARD static time_point now() + { + return time_point{ get_process_cpu_time() }; + } + + WI_NODISCARD static time_point now() + { + return time_point{ now() }; + } + }; } #endif // __WIL_CHRONO_INCLUDED From 833302ff583f30eaf83d69f407b89847cdb012bc Mon Sep 17 00:00:00 2001 From: hackbunny Date: Mon, 2 Sep 2019 21:27:26 +0200 Subject: [PATCH 03/11] Refactoring: renamed namespace wil::impl to wil::details [As requested in the comments to PR #89](https://github.com/microsoft/wil/pull/89#discussion_r319776779) --- include/wil/chrono.h | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 8bb97bfaf..295a58eb3 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -31,7 +31,7 @@ namespace wil { #pragma region std::chrono wrappers for GetTickCount[64] - namespace impl + namespace details { template struct tick_count_clock_impl @@ -49,13 +49,13 @@ namespace wil }; } - struct tick_count_clock : impl::tick_count_clock_impl {}; + struct tick_count_clock : details::tick_count_clock_impl {}; #if _WIN32_WINNT >= 0x0600 - struct tick_count64_clock : impl::tick_count_clock_impl {}; + struct tick_count64_clock : details::tick_count_clock_impl {}; #endif // _WIN32_WINNT >= 0x0600 #pragma endregion - namespace impl + namespace details { constexpr DWORD64 filetime_to_int(const FILETIME& ft) WI_NOEXCEPT { @@ -72,7 +72,7 @@ namespace wil } #pragma region std::chrono wrappers for GetSystemTime[Precise]AsFileTime - namespace impl + namespace details { template struct system_time_clock_impl @@ -184,10 +184,10 @@ namespace wil #endif } - struct system_time_clock : impl::system_time_clock_impl<&::GetSystemTimeAsFileTime, system_time_clock> {}; + struct system_time_clock : details::system_time_clock_impl<&::GetSystemTimeAsFileTime, system_time_clock> {}; #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - struct precise_system_time_clock : impl::system_time_clock_impl<&::GetSystemTimePreciseAsFileTime, system_time_clock> {}; + struct precise_system_time_clock : details::system_time_clock_impl<&::GetSystemTimePreciseAsFileTime, system_time_clock> {}; using high_precision_system_time_clock = precise_system_time_clock; #else // _WIN32_WINNT < _WIN32_WINNT_WIN8 using high_precision_system_time_clock = system_time_clock; @@ -195,7 +195,7 @@ namespace wil #pragma endregion #pragma region std::chrono wrappers for Query[Unbiased]InterruptTime[Precise] - namespace impl + namespace details { template struct interrupt_time_clock_impl @@ -223,11 +223,11 @@ namespace wil } #if _WIN32_WINNT >= 0x0601 - struct unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&impl::QueryUnbiasedInterruptTime, unbiased_interrupt_time_clock> {}; + struct unbiased_interrupt_time_clock : details::interrupt_time_clock_impl<&details::QueryUnbiasedInterruptTime, unbiased_interrupt_time_clock> {}; #if defined(NTDDI_WIN10) - struct interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTime, interrupt_time_clock> {}; - struct precise_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryInterruptTimePrecise, interrupt_time_clock> {}; - struct precise_unbiased_interrupt_time_clock : impl::interrupt_time_clock_impl<&::QueryUnbiasedInterruptTimePrecise, unbiased_interrupt_time_clock> {}; + struct interrupt_time_clock : details::interrupt_time_clock_impl<&::QueryInterruptTime, interrupt_time_clock> {}; + struct precise_interrupt_time_clock : details::interrupt_time_clock_impl<&::QueryInterruptTimePrecise, interrupt_time_clock> {}; + struct precise_unbiased_interrupt_time_clock : details::interrupt_time_clock_impl<&::QueryUnbiasedInterruptTimePrecise, unbiased_interrupt_time_clock> {}; #endif #endif #pragma endregion @@ -249,15 +249,15 @@ namespace wil cpu_time_duration user_time; }; - namespace impl + namespace details { inline execution_times __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 execution_times_from_filetimes(FILETIME creation_time, FILETIME exit_time, FILETIME kernel_time, FILETIME user_time) WI_NOEXCEPT { return { system_time_clock::from_filetime(creation_time), system_time_clock::from_filetime(exit_time), - cpu_time_duration{ static_cast(impl::filetime_to_int(kernel_time)) }, - cpu_time_duration{ static_cast(impl::filetime_to_int(user_time)) } + cpu_time_duration{ static_cast(details::filetime_to_int(kernel_time)) }, + cpu_time_duration{ static_cast(details::filetime_to_int(user_time)) } }; } @@ -285,7 +285,7 @@ namespace wil ErrorPolicy::Win32BOOL(::GetThreadTimes(thread, &creation_time, &exit_time, &kernel_time, &user_time)); - return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); + return details::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); } WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) @@ -296,7 +296,7 @@ namespace wil template WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) { - return impl::get_cpu_time(get_thread_times(thread), kind); + return details::get_cpu_time(get_thread_times(thread), kind); } WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) @@ -316,7 +316,7 @@ namespace wil ErrorPolicy::Win32BOOL(::GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time)); - return impl::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); + return details::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); } WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) @@ -327,7 +327,7 @@ namespace wil template WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) { - return impl::get_cpu_time(get_process_times(process), kind); + return details::get_cpu_time(get_process_times(process), kind); } WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) From a48067820fa8ba17ec25836f843fc3ae18cddd31 Mon Sep 17 00:00:00 2001 From: hackbunny Date: Mon, 2 Sep 2019 21:49:46 +0200 Subject: [PATCH 04/11] Refactored to match WIL coding standard Changed template parameter names from Pascal case to snake case, with _t suffixes where appropriate Split function template parameters into a type parameter and a value parameter; as a result, the details::QueryUnbiasedInterruptTime wrapper is now unnecessary [As requested in the comments to PR #89](https://github.com/microsoft/wil/pull/89#discussion_r319776770) --- include/wil/chrono.h | 71 ++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 295a58eb3..94f597125 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -33,25 +33,25 @@ namespace wil #pragma region std::chrono wrappers for GetTickCount[64] namespace details { - template + template struct tick_count_clock_impl { - using rep = ResultT; + using rep = rep_t; using period = std::milli; using duration = std::chrono::duration; - using time_point = std::chrono::time_point; + using time_point = std::chrono::time_point; static constexpr bool const is_steady = true; WI_NODISCARD static time_point now() WI_NOEXCEPT { - return time_point{duration{ GetTickCount() }}; + return time_point{duration{ get_tick_count_fn() }}; } }; } - struct tick_count_clock : details::tick_count_clock_impl {}; + struct tick_count_clock : details::tick_count_clock_impl {}; #if _WIN32_WINNT >= 0x0600 - struct tick_count64_clock : details::tick_count_clock_impl {}; + struct tick_count64_clock : details::tick_count_clock_impl {}; #endif // _WIN32_WINNT >= 0x0600 #pragma endregion @@ -74,20 +74,20 @@ namespace wil #pragma region std::chrono wrappers for GetSystemTime[Precise]AsFileTime namespace details { - template + template struct system_time_clock_impl { public: using rep = LONGLONG; using period = std::ratio_multiply; using duration = std::chrono::duration; - using time_point = std::chrono::time_point; + using time_point = std::chrono::time_point; static constexpr bool const is_steady = false; WI_NODISCARD static time_point now() WI_NOEXCEPT { alignas(rep) FILETIME ft; - GetSystemTime(&ft); + get_system_time_fn(&ft); return from_filetime(ft); } @@ -179,15 +179,15 @@ namespace wil }; #if __WI_LIBCPP_STD_VER <= 11 - template - typename system_time_clock_impl::time_point const system_time_clock_impl::unix_epoch{duration{116444736000000000LL}}; + template + typename system_time_clock_impl::time_point const system_time_clock_impl::unix_epoch{duration{116444736000000000LL}}; #endif } - struct system_time_clock : details::system_time_clock_impl<&::GetSystemTimeAsFileTime, system_time_clock> {}; + struct system_time_clock : details::system_time_clock_impl {}; #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - struct precise_system_time_clock : details::system_time_clock_impl<&::GetSystemTimePreciseAsFileTime, system_time_clock> {}; + struct precise_system_time_clock : details::system_time_clock_impl {}; using high_precision_system_time_clock = precise_system_time_clock; #else // _WIN32_WINNT < _WIN32_WINNT_WIN8 using high_precision_system_time_clock = system_time_clock; @@ -197,37 +197,30 @@ namespace wil #pragma region std::chrono wrappers for Query[Unbiased]InterruptTime[Precise] namespace details { - template + template struct interrupt_time_clock_impl { using rep = LONGLONG; using period = std::ratio_multiply; using duration = std::chrono::duration; - using time_point = std::chrono::time_point; + using time_point = std::chrono::time_point; static constexpr bool const is_steady = true; WI_NODISCARD static time_point now() WI_NOEXCEPT { ULONGLONG t; - QueryInterruptTime(&t); + query_interrupt_time_fn(&t); return time_point{duration{ static_cast(t) }}; } }; - -#if _WIN32_WINNT >= 0x0601 - VOID WINAPI QueryUnbiasedInterruptTime(_Out_ PULONGLONG UnbiasedTime) WI_NOEXCEPT - { - ::QueryUnbiasedInterruptTime(UnbiasedTime); - } -#endif } #if _WIN32_WINNT >= 0x0601 - struct unbiased_interrupt_time_clock : details::interrupt_time_clock_impl<&details::QueryUnbiasedInterruptTime, unbiased_interrupt_time_clock> {}; + struct unbiased_interrupt_time_clock : details::interrupt_time_clock_impl {}; #if defined(NTDDI_WIN10) - struct interrupt_time_clock : details::interrupt_time_clock_impl<&::QueryInterruptTime, interrupt_time_clock> {}; - struct precise_interrupt_time_clock : details::interrupt_time_clock_impl<&::QueryInterruptTimePrecise, interrupt_time_clock> {}; - struct precise_unbiased_interrupt_time_clock : details::interrupt_time_clock_impl<&::QueryUnbiasedInterruptTimePrecise, unbiased_interrupt_time_clock> {}; + struct interrupt_time_clock : details::interrupt_time_clock_impl {}; + struct precise_interrupt_time_clock : details::interrupt_time_clock_impl {}; + struct precise_unbiased_interrupt_time_clock : details::interrupt_time_clock_impl {}; #endif #endif #pragma endregion @@ -275,7 +268,7 @@ namespace wil using thread_times = execution_times; - template + template WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) { FILETIME creation_time; @@ -283,7 +276,7 @@ namespace wil FILETIME kernel_time; FILETIME user_time; - ErrorPolicy::Win32BOOL(::GetThreadTimes(thread, &creation_time, &exit_time, &kernel_time, &user_time)); + error_policy::Win32BOOL(::GetThreadTimes(thread, &creation_time, &exit_time, &kernel_time, &user_time)); return details::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); } @@ -293,10 +286,10 @@ namespace wil return get_thread_times(thread); } - template + template WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) { - return details::get_cpu_time(get_thread_times(thread), kind); + return details::get_cpu_time(get_thread_times(thread), kind); } WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) @@ -306,7 +299,7 @@ namespace wil using process_times = execution_times; - template + template WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) { FILETIME creation_time; @@ -314,7 +307,7 @@ namespace wil FILETIME kernel_time; FILETIME user_time; - ErrorPolicy::Win32BOOL(::GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time)); + error_policy::Win32BOOL(::GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time)); return details::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); } @@ -324,10 +317,10 @@ namespace wil return get_process_times(process); } - template + template WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) { - return details::get_cpu_time(get_process_times(process), kind); + return details::get_cpu_time(get_process_times(process), kind); } WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) @@ -342,10 +335,10 @@ namespace wil using duration = cpu_time_duration; using time_point = std::chrono::time_point; - template + template WI_NODISCARD static time_point now() { - return time_point{ get_thread_cpu_time() }; + return time_point{ get_thread_cpu_time() }; } WI_NODISCARD static time_point now() @@ -361,10 +354,10 @@ namespace wil using duration = cpu_time_duration; using time_point = std::chrono::time_point; - template + template WI_NODISCARD static time_point now() { - return time_point{ get_process_cpu_time() }; + return time_point{ get_process_cpu_time() }; } WI_NODISCARD static time_point now() From 30de32557f1011d577d520899995d6f13770bebd Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Wed, 4 Sep 2019 21:21:45 +0200 Subject: [PATCH 05/11] Syntax Moved the optional constexpr specifiers of details::execution_times_from_filetimes and details::get_cpu_time to their appropriate places --- include/wil/chrono.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 94f597125..e6b334faf 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -244,7 +244,7 @@ namespace wil namespace details { - inline execution_times __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 execution_times_from_filetimes(FILETIME creation_time, FILETIME exit_time, FILETIME kernel_time, FILETIME user_time) WI_NOEXCEPT + inline __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 execution_times execution_times_from_filetimes(FILETIME creation_time, FILETIME exit_time, FILETIME kernel_time, FILETIME user_time) WI_NOEXCEPT { return { system_time_clock::from_filetime(creation_time), @@ -254,7 +254,7 @@ namespace wil }; } - inline cpu_time_duration __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 get_cpu_time(const execution_times& times, cpu_time kind) WI_NOEXCEPT + inline __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 cpu_time_duration get_cpu_time(const execution_times& times, cpu_time kind) WI_NOEXCEPT { switch (kind) { From 026f60081e631827b8b65591b96e82eef233a92e Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Wed, 4 Sep 2019 21:22:51 +0200 Subject: [PATCH 06/11] Inline functions Added the inline specifier to the inline functions that were missing it --- include/wil/chrono.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index e6b334faf..3af5380c0 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -281,7 +281,7 @@ namespace wil return details::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); } - WI_NODISCARD thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) + WI_NODISCARD inline thread_times get_thread_times(HANDLE thread = ::GetCurrentThread()) { return get_thread_times(thread); } @@ -292,7 +292,7 @@ namespace wil return details::get_cpu_time(get_thread_times(thread), kind); } - WI_NODISCARD cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) + WI_NODISCARD inline cpu_time_duration get_thread_cpu_time(HANDLE thread = ::GetCurrentThread(), cpu_time kind = cpu_time::total) { return get_thread_cpu_time(thread, kind); } @@ -312,7 +312,7 @@ namespace wil return details::execution_times_from_filetimes(creation_time, exit_time, kernel_time, user_time); } - WI_NODISCARD process_times get_process_times(HANDLE process = ::GetCurrentProcess()) + WI_NODISCARD inline process_times get_process_times(HANDLE process = ::GetCurrentProcess()) { return get_process_times(process); } @@ -323,7 +323,7 @@ namespace wil return details::get_cpu_time(get_process_times(process), kind); } - WI_NODISCARD cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) + WI_NODISCARD inline cpu_time_duration get_process_cpu_time(HANDLE process = ::GetCurrentProcess(), cpu_time kind = cpu_time::total) { return get_process_cpu_time(process, kind); } From 894f028ab2b4ddeee6c9b3341efb874c1e99b262 Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Wed, 4 Sep 2019 21:59:19 +0200 Subject: [PATCH 07/11] Comments Added clarifying comments to some #endifs --- include/wil/chrono.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 3af5380c0..5ab2b4220 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -155,7 +155,7 @@ namespace wil { return from_crt_time(t); } -#endif +#endif // !defined(_CRT_NO_TIME_T) WI_NODISCARD static __WI_LIBCPP_CONSTEXPR_AFTER_CXX11 ::__time32_t to_time32_t(const time_point& t) WI_NOEXCEPT { @@ -221,8 +221,8 @@ namespace wil struct interrupt_time_clock : details::interrupt_time_clock_impl {}; struct precise_interrupt_time_clock : details::interrupt_time_clock_impl {}; struct precise_unbiased_interrupt_time_clock : details::interrupt_time_clock_impl {}; -#endif -#endif +#endif // defined(NTDDI_WIN10) +#endif // _WIN32_WINNT >= 0x0601 #pragma endregion using cpu_time_duration = std::chrono::duration>; From 4a18fc0c4feadf59589192b88d9b2ff2150cb791 Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Wed, 4 Sep 2019 22:01:00 +0200 Subject: [PATCH 08/11] Code style Don't mix typename and class in template parameter lists --- include/wil/chrono.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 5ab2b4220..ac7306840 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -197,7 +197,7 @@ namespace wil #pragma region std::chrono wrappers for Query[Unbiased]InterruptTime[Precise] namespace details { - template + template struct interrupt_time_clock_impl { using rep = LONGLONG; From ce6684ec171dc7a708a139d9b9d182d05965d323 Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Wed, 4 Sep 2019 23:52:54 +0200 Subject: [PATCH 09/11] region/endregion Delimit the code regions for thread/process times functions and thread/process CPU time clocks --- include/wil/chrono.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index ac7306840..20079045b 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -225,6 +225,7 @@ namespace wil #endif // _WIN32_WINNT >= 0x0601 #pragma endregion +#pragma region std::chrono wrappers for thread/process times using cpu_time_duration = std::chrono::duration>; enum class cpu_time @@ -327,7 +328,9 @@ namespace wil { return get_process_cpu_time(process, kind); } +#pragma endregion +#pragma region Thread/process CPU clocks struct current_thread_cpu_time_clock { using rep = cpu_time_duration::rep; @@ -365,6 +368,7 @@ namespace wil return time_point{ now() }; } }; +#pragma endregion } #endif // __WIL_CHRONO_INCLUDED From 0c5885d27e7b322b36ed7a77f6801ab3afb705d8 Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Wed, 4 Sep 2019 23:53:54 +0200 Subject: [PATCH 10/11] Refactoring In the non-template overload of current_thread_cpu_time_clock::now, call the non-template overload of get_thread_cpu_time; in the non-template overload of current_process_cpu_time_clock::now, call the non-template overload get_process_cpu_time. This makes sure the two methods inherit the error policy of the underlying functions --- include/wil/chrono.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 20079045b..8fc1a611d 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -346,7 +346,7 @@ namespace wil WI_NODISCARD static time_point now() { - return time_point{ now() }; + return time_point{ get_thread_cpu_time() }; } }; @@ -365,7 +365,7 @@ namespace wil WI_NODISCARD static time_point now() { - return time_point{ now() }; + return time_point{ get_process_cpu_time() }; } }; #pragma endregion From 42ef1f097d71b528b5dd97ab62ee4802dcd1a7af Mon Sep 17 00:00:00 2001 From: hackbunny <7533668+hackbunny@users.noreply.github.com> Date: Mon, 28 Oct 2019 21:06:01 +0100 Subject: [PATCH 11/11] Refactoring constexpr implies const --- include/wil/chrono.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/wil/chrono.h b/include/wil/chrono.h index 8fc1a611d..423464786 100644 --- a/include/wil/chrono.h +++ b/include/wil/chrono.h @@ -40,7 +40,7 @@ namespace wil using period = std::milli; using duration = std::chrono::duration; using time_point = std::chrono::time_point; - static constexpr bool const is_steady = true; + static constexpr bool is_steady = true; WI_NODISCARD static time_point now() WI_NOEXCEPT { @@ -82,7 +82,7 @@ namespace wil using period = std::ratio_multiply; using duration = std::chrono::duration; using time_point = std::chrono::time_point; - static constexpr bool const is_steady = false; + static constexpr bool is_steady = false; WI_NODISCARD static time_point now() WI_NOEXCEPT { @@ -204,7 +204,7 @@ namespace wil using period = std::ratio_multiply; using duration = std::chrono::duration; using time_point = std::chrono::time_point; - static constexpr bool const is_steady = true; + static constexpr bool is_steady = true; WI_NODISCARD static time_point now() WI_NOEXCEPT {