From: Michael Tremer Date: Thu, 26 Jun 2025 17:16:14 +0000 (+0000) Subject: ctx: Move the loop running check X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=42c1c36a837d1d1848190c23f52100d27478a241;p=pakfire.git ctx: Move the loop running check Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/ctx.c b/src/pakfire/ctx.c index 71e4d315..6a130018 100644 --- a/src/pakfire/ctx.c +++ b/src/pakfire/ctx.c @@ -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) { diff --git a/src/pakfire/ctx.h b/src/pakfire/ctx.h index a74c9576..ee5d853f 100644 --- a/src/pakfire/ctx.h +++ b/src/pakfire/ctx.h @@ -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