aboutsummaryrefslogtreecommitdiffstats
path: root/src/generic/timer_irq.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-08-09 18:51:55 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-08-09 21:08:40 -0400
commit1051a52755f3f9b8f8b9d21a430ddf3b9982e5a0 (patch)
treeb4271c4e138a6f04f2c6ff8740bf27bbde6ef6c8 /src/generic/timer_irq.c
parent5583b050a0877c037fc05ed0de522c33123e6191 (diff)
downloadkutter-1051a52755f3f9b8f8b9d21a430ddf3b9982e5a0.tar.gz
kutter-1051a52755f3f9b8f8b9d21a430ddf3b9982e5a0.tar.xz
kutter-1051a52755f3f9b8f8b9d21a430ddf3b9982e5a0.zip
timer_irq: Rework timer irq handler to check for tasks pending
Allow timer_dispatch_many() to run for extended periods if there are no tasks pending. This reduces the amount of lost cpu time spent entering and exiting the irq handler. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/generic/timer_irq.c')
-rw-r--r--src/generic/timer_irq.c38
1 files changed, 14 insertions, 24 deletions
diff --git a/src/generic/timer_irq.c b/src/generic/timer_irq.c
index 03881b2e..6527c949 100644
--- a/src/generic/timer_irq.c
+++ b/src/generic/timer_irq.c
@@ -36,17 +36,6 @@ static uint32_t timer_repeat_until;
#define TIMER_MIN_TRY_TICKS timer_from_us(1)
#define TIMER_DEFER_REPEAT_TICKS timer_from_us(5)
-// Reschedule timers after a brief pause to prevent task starvation
-static uint32_t noinline
-force_defer(uint32_t next)
-{
- uint32_t now = timer_read_time();
- if (timer_is_before(next + timer_from_us(1000), now))
- shutdown("Rescheduled timer in the past");
- timer_repeat_until = now + TIMER_REPEAT_TICKS;
- return now + TIMER_DEFER_REPEAT_TICKS;
-}
-
// Invoke timers - called from board irq code.
uint32_t
timer_dispatch_many(void)
@@ -62,9 +51,16 @@ timer_dispatch_many(void)
// Schedule next timer normally.
return next;
- if (unlikely(timer_is_before(tru, now)))
- // Too many repeat timers from a single interrupt - force a pause
- return force_defer(next);
+ if (unlikely(timer_is_before(tru, now))) {
+ // Check if there are too many repeat timers
+ if (diff < (int32_t)(-timer_from_us(1000)))
+ shutdown("Rescheduled timer in the past");
+ if (sched_tasks_busy()) {
+ timer_repeat_until = now + TIMER_REPEAT_TICKS;
+ return now + TIMER_DEFER_REPEAT_TICKS;
+ }
+ timer_repeat_until = tru = now + TIMER_IDLE_REPEAT_TICKS;
+ }
// Next timer in the past or near future - wait for it to be ready
irq_enable();
@@ -74,20 +70,14 @@ timer_dispatch_many(void)
}
}
-// Periodic background task that temporarily boosts priority of
-// timers. This helps prioritize timers when tasks are idling.
+// Make sure timer_repeat_until doesn't wrap 32bit comparisons
void
timer_task(void)
{
+ uint32_t now = timer_read_time();
irq_disable();
- timer_repeat_until = timer_read_time() + TIMER_IDLE_REPEAT_TICKS;
+ if (timer_is_before(timer_repeat_until, now))
+ timer_repeat_until = now;
irq_enable();
}
DECL_TASK(timer_task);
-
-void
-timer_irq_shutdown(void)
-{
- timer_repeat_until = timer_read_time() + TIMER_IDLE_REPEAT_TICKS;
-}
-DECL_SHUTDOWN(timer_irq_shutdown);