From: Michael Tremer Date: Thu, 26 Jun 2025 13:50:22 +0000 (+0000) Subject: httpclient: Automatically detect whether the event loop is running X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=220fa9d8214651cf368c11fb72cd3a484488ef5b;p=pakfire.git httpclient: Automatically detect whether the event loop is running Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/httpclient.c b/src/pakfire/httpclient.c index 792744dc..9662da42 100644 --- a/src/pakfire/httpclient.c +++ b/src/pakfire/httpclient.c @@ -63,6 +63,7 @@ struct pakfire_httpclient { // Event Loop sd_event* loop; + int is_running; int still_running; // Timer @@ -477,6 +478,32 @@ ERROR: return r; } +static int pakfire_httpclient_exited(sd_event_source* event, void* data); + +static int pakfire_httpclient_started(sd_event_source* event, void* data) { + struct pakfire_httpclient* self = data; + + DEBUG(self->ctx, "Event loop started\n"); + + // Mark as running + self->is_running = 1; + + // Register an event that get's called once we exit + return sd_event_add_exit(self->loop, NULL, pakfire_httpclient_exited, self); +} + +static int pakfire_httpclient_exited(sd_event_source* event, void* data) { + struct pakfire_httpclient* self = data; + + DEBUG(self->ctx, "Event loop exited\n"); + + // Mark as not running + self->is_running = 0; + + // Register an event so that we catch when the loop is being restarted + return sd_event_add_defer(self->loop, NULL, pakfire_httpclient_started, self); +} + static int pakfire_httpclient_setup_loop(struct pakfire_httpclient* self, sd_event* loop) { int r; @@ -506,6 +533,11 @@ static int pakfire_httpclient_setup_loop(struct pakfire_httpclient* self, sd_eve return r; } + // Have something fire when the loop has started + r = sd_event_add_defer(self->loop, NULL, pakfire_httpclient_started, self); + if (r < 0) + return r; + return 0; } @@ -706,12 +738,11 @@ int pakfire_httpclient_enqueue(struct pakfire_httpclient* self, struct pakfire_x // Keep a reference to the xfer TAILQ_INSERT_TAIL(&self->xfers, e, nodes); - // We are done if we are running in standalone mode - if (pakfire_httpclient_has_flag(self, PAKFIRE_HTTPCLIENT_STANDALONE)) - return 0; + // Launch the transfer straight away if we are running already + if (self->is_running) + return pakfire_httpclient_launch_one(self, e); - // Otherwise we launch the transfer straight away - return pakfire_httpclient_launch_one(self, e); + return 0; } int pakfire_httpclient_dequeue(struct pakfire_httpclient* self, struct pakfire_xfer* xfer) {