summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-03-10 21:49:30 +0100
committerThomas Haller <thaller@redhat.com>2023-03-21 15:58:36 +0100
commit684760a671c61561147e3383acec5a73f324f858 (patch)
tree49f9f31f74608b24950ef2833486a56c1ad7d480
parent9b48c7f3735a034a93b1734242ebd58b622766bb (diff)
glib-aux: add nm_g_timeout_reschedule() helper
-rw-r--r--src/libnm-glib-aux/nm-shared-utils.c45
-rw-r--r--src/libnm-glib-aux/nm-shared-utils.h6
2 files changed, 51 insertions, 0 deletions
diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c
index c71d3dc190..8b255101b3 100644
--- a/src/libnm-glib-aux/nm-shared-utils.c
+++ b/src/libnm-glib-aux/nm-shared-utils.c
@@ -20,6 +20,7 @@
#include "c-list/src/c-list.h"
#include "nm-errno.h"
+#include "nm-time-utils.h"
#include "nm-str-buf.h"
#include "nm-time-utils.h"
@@ -4840,6 +4841,50 @@ nm_g_child_watch_source_new(GPid pid,
return source;
}
+gboolean
+nm_g_timeout_reschedule(GSource **src,
+ gint64 *p_expiry_msec,
+ gint64 expiry_msec,
+ GSourceFunc func,
+ gpointer user_data)
+{
+ gint64 now_msec;
+ gint64 timeout_msec;
+
+ /* (Re-)Schedules a timeout at "expiry_msec" (in
+ * nm_utils_get_monotonic_timestamp_msec() scale).
+ *
+ * If a source is already scheduled in "*src" and "*p_expiry_msec" is
+ * identical to "expiry_msec", then we assume the timer is already ticking,
+ * and nothing is rescheduled.
+ *
+ * Otherwise, "*src" gets cancelled (if any), a new timer is scheduled
+ * (assigned to "*src") and the new expiry is written to "*p_expiry_msec".
+ */
+
+ nm_assert(src);
+ nm_assert(p_expiry_msec);
+
+ if (*src) {
+ if (*p_expiry_msec == expiry_msec) {
+ /* already scheduled with same expiry. */
+ return FALSE;
+ }
+ nm_clear_g_source_inst(src);
+ }
+
+ now_msec = nm_utils_get_monotonic_timestamp_msec();
+
+ if (expiry_msec <= now_msec)
+ timeout_msec = 0;
+ else
+ timeout_msec = NM_MIN(expiry_msec - now_msec, (gint64) G_MAXUINT);
+
+ *p_expiry_msec = expiry_msec;
+ *src = nm_g_timeout_add_source(timeout_msec, func, user_data);
+ return TRUE;
+}
+
/*****************************************************************************/
#define _CTX_LOG(fmt, ...) \
diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h
index 05d96cbd7e..d9f1d7b008 100644
--- a/src/libnm-glib-aux/nm-shared-utils.h
+++ b/src/libnm-glib-aux/nm-shared-utils.h
@@ -1543,6 +1543,12 @@ nm_g_timeout_add_source(guint timeout_msec, GSourceFunc func, gpointer user_data
NULL);
}
+gboolean nm_g_timeout_reschedule(GSource **src,
+ gint64 *p_expiry_msec,
+ gint64 expiry_msec,
+ GSourceFunc func,
+ gpointer user_data);
+
static inline GSource *
nm_g_timeout_add_seconds_source(guint timeout_sec, GSourceFunc func, gpointer user_data)
{