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