From cf198dc03e51439da9e824abb7acc29c30e691ae Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Mon, 6 Feb 2012 22:35:30 +1100 Subject: [PATCH 3/3] LOOP: prevent jobs from consuming too much cpu. Signed-off-by: Angus Salkeld --- lib/loop.c | 23 ++++++++++++++++++++--- 1 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/loop.c b/lib/loop.c index 45ceb3f..3e10b89 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -118,6 +118,8 @@ qb_loop_run(struct qb_loop *l) int32_t p; int32_t p_stop = QB_LOOP_LOW; int32_t todo = 0; + int32_t job_todo; + int32_t timer_todo; int32_t ms_timeout; l->stop_requested = QB_FALSE; @@ -130,13 +132,28 @@ qb_loop_run(struct qb_loop *l) } if (l->job_source && l->job_source->poll) { - todo += l->job_source->poll(l->job_source, 0); + job_todo = l->job_source->poll(l->job_source, 0); + } else { + job_todo = 0; } if (l->timer_source && l->timer_source->poll) { - todo += l->timer_source->poll(l->timer_source, 0); + timer_todo = l->timer_source->poll(l->timer_source, 0); + } else { + timer_todo = 0; } - if (todo > 0) { + if (todo > 0 || timer_todo > 0) { + /* + * if there are old todos or timer todos then don't wait. + */ ms_timeout = 0; + } else if (job_todo > 0) { + todo = 0; + /* + * if we only have jobs to do (not timers or old todos) + * then set a non-zero timeout. Jobs can spin out of + * control if someone keeps adding them. + */ + ms_timeout = 50; } else { if (l->timer_source) { ms_timeout = qb_loop_timer_msec_duration_to_expire(l->timer_source); -- 1.7.7.6