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