From 53b717c454e9d853fb924c484ff20fb7ec602e4a Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Tue, 23 Sep 2014 23:42:35 +0200 Subject: Avoid spinning in a loop This also helps reduce a lot of noice in strace. --- ponymap.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ponymap.c b/ponymap.c index 306830c..19c6121 100644 --- a/ponymap.c +++ b/ponymap.c @@ -312,6 +312,7 @@ struct generator }; static bool generator_step (struct app_context *ctx); +static void on_generator_step_requested (struct app_context *ctx); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -345,6 +346,7 @@ struct app_context struct target *running_tail; ///< The tail link of `running_targets' struct poller poller; ///< Manages polled descriptors + struct poller_idle step_event; ///< Idle event to make more units bool quitting; ///< User requested quitting bool polling; ///< The event loop is running }; @@ -369,6 +371,10 @@ app_context_init (struct app_context *self) poller_init (&self->poller); self->quitting = false; self->polling = false; + + poller_idle_init (&self->step_event, &self->poller); + self->step_event.dispatcher = (poller_idle_fn) on_generator_step_requested; + self->step_event.user_data = self; } static void @@ -538,9 +544,9 @@ unit_abort (struct unit *u) // We're no longer running LIST_UNLINK (u->target->running_units, u); - // We might have made it possible to launch new units - while (generator_step (u->target->ctx)) - ; + // We might have made it possible to launch new units; we cannot run + // the generator right now, though, as we could spin in a long loop + poller_idle_set (&u->target->ctx->step_event); if (u->success) { @@ -1607,6 +1613,14 @@ generator_step (struct app_context *ctx) return false; } +static void +on_generator_step_requested (struct app_context *ctx) +{ + poller_idle_reset (&ctx->step_event); + while (generator_step (ctx)) + ; +} + // --- Signals ----------------------------------------------------------------- static int g_signal_pipe[2]; ///< A pipe used to signal... signals -- cgit v1.2.3-70-g09d2