Skip to content

Commit 3acfb65

Browse files
committed
[timer] The rt_timer_check and _soft_timer_check functions are merged
1 parent 7af592a commit 3acfb65

File tree

1 file changed

+84
-141
lines changed

1 file changed

+84
-141
lines changed

src/timer.c

+84-141
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,84 @@ static rt_err_t _timer_start(rt_list_t *timer_list, rt_timer_t timer)
476476
return RT_EOK;
477477
}
478478

479+
/**
480+
* @brief This function will check timer list, if a timeout event happens,
481+
* the corresponding timeout function will be invoked.
482+
*
483+
* @param timer_list The timer list to check.
484+
* @param lock The lock for the timer list.
485+
*/
486+
static void _timer_check(rt_list_t *timer_list, struct rt_spinlock *lock)
487+
{
488+
struct rt_timer *t;
489+
rt_tick_t current_tick;
490+
rt_base_t level;
491+
rt_list_t list;
492+
493+
level = rt_spin_lock_irqsave(lock);
494+
495+
current_tick = rt_tick_get();
496+
497+
rt_list_init(&list);
498+
499+
while (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
500+
{
501+
t = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
502+
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
503+
504+
/* re-get tick */
505+
current_tick = rt_tick_get();
506+
507+
/*
508+
* It supposes that the new tick shall less than the half duration of
509+
* tick max.
510+
*/
511+
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
512+
{
513+
RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
514+
515+
/* remove timer from timer list firstly */
516+
_timer_remove(t);
517+
if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
518+
{
519+
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
520+
}
521+
522+
t->parent.flag |= RT_TIMER_FLAG_PROCESSING;
523+
/* add timer to temporary list */
524+
rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
525+
526+
rt_spin_unlock_irqrestore(lock, level);
527+
528+
/* call timeout function */
529+
t->timeout_func(t->parameter);
530+
531+
RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
532+
LOG_D("current tick: %d", current_tick);
533+
534+
level = rt_spin_lock_irqsave(lock);
535+
536+
t->parent.flag &= ~RT_TIMER_FLAG_PROCESSING;
537+
538+
/* Check whether the timer object is detached or started again */
539+
if (rt_list_isempty(&list))
540+
{
541+
continue;
542+
}
543+
rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
544+
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
545+
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
546+
{
547+
/* start it */
548+
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
549+
_timer_start(timer_list, t);
550+
}
551+
}
552+
else break;
553+
}
554+
rt_spin_unlock_irqrestore(lock, level);
555+
}
556+
479557
/**
480558
* @brief This function will start the timer
481559
*
@@ -673,83 +751,19 @@ RTM_EXPORT(rt_timer_control);
673751
*/
674752
void rt_timer_check(void)
675753
{
676-
struct rt_timer *t;
677-
rt_tick_t current_tick;
678-
rt_base_t level;
679-
rt_list_t list;
680-
681754
RT_ASSERT(rt_interrupt_get_nest() > 0);
682755

683756
LOG_D("timer check enter");
684757

685-
level = rt_spin_lock_irqsave(&_htimer_lock);
686-
687-
current_tick = rt_tick_get();
688-
689758
#ifdef RT_USING_SMP
690759
/* Running on core 0 only */
691760
if (rt_hw_cpu_id() != 0)
692761
{
693-
rt_spin_unlock_irqrestore(&_htimer_lock, level);
694762
return;
695763
}
696764
#endif
765+
_timer_check(_timer_list, &_htimer_lock);
697766

698-
rt_list_init(&list);
699-
700-
while (!rt_list_isempty(&_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
701-
{
702-
t = rt_list_entry(_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
703-
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
704-
705-
/*
706-
* It supposes that the new tick shall less than the half duration of
707-
* tick max.
708-
*/
709-
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
710-
{
711-
RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
712-
713-
/* remove timer from timer list firstly */
714-
_timer_remove(t);
715-
if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
716-
{
717-
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
718-
}
719-
720-
t->parent.flag |= RT_TIMER_FLAG_PROCESSING;
721-
/* add timer to temporary list */
722-
rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
723-
rt_spin_unlock_irqrestore(&_htimer_lock, level);
724-
/* call timeout function */
725-
t->timeout_func(t->parameter);
726-
727-
/* re-get tick */
728-
current_tick = rt_tick_get();
729-
730-
RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
731-
LOG_D("current tick: %d", current_tick);
732-
level = rt_spin_lock_irqsave(&_htimer_lock);
733-
734-
t->parent.flag &= ~RT_TIMER_FLAG_PROCESSING;
735-
736-
/* Check whether the timer object is detached or started again */
737-
if (rt_list_isempty(&list))
738-
{
739-
continue;
740-
}
741-
rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
742-
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
743-
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
744-
{
745-
/* start it */
746-
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
747-
_timer_start(_timer_list, t);
748-
}
749-
}
750-
else break;
751-
}
752-
rt_spin_unlock_irqrestore(&_htimer_lock, level);
753767
LOG_D("timer check leave");
754768
}
755769

@@ -771,81 +785,6 @@ rt_tick_t rt_timer_next_timeout_tick(void)
771785
}
772786

773787
#ifdef RT_USING_TIMER_SOFT
774-
/**
775-
* @brief This function will check software-timer list, if a timeout event happens, the
776-
* corresponding timeout function will be invoked.
777-
*/
778-
static void _soft_timer_check(void)
779-
{
780-
rt_tick_t current_tick;
781-
struct rt_timer *t;
782-
rt_base_t level;
783-
rt_list_t list;
784-
785-
rt_list_init(&list);
786-
LOG_D("software timer check enter");
787-
level = rt_spin_lock_irqsave(&_stimer_lock);
788-
789-
while (!rt_list_isempty(&_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
790-
{
791-
t = rt_list_entry(_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
792-
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
793-
794-
current_tick = rt_tick_get();
795-
796-
/*
797-
* It supposes that the new tick shall less than the half duration of
798-
* tick max.
799-
*/
800-
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
801-
{
802-
RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
803-
804-
/* remove timer from timer list firstly */
805-
_timer_remove(t);
806-
if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
807-
{
808-
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
809-
}
810-
811-
t->parent.flag |= RT_TIMER_FLAG_PROCESSING;
812-
/* add timer to temporary list */
813-
rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
814-
815-
rt_spin_unlock_irqrestore(&_stimer_lock, level);
816-
817-
/* call timeout function */
818-
t->timeout_func(t->parameter);
819-
820-
RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
821-
LOG_D("current tick: %d", current_tick);
822-
823-
level = rt_spin_lock_irqsave(&_stimer_lock);
824-
825-
t->parent.flag &= ~RT_TIMER_FLAG_PROCESSING;
826-
827-
/* Check whether the timer object is detached or started again */
828-
if (rt_list_isempty(&list))
829-
{
830-
continue;
831-
}
832-
rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
833-
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
834-
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
835-
{
836-
/* start it */
837-
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
838-
_timer_start(_soft_timer_list, t);
839-
}
840-
}
841-
else break; /* not check anymore */
842-
}
843-
844-
rt_spin_unlock_irqrestore(&_stimer_lock, level);
845-
846-
LOG_D("software timer check leave");
847-
}
848-
849788
/**
850789
* @brief System timer thread entry
851790
*
@@ -888,7 +827,11 @@ static void _timer_thread_entry(void *parameter)
888827
}
889828

890829
/* check software timer */
891-
_soft_timer_check();
830+
LOG_D("software timer check enter");
831+
832+
_timer_check(_soft_timer_list, &_stimer_lock);
833+
834+
LOG_D("software timer check leave");
892835
}
893836
}
894837
#endif /* RT_USING_TIMER_SOFT */

0 commit comments

Comments
 (0)