util: uvw_clock_id and gettime

This commit is contained in:
Michele Caini 2023-05-19 14:23:56 +02:00
parent f0ac08cb42
commit 5e4694abba
3 changed files with 19 additions and 0 deletions

View File

@ -322,6 +322,12 @@ UVW_INLINE resource_usage utilities::rusage() noexcept {
return err ? resource_usage{} : ru;
}
UVW_INLINE timespec64 utilities::gettime(clock_id source) noexcept {
timespec64 ts;
auto err = uv_clock_gettime(static_cast<uv_clock_id>(source), &ts);
return err ? timespec64{} : ts;
}
UVW_INLINE uint64_t utilities::hrtime() noexcept {
return uv_hrtime();
}

View File

@ -38,6 +38,11 @@ enum class uvw_handle_type : std::underlying_type_t<uv_handle_type> {
FILE = UV_FILE
};
enum class uvw_clock_id : std::underlying_type_t<uv_clock_id> {
MONOTONIC = UV_CLOCK_MONOTONIC,
REALTIME = UV_CLOCK_REALTIME
};
template<typename T>
struct uv_type_wrapper {
using Type = T;
@ -603,6 +608,13 @@ struct utilities {
*/
static resource_usage rusage() noexcept;
/**
* @brief Gets the current system time from a high-resolution clock source.
* @param source Clock source, either real-time or monotonic.
* @return Current system time from the given high-resolution clock source.
*/
static timespec64 gettime(clock_id source) noexcept;
/**
* @brief Gets the current high-resolution real time.
*

View File

@ -83,6 +83,7 @@ TEST(Util, Utilities) {
ASSERT_NE(uvw::utilities::total_memory(), decltype(uvw::utilities::total_memory()){0});
ASSERT_NE(uvw::utilities::uptime(), decltype(uvw::utilities::uptime()){0});
ASSERT_NO_THROW(uvw::utilities::rusage());
ASSERT_NO_THROW(uvw::utilities::gettime(uvw::clock_id::MONOTONIC));
ASSERT_NE(uvw::utilities::hrtime(), decltype(uvw::utilities::hrtime()){0});
ASSERT_FALSE(uvw::utilities::path().empty());
ASSERT_FALSE(uvw::utilities::cwd().empty());