592caf
From 88b2618e4de850060a1c5c22b049e6de0578fbb5 Mon Sep 17 00:00:00 2001
592caf
From: Lennart Poettering <lennart@poettering.net>
592caf
Date: Mon, 23 Nov 2020 15:25:35 +0100
592caf
Subject: [PATCH] sd-event: split out code to add/remove timer event sources to
592caf
 earliest/latest prioq
592caf
592caf
Just some refactoring that makes code prettier, and will come handy
592caf
later, because we can reuse these functions at more places.
592caf
592caf
(cherry picked from commit 1e45e3fecc303e7ae9946220c742f69675e99c34)
592caf
592caf
Related: #1819868
592caf
---
592caf
 src/libsystemd/sd-event/sd-event.c | 57 +++++++++++++++++++++---------
592caf
 1 file changed, 41 insertions(+), 16 deletions(-)
592caf
592caf
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
592caf
index dc78dc7291..de9bb02059 100644
592caf
--- a/src/libsystemd/sd-event/sd-event.c
592caf
+++ b/src/libsystemd/sd-event/sd-event.c
592caf
@@ -913,6 +913,19 @@ static void event_source_time_prioq_reshuffle(sd_event_source *s) {
592caf
         d->needs_rearm = true;
592caf
 }
592caf
 
592caf
+static void event_source_time_prioq_remove(
592caf
+                sd_event_source *s,
592caf
+                struct clock_data *d) {
592caf
+
592caf
+        assert(s);
592caf
+        assert(d);
592caf
+
592caf
+        prioq_remove(d->earliest, s, &s->time.earliest_index);
592caf
+        prioq_remove(d->latest, s, &s->time.latest_index);
592caf
+        s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
592caf
+        d->needs_rearm = true;
592caf
+}
592caf
+
592caf
 static void source_disconnect(sd_event_source *s) {
592caf
         sd_event *event;
592caf
 
592caf
@@ -937,13 +950,8 @@ static void source_disconnect(sd_event_source *s) {
592caf
         case SOURCE_TIME_REALTIME_ALARM:
592caf
         case SOURCE_TIME_BOOTTIME_ALARM: {
592caf
                 struct clock_data *d;
592caf
-
592caf
-                d = event_get_clock_data(s->event, s->type);
592caf
-                assert(d);
592caf
-
592caf
-                prioq_remove(d->earliest, s, &s->time.earliest_index);
592caf
-                prioq_remove(d->latest, s, &s->time.latest_index);
592caf
-                d->needs_rearm = true;
592caf
+                assert_se(d = event_get_clock_data(s->event, s->type));
592caf
+                event_source_time_prioq_remove(s, d);
592caf
                 break;
592caf
         }
592caf
 
592caf
@@ -1254,6 +1262,30 @@ static int setup_clock_data(sd_event *e, struct clock_data *d, clockid_t clock)
592caf
         return 0;
592caf
 }
592caf
 
592caf
+static int event_source_time_prioq_put(
592caf
+                sd_event_source *s,
592caf
+                struct clock_data *d) {
592caf
+
592caf
+        int r;
592caf
+
592caf
+        assert(s);
592caf
+        assert(d);
592caf
+
592caf
+        r = prioq_put(d->earliest, s, &s->time.earliest_index);
592caf
+        if (r < 0)
592caf
+                return r;
592caf
+
592caf
+        r = prioq_put(d->latest, s, &s->time.latest_index);
592caf
+        if (r < 0) {
592caf
+                assert_se(prioq_remove(d->earliest, s, &s->time.earliest_index) > 0);
592caf
+                s->time.earliest_index = PRIOQ_IDX_NULL;
592caf
+                return r;
592caf
+        }
592caf
+
592caf
+        d->needs_rearm = true;
592caf
+        return 0;
592caf
+}
592caf
+
592caf
 _public_ int sd_event_add_time(
592caf
                 sd_event *e,
592caf
                 sd_event_source **ret,
592caf
@@ -1284,8 +1316,7 @@ _public_ int sd_event_add_time(
592caf
         if (!callback)
592caf
                 callback = time_exit_callback;
592caf
 
592caf
-        d = event_get_clock_data(e, type);
592caf
-        assert(d);
592caf
+        assert_se(d = event_get_clock_data(e, type));
592caf
 
592caf
         r = setup_clock_data(e, d, clock);
592caf
         if (r < 0)
592caf
@@ -1302,13 +1333,7 @@ _public_ int sd_event_add_time(
592caf
         s->userdata = userdata;
592caf
         s->enabled = SD_EVENT_ONESHOT;
592caf
 
592caf
-        d->needs_rearm = true;
592caf
-
592caf
-        r = prioq_put(d->earliest, s, &s->time.earliest_index);
592caf
-        if (r < 0)
592caf
-                goto fail;
592caf
-
592caf
-        r = prioq_put(d->latest, s, &s->time.latest_index);
592caf
+        r = event_source_time_prioq_put(s, d);
592caf
         if (r < 0)
592caf
                 goto fail;
592caf