From 05248535066bcff365b2aa6c9c3b7fa0e5a4f475 Mon Sep 17 00:00:00 2001 From: batt Date: Wed, 13 Jan 2010 10:04:14 +0000 Subject: [PATCH] Add synchronous timer scheduler. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3132 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/drv/timer.c | 119 ++++++++++++++++++++++++++-------------- bertos/drv/timer.h | 10 ++++ bertos/drv/timer_test.c | 49 +++++++++++++++++ 3 files changed, 136 insertions(+), 42 deletions(-) diff --git a/bertos/drv/timer.c b/bertos/drv/timer.c index 9e2a1173..60494413 100644 --- a/bertos/drv/timer.c +++ b/bertos/drv/timer.c @@ -32,8 +32,8 @@ * * \brief Hardware independent timer driver (implementation) * - * \version $Id$ * \author Bernie Innocenti + * \author Francesco Sacchi */ #include "timer.h" @@ -115,25 +115,16 @@ volatile ticks_t _clock; */ REGISTER static List timers_queue; - /** - * Add the specified timer to the software timer service queue. - * When the delay indicated by the timer expires, the timer - * device will execute the event associated with it. - * - * \note Interrupt safe + * This function really does the job. It adds \a timer to \a queue. + * \see timer_add for details. */ -void timer_add(Timer *timer) +INLINE void timer_addToList(Timer *timer, List *queue) { - Timer *node; - cpu_flags_t flags; - - /* Inserting timers twice causes mayhem. */ ASSERT(timer->magic != TIMER_MAGIC_ACTIVE); DB(timer->magic = TIMER_MAGIC_ACTIVE;) - IRQ_SAVE_DISABLE(flags); /* Calculate expiration time for this timer */ timer->tick = _clock + timer->_delay; @@ -142,7 +133,7 @@ void timer_add(Timer *timer) * Search for the first node whose expiration time is * greater than the timer we want to add. */ - node = (Timer *)LIST_HEAD(&timers_queue); + Timer *node = (Timer *)LIST_HEAD(queue); while (node->link.succ) { /* @@ -158,10 +149,19 @@ void timer_add(Timer *timer) /* Enqueue timer request into the list */ INSERT_BEFORE(&timer->link, &node->link); - - IRQ_RESTORE(flags); } +/** + * Add the specified timer to the software timer service queue. + * When the delay indicated by the timer expires, the timer + * device will execute the event associated with it. + * + * \note Interrupt safe + */ +void timer_add(Timer *timer) +{ + ATOMIC(timer_addToList(timer, &timers_queue)); +} /** * Remove a timer from the timers queue before it has expired. @@ -177,6 +177,64 @@ Timer *timer_abort(Timer *timer) return timer; } + +INLINE void timer_poll(List *queue) +{ + Timer *timer; + + /* + * Check the first timer request in the list and process + * it when it has expired. Repeat this check until the + * first node has not yet expired. Since the list is sorted + * by expiry time, all the following requests are guaranteed + * to expire later. + */ + while ((timer = (Timer *)LIST_HEAD(queue))->link.succ) + { + /* This request in list has not yet expired? */ + if (timer_clock() - timer->tick < 0) + break; + + /* Retreat the expired timer */ + REMOVE(&timer->link); + DB(timer->magic = TIMER_MAGIC_INACTIVE;) + + /* Execute the associated event */ + event_do(&timer->expire); + } +} + +/** + * Add \a timer to \a queue. + * \see synctimer_poll() for details. + */ +void synctimer_add(Timer *timer, List *queue) +{ + timer_addToList(timer, queue); +} + +/** + * Simple synchronous timer based scheduler polling routine. + * + * Sometimes you would like to have a proper scheduler, + * but you can't afford it due to memory constraints. + * + * This is a simple replacement: you can create events and call + * them periodically at specific time intervals. + * All you have to do is to set up normal timers, and call synctimer_add() + * instead of timer_add() to add the events to your specific queue. + * Then, in the main loop or wherever you want, you can call + * synctimer_poll() to process expired events. The associated callbacks will be + * executed. + * As this is done synchronously you don't have to worry about race conditions. + * You can kill an event by simply calling synctimer_abort(). + * + */ +void synctimer_poll(List *queue) +{ + timer_poll(queue); +} + #endif /* CONFIG_TIMER_EVENTS */ @@ -273,10 +331,6 @@ DEFINE_TIMER_ISR #pragma interrupt saveall #endif -#if CONFIG_TIMER_EVENTS - Timer *timer; -#endif - /* * On systems sharing IRQ line and vector, this check is needed * to ensure that IRQ is generated by timer source. @@ -292,28 +346,9 @@ DEFINE_TIMER_ISR /* Update the master ms counter */ ++_clock; -#if CONFIG_TIMER_EVENTS - /* - * Check the first timer request in the list and process - * it when it has expired. Repeat this check until the - * first node has not yet expired. Since the list is sorted - * by expiry time, all the following requests are guaranteed - * to expire later. - */ - while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ) - { - /* This request in list has not yet expired? */ - if (_clock - timer->tick < 0) - break; - - /* Retreat the expired timer */ - REMOVE(&timer->link); - DB(timer->magic = TIMER_MAGIC_INACTIVE;) - - /* Execute the associated event */ - event_do(&timer->expire); - } -#endif /* CONFIG_TIMER_EVENTS */ + #if CONFIG_TIMER_EVENTS + timer_poll(&timers_queue); + #endif TIMER_STROBE_OFF; } diff --git a/bertos/drv/timer.h b/bertos/drv/timer.h index 45b8103e..de268009 100644 --- a/bertos/drv/timer.h +++ b/bertos/drv/timer.h @@ -36,6 +36,7 @@ * \li simple delay: just use timer_delay() if you want to wait for a few milliseconds; * \li delay with callback: create a timer structure and use timer_setDelay() and timer_setSoftint() to set the callback; * \li delay with signal: same as above but use timer_setSignal() to set specify which signal to send. + * \li simple synchronous timer based scheduler: use synctimer_add() to schedule an event in a user provided queue. * * Whenever a timer expires you need to explicitly arm it again with timer_add(). If you want to abort a timer, use timer_abort(). * You can use conversion macros when using msecs to specify the delay. @@ -289,6 +290,15 @@ INLINE void timer_setDelay(Timer *timer, ticks_t delay) timer->_delay = delay; } + +void synctimer_add(Timer *timer, List* q); + +/** \sa timer_abort */ +#define synctimer_abort(t) timer_abort(t) + +void synctimer_poll(List* q); + + #endif /* CONFIG_TIMER_EVENTS */ #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS diff --git a/bertos/drv/timer_test.c b/bertos/drv/timer_test.c index c3414d79..c8ea23b8 100644 --- a/bertos/drv/timer_test.c +++ b/bertos/drv/timer_test.c @@ -97,6 +97,18 @@ static void timer_test_hook(iptr_t _timer) } static Timer test_timers[5]; + +List synctimer_list; +static Timer synctimer_timers[5]; + +static void synctimer_test_hook(iptr_t _timer) +{ + Timer *timer = (Timer *)(void *)_timer; + kprintf("Sync timer process %lu expired\n", (unsigned long)ticks_to_ms(timer->_delay)); + synctimer_add(timer, &synctimer_list); +} + + static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 }; static void timer_test_async(void) @@ -131,11 +143,47 @@ static void timer_test_poll(void) } } +static void synctimer_test(void) +{ + size_t i; + + for (i = 0; i < countof(synctimer_timers); ++i) + { + Timer *timer = &synctimer_timers[i]; + timer_setDelay(timer, ms_to_ticks(test_delays[i])); + timer_setSoftint(timer, synctimer_test_hook, (iptr_t)timer); + synctimer_add(timer, &synctimer_list); + } + + int secs = 0; + mtime_t start_time = ticks_to_ms(timer_clock()); + mtime_t now; + + while (secs <= 10) + { + now = ticks_to_ms(timer_clock()); + synctimer_poll(&synctimer_list); + if (now - start_time >= 1000) + { + ++secs; + start_time += 1000; + kprintf("seconds = %d, ticks=%lu\n", secs, (unsigned long)now); + } + wdt_reset(); + } + + for (i = 0; i < countof(synctimer_timers); ++i) + { + synctimer_abort(&synctimer_timers[i]); + } +} + int timer_testSetup(void) { IRQ_ENABLE; wdt_start(7); timer_init(); + LIST_INIT(&synctimer_list); kdbg_init(); return 0; } @@ -146,6 +194,7 @@ int timer_testRun(void) timer_test_delay(); timer_test_async(); timer_test_poll(); + synctimer_test(); return 0; } -- 2.25.1