From 4417b8bf3eef8ca7cf45dc973ab107249bd10d67 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 10 Apr 2018 15:11:10 +0200 Subject: [PATCH] core: add nm_utils_get_monotonic_timestamp_ns_cached() helper Add a helper function to cache the current timestamp and return it. The caching is a performance optimization, but it serves a much more important purpose: repeatedly getting the timestamp likely will yield different timings. So, commonly, within a certain context we want to get the current time once, and stick to that as "now". --- src/nm-core-utils.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index d974c8af36..33773ab679 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -239,6 +239,13 @@ gint64 nm_utils_get_monotonic_timestamp_ms (void); gint32 nm_utils_get_monotonic_timestamp_s (void); gint64 nm_utils_monotonic_timestamp_as_boottime (gint64 timestamp, gint64 timestamp_ticks_per_ns); +static inline gint64 +nm_utils_get_monotonic_timestamp_ns_cached (gint64 *cache_now) +{ + return (*cache_now) + ?: (*cache_now = nm_utils_get_monotonic_timestamp_ns ()); +} + gboolean nm_utils_is_valid_path_component (const char *name); const char *NM_ASSERT_VALID_PATH_COMPONENT (const char *name);