From 89621050e32c0462e5fc8f27e646ddc68bf2616e Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 10 Sep 2014 09:35:23 +0400 Subject: [PATCH] (mc_time_elapsed): add new function to detect elapsed time intervals. The author of initial patch is Egmont Koblinger. Signed-off-by: Andrew Borodin --- lib/util.c | 26 ++++++++++++++++++++++++++ lib/util.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/lib/util.c b/lib/util.c index ceace41f1..843efc441 100644 --- a/lib/util.c +++ b/lib/util.c @@ -52,6 +52,7 @@ #include "lib/vfs/vfs.h" #include "lib/strutil.h" #include "lib/util.h" +#include "lib/timer.h" /*** global variables ****************************************************************************/ @@ -1451,3 +1452,28 @@ mc_replace_error (GError ** dest, int code, const char *format, ...) } /* --------------------------------------------------------------------------------------------- */ + +/** + * Returns if the given duration has elapsed since the given timestamp, + * and if it has then updates the timestamp. + * + * @param timestamp the last timestamp in microseconds, updated if the given time elapsed + * @param deleay amount of time in microseconds + + * @return TRUE if clock skew detected, FALSE otherwise + */ +gboolean +mc_time_elapsed (guint64 * timestamp, guint64 delay) +{ + guint64 now; + + now = mc_timer_elapsed (mc_global.timer); + + if (now >= *timestamp && now < *timestamp + delay) + return FALSE; + + *timestamp = now; + return TRUE; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/util.h b/lib/util.h index 213f7d8fa..a8acfed82 100644 --- a/lib/util.h +++ b/lib/util.h @@ -249,6 +249,8 @@ char *mc_build_filenamev (const char *first_element, va_list args); void mc_propagate_error (GError ** dest, int code, const char *format, ...); void mc_replace_error (GError ** dest, int code, const char *format, ...); +gboolean mc_time_elapsed (guint64 * timestamp, guint64 delay); + /*** inline functions **************************************************/ static inline gboolean