Skip to content

Commit 7879986

Browse files
authored
[timer] The rt_timer_check and _soft_timer_check functions are merged (#8884)
1 parent 48bd0e4 commit 7879986

File tree

1 file changed

+86
-142
lines changed

1 file changed

+86
-142
lines changed

src/timer.c

+86-142
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2022, RT-Thread Development Team
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -21,6 +21,7 @@
2121
* 2022-04-19 Stanley Correct descriptions
2222
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
2323
* 2024-01-25 Shell add RT_TIMER_FLAG_THREAD_TIMER for timer to sync with sched
24+
* 2024-05-01 wdfk-prog The rt_timer_check and _soft_timer_check functions are merged
2425
*/
2526

2627
#include <rtthread.h>
@@ -472,6 +473,84 @@ static rt_err_t _timer_start(rt_list_t *timer_list, rt_timer_t timer)
472473
return RT_EOK;
473474
}
474475

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

679753
LOG_D("timer check enter");
680754

681-
level = rt_spin_lock_irqsave(&_htimer_lock);
682-
683-
current_tick = rt_tick_get();
684-
685755
#ifdef RT_USING_SMP
686756
/* Running on core 0 only */
687757
if (rt_cpu_get_id() != 0)
688758
{
689-
rt_spin_unlock_irqrestore(&_htimer_lock, level);
690759
return;
691760
}
692761
#endif
762+
_timer_check(_timer_list, &_htimer_lock);
693763

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

@@ -767,81 +782,6 @@ rt_tick_t rt_timer_next_timeout_tick(void)
767782
}
768783

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

886826
/* check software timer */
887-
_soft_timer_check();
827+
LOG_D("software timer check enter");
828+
829+
_timer_check(_soft_timer_list, &_stimer_lock);
830+
831+
LOG_D("software timer check leave");
888832
}
889833
}
890834
#endif /* RT_USING_TIMER_SOFT */

0 commit comments

Comments
 (0)