From b0c4b2824693fe6a27ea9439ec7a6328a0e23704 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Fri, 12 Nov 2021 09:43:07 +0100
Subject: [PATCH] pid1: add a manager_trigger_run_queue() helper

We have two different places where we re-trigger the run queue now.
let's unify it under a common function, that is part of the Manager
code.

Follow-up for #20953

Conflict:code context adaptation
Reference:https://github.com/systemd/systemd-stable/commit/b0c4b2824693fe6a27ea9439ec7a6328a0e23704
---
 src/core/job.c     |  8 ++------
 src/core/manager.c | 12 ++++++++++++
 src/core/manager.h |  2 ++
 src/core/mount.c   |  9 +++------
 4 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/src/core/job.c b/src/core/job.c
index 6dd01a6f49..b7ac75ac71 100644
--- a/src/core/job.c
+++ b/src/core/job.c
@@ -1107,17 +1107,13 @@ void job_add_to_run_queue(Job *j) {
         if (j->in_run_queue)
                 return;
 
-        if (prioq_isempty(j->manager->run_queue)) {
-                r = sd_event_source_set_enabled(j->manager->run_queue_event_source, SD_EVENT_ONESHOT);
-                if (r < 0)
-                        log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m");
-        }
-
         r = prioq_put(j->manager->run_queue, j, &j->run_queue_idx);
         if (r < 0)
                 log_warning_errno(r, "Failed put job in run queue, ignoring: %m");
         else
                 j->in_run_queue = true;
+
+        manager_trigger_run_queue(j->manager);
 }
 
 void job_add_to_dbus_queue(Job *j) {
diff --git a/src/core/manager.c b/src/core/manager.c
index 61895ab092..b21747daea 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -2248,6 +2248,18 @@ static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) {
         return 1;
 }
 
+void manager_trigger_run_queue(Manager *m) {
+        int r;
+
+        assert(m);
+
+        r = sd_event_source_set_enabled(
+                        m->run_queue_event_source,
+                        prioq_isempty(m->run_queue) ? SD_EVENT_OFF : SD_EVENT_ONESHOT);
+        if (r < 0)
+                log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m");
+}
+
 static unsigned manager_dispatch_dbus_queue(Manager *m) {
         unsigned n = 0, budget;
         Unit *u;
diff --git a/src/core/manager.h b/src/core/manager.h
index 29ce812121..c9230cb5c4 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -507,6 +507,8 @@ int manager_get_effective_environment(Manager *m, char ***ret);
 
 int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit);
 
+void manager_trigger_run_queue(Manager *m);
+
 int manager_loop(Manager *m);
 
 int manager_open_serialization(Manager *m, FILE **_f);
diff --git a/src/core/mount.c b/src/core/mount.c
index 3463641c6c..4f76b552c2 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -1840,15 +1840,12 @@ static bool mount_is_mounted(Mount *m) {
 
 static int mount_on_ratelimit_expire(sd_event_source *s, void *userdata) {
         Manager *m = userdata;
-        int r;
 
         assert(m);
 
-        /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so let's
-         * make sure we dispatch them in the next iteration. */
-        r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_ONESHOT);
-        if (r < 0)
-                log_debug_errno(r, "Failed to enable run queue event source, ignoring: %m");
+        /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
+         * let's make sure we dispatch them in the next iteration. */
+        manager_trigger_run_queue(m);
 
         return 0;
 }
-- 
2.33.0

