#include <cpu/irq.h>
#include <cpu/power.h> // cpu_relax()
+#include <kern/proc_p.h> // proc_decQuantun()
+
/*
* Include platform-specific binding code if we're hosted.
* Try the CPU specific one for bare-metal environments.
ASSERT(timer->magic != TIMER_MAGIC_ACTIVE);
DB(timer->magic = TIMER_MAGIC_ACTIVE;)
-
- /* Calculate expiration time for this timer */
- timer->tick = _clock + timer->_delay;
-
/*
* Search for the first node whose expiration time is
* greater than the timer we want to add.
*/
void timer_add(Timer *timer)
{
- ATOMIC(timer_addToList(timer, &timers_queue));
+ ATOMIC(
+ /* Calculate expiration time for this timer */
+ timer->tick = _clock + timer->_delay;
+
+ timer_addToList(timer, &timers_queue);
+ );
}
/**
*/
void synctimer_add(Timer *timer, List *queue)
{
+ timer->tick = timer_clock() + timer->_delay;
+
+ timer_addToList(timer, queue);
+}
+
+void synctimer_readd(Timer *timer, List *queue)
+{
+ timer->tick += timer->_delay;
timer_addToList(timer, queue);
}
+
/**
* Simple synchronous timer based scheduler polling routine.
*
/**
* Wait for the specified amount of timer ticks.
+ *
+ * \note Sleeping while preemption is disabled fallbacks to a busy wait sleep.
*/
void timer_delayTicks(ticks_t delay)
{
/* We shouldn't sleep with interrupts disabled */
IRQ_ASSERT_ENABLED();
-#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
+#if CONFIG_KERN_SIGNALS
Timer t;
-
- ASSERT(!sig_check(SIG_SINGLE));
- timer_setSignal(&t, proc_current(), SIG_SINGLE);
- timer_setDelay(&t, delay);
- timer_add(&t);
- sig_wait(SIG_SINGLE);
-
-#else /* !CONFIG_KERN_SIGNALS */
-
- ticks_t start = timer_clock();
-
- /* Busy wait */
- while (timer_clock() - start < delay)
- cpu_relax();
-
+ DB(t.magic = TIMER_MAGIC_INACTIVE;)
+ if (proc_preemptAllowed())
+ {
+ ASSERT(!sig_check(SIG_SINGLE));
+ timer_setSignal(&t, proc_current(), SIG_SINGLE);
+ timer_setDelay(&t, delay);
+ timer_add(&t);
+ sig_wait(SIG_SINGLE);
+ }
+ else
#endif /* !CONFIG_KERN_SIGNALS */
+ {
+ ticks_t start = timer_clock();
+
+ /* Busy wait */
+ while (timer_clock() - start < delay)
+ cpu_relax();
+ }
}
hptime_t now, prev = timer_hw_hpread();
hptime_t delta;
- for(;;)
+ for (;;)
{
now = timer_hw_hpread();
/*
- * We rely on hptime_t being unsigned here to
- * reduce the modulo to an AND in the common
- * case of TIMER_HW_CNT.
+ * The timer counter may wrap here and "prev" can become
+ * greater than "now". So, be sure to always evaluate a
+ * coherent timer difference:
+ *
+ * 0 prev now TIMER_HW_CNT
+ * |_____|_______________|_____|
+ * ^^^^^^^^^^^^^^^
+ * delta = now - prev
+ *
+ * 0 now prev TIMER_HW_CNT
+ * |_____|_______________|_____|
+ * ^^^^^ ^^^^^
+ * delta = (TIMER_HW_CNT - prev) + now
+ *
+ * NOTE: TIMER_HW_CNT can be any value, not necessarily a power
+ * of 2. For this reason the "%" operator is not suitable for
+ * the generic case.
*/
- delta = (now - prev) % TIMER_HW_CNT;
+ delta = (now < prev) ? ((hptime_t)TIMER_HW_CNT - prev + now) :
+ (now - prev);
if (delta >= delay)
break;
delay -= delta;
}
#endif /* CONFIG_TIMER_UDELAY */
-
/**
* Timer interrupt handler. Find soft timers expired and
* trigger corresponding events.
TIMER_STROBE_ON;
- /* Perform hw IRQ handling */
- timer_hw_irq();
-
/* Update the master ms counter */
++_clock;
+ /* Update the current task's quantum (if enabled). */
+ proc_decQuantum();
+
#if CONFIG_TIMER_EVENTS
timer_poll(&timers_queue);
#endif
+ /* Perform hw IRQ handling */
+ timer_hw_irq();
+
TIMER_STROBE_OFF;
}
}
-#if (ARCH & ARCH_EMUL)
+#if (ARCH & ARCH_EMUL) || (CPU_ARM_AT91)
/**
- * Stop timer (only used by emulator)
+ * Stop timer
*/
void timer_cleanup(void)
{
MOD_CLEANUP(timer);
timer_hw_cleanup();
-
- // Hmmm... apparently, the demo app does not cleanup properly
- //ASSERT(LIST_EMPTY(&timers_queue));
}
-#endif /* ARCH_EMUL */
+#endif