]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Move the loop running check
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 17:16:14 +0000 (17:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 17:16:14 +0000 (17:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/ctx.c
src/pakfire/ctx.h

index 71e4d315a51d3798e2e46e63d453e824ff585bd7..6a130018c44033e632fb08e71f9b320c8b51c519 100644 (file)
@@ -54,6 +54,13 @@ struct pakfire_ctx {
 
        // Event Loop
        sd_event* loop;
+       int loop_is_running;
+
+       // Events
+       struct pakfire_ctx_events {
+               sd_event_source* loop_started;
+               sd_event_source* loop_exited;
+       } events;
 
        // Logging
        struct pakfire_ctx_log {
@@ -176,6 +183,12 @@ static int pakfire_ctx_load_config(struct pakfire_ctx* ctx, const char* path) {
 }
 
 static void pakfire_ctx_free(struct pakfire_ctx* ctx) {
+       // Release the events
+       if (ctx->events.loop_started)
+               sd_event_source_unref(ctx->events.loop_started);
+       if (ctx->events.loop_exited)
+               sd_event_source_unref(ctx->events.loop_exited);
+
        // Release event loop
        if (ctx->loop)
                sd_event_unref(ctx->loop);
@@ -376,6 +389,42 @@ int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire,
 
 // Event Loop
 
+static int pakfire_ctx_loop_exited(sd_event_source* event, void* data);
+
+static int pakfire_ctx_loop_started(sd_event_source* event, void* data) {
+       struct pakfire_ctx* self = data;
+
+       // Log action
+       DEBUG(self, "Event loop started\n");
+
+       // The loop is now running
+       self->loop_is_running = 1;
+
+       if (self->events.loop_exited)
+               sd_event_source_unref(self->events.loop_exited);
+
+       // Register an event that get's called once we exit
+       return sd_event_add_exit(self->loop,
+                       &self->events.loop_exited, pakfire_ctx_loop_exited, self);
+}
+
+static int pakfire_ctx_loop_exited(sd_event_source* event, void* data) {
+       struct pakfire_ctx* self = data;
+
+       // Log action
+       DEBUG(self, "Event loop exited\n");
+
+       // The loop is no longer running
+       self->loop_is_running = 0;
+
+       if (self->events.loop_started)
+               sd_event_source_unref(self->events.loop_started);
+
+       // Register an event that get's called once we start again
+       return sd_event_add_defer(self->loop,
+                       &self->events.loop_started, pakfire_ctx_loop_started, self);
+}
+
 int pakfire_ctx_loop(struct pakfire_ctx* ctx, sd_event** loop) {
        int r;
 
@@ -384,6 +433,12 @@ int pakfire_ctx_loop(struct pakfire_ctx* ctx, sd_event** loop) {
                r = sd_event_new(&ctx->loop);
                if (r < 0)
                        return r;
+
+               // Register an event that gets called when the loop started
+               r = sd_event_add_defer(ctx->loop,
+                               &ctx->events.loop_started, pakfire_ctx_loop_started, ctx);
+               if (r < 0)
+                       return r;
        }
 
        // Return the loop
@@ -392,6 +447,10 @@ int pakfire_ctx_loop(struct pakfire_ctx* ctx, sd_event** loop) {
        return 0;
 }
 
+int pakfire_ctx_loop_is_running(struct pakfire_ctx* self) {
+       return self->loop_is_running;
+}
+
 // cURL
 
 CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* ctx) {
index a74c9576c514b24a37ec456567bf288eff60cc8d..ee5d853fc407cc0654f6c93e2f3d1b8b480347f1 100644 (file)
@@ -114,6 +114,8 @@ int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire,
 
 int pakfire_ctx_loop(struct pakfire_ctx* ctx, sd_event** loop);
 
+int pakfire_ctx_loop_is_running(struct pakfire_ctx* self);
+
 // cURL
 
 #include <curl/curl.h>