Skip to content

Commit 9a42eb0

Browse files
committed
add all soft timer
1 parent 479fab7 commit 9a42eb0

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

src/Kconfig

+4
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ if RT_USING_TIMER_SOFT
181181
int "The stack size of timer thread"
182182
default 2048 if ARCH_CPU_64BIT
183183
default 512
184+
185+
config RT_USING_TIMER_ALL_SOFT
186+
bool "Set all timer as soft timer"
187+
default n
184188
endif
185189

186190
config RT_USING_CPU_USAGE_TRACER

src/timer.c

+44-40
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
#define DBG_LVL DBG_INFO
3232
#include <rtdbg.h>
3333

34+
#ifndef RT_USING_TIMER_ALL_SOFT
3435
/* hard timer list */
3536
static rt_list_t _timer_list[RT_TIMER_SKIP_LIST_LEVEL];
3637
static struct rt_spinlock _htimer_lock;
38+
#endif
3739

3840
#ifdef RT_USING_TIMER_SOFT
3941

@@ -93,6 +95,9 @@ void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
9395

9496
rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
9597
{
98+
#ifdef RT_USING_TIMER_ALL_SOFT
99+
return &_stimer_lock;
100+
#else
96101
#ifdef RT_USING_TIMER_SOFT
97102
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
98103
{
@@ -103,6 +108,7 @@ rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
103108
{
104109
return &_htimer_lock;
105110
}
111+
#endif
106112
}
107113

108114
/**
@@ -130,6 +136,10 @@ static void _timer_init(rt_timer_t timer,
130136
{
131137
int i;
132138

139+
#ifdef RT_USING_TIMER_ALL_SOFT
140+
flag |= RT_TIMER_FLAG_SOFT_TIMER;
141+
#endif
142+
133143
/* set flag */
134144
timer->parent.flag = flag;
135145

@@ -570,6 +580,10 @@ rt_err_t rt_timer_start(rt_timer_t timer)
570580
RT_ASSERT(timer != RT_NULL);
571581
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
572582

583+
#ifdef RT_USING_TIMER_ALL_SOFT
584+
timer_list = _soft_timer_list;
585+
spinlock = &_stimer_lock;
586+
#else
573587
#ifdef RT_USING_TIMER_SOFT
574588
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
575589
{
@@ -582,6 +596,7 @@ rt_err_t rt_timer_start(rt_timer_t timer)
582596
timer_list = _timer_list;
583597
spinlock = &_htimer_lock;
584598
}
599+
#endif
585600

586601
if (timer->parent.flag & RT_TIMER_FLAG_THREAD_TIMER)
587602
{
@@ -598,13 +613,6 @@ rt_err_t rt_timer_start(rt_timer_t timer)
598613

599614
err = _timer_start(timer_list, timer);
600615

601-
#ifdef RT_USING_TIMER_SOFT
602-
if (err == RT_EOK && (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER))
603-
{
604-
rt_sem_release(&_soft_timer_sem);
605-
}
606-
#endif /* RT_USING_TIMER_SOFT */
607-
608616
rt_spin_unlock_irqrestore(spinlock, level);
609617

610618
if (is_thread_timer)
@@ -756,7 +764,20 @@ void rt_timer_check(void)
756764
return;
757765
}
758766
#endif
767+
768+
#ifdef RT_USING_TIMER_SOFT
769+
rt_err_t ret = RT_ERROR;
770+
rt_tick_t next_timeout;
771+
772+
ret = _timer_list_next_timeout(_soft_timer_list, &next_timeout);
773+
if ((ret == RT_EOK) && (next_timeout <= rt_tick_get()))
774+
{
775+
rt_sem_release(&_soft_timer_sem);
776+
}
777+
#endif
778+
#ifndef RT_USING_TIMER_ALL_SOFT
759779
_timer_check(_timer_list, &_htimer_lock);
780+
#endif
760781
}
761782

762783
/**
@@ -767,13 +788,21 @@ void rt_timer_check(void)
767788
rt_tick_t rt_timer_next_timeout_tick(void)
768789
{
769790
rt_base_t level;
770-
rt_tick_t next_timeout = RT_TICK_MAX;
791+
rt_tick_t htimer_next_timeout = RT_TICK_MAX, stimer_next_timeout = RT_TICK_MAX;
771792

793+
#ifndef RT_USING_TIMER_ALL_SOFT
772794
level = rt_spin_lock_irqsave(&_htimer_lock);
773-
_timer_list_next_timeout(_timer_list, &next_timeout);
795+
_timer_list_next_timeout(_timer_list, &htimer_next_timeout);
774796
rt_spin_unlock_irqrestore(&_htimer_lock, level);
797+
#endif
775798

776-
return next_timeout;
799+
#ifdef RT_USING_TIMER_SOFT
800+
level = rt_spin_lock_irqsave(&_stimer_lock);
801+
_timer_list_next_timeout(_soft_timer_list, &stimer_next_timeout);
802+
rt_spin_unlock_irqrestore(&_stimer_lock, level);
803+
#endif
804+
805+
return htimer_next_timeout < stimer_next_timeout ? htimer_next_timeout : stimer_next_timeout;
777806
}
778807

779808
#ifdef RT_USING_TIMER_SOFT
@@ -784,41 +813,12 @@ rt_tick_t rt_timer_next_timeout_tick(void)
784813
*/
785814
static void _timer_thread_entry(void *parameter)
786815
{
787-
rt_err_t ret = RT_ERROR;
788-
rt_tick_t next_timeout;
789-
rt_base_t level;
790-
791816
RT_UNUSED(parameter);
792817

793-
rt_sem_control(&_soft_timer_sem, RT_IPC_CMD_SET_VLIMIT, (void*)1);
794-
795818
while (1)
796819
{
797-
/* get the next timeout tick */
798-
level = rt_spin_lock_irqsave(&_stimer_lock);
799-
ret = _timer_list_next_timeout(_soft_timer_list, &next_timeout);
800-
rt_spin_unlock_irqrestore(&_stimer_lock, level);
801-
802-
if (ret != RT_EOK)
803-
{
804-
rt_sem_take(&_soft_timer_sem, RT_WAITING_FOREVER);
805-
}
806-
else
807-
{
808-
rt_tick_t current_tick;
809-
810-
/* get current tick */
811-
current_tick = rt_tick_get();
812-
813-
if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
814-
{
815-
/* get the delta timeout tick */
816-
next_timeout = next_timeout - current_tick;
817-
rt_sem_take(&_soft_timer_sem, next_timeout);
818-
}
819-
}
820-
821820
_timer_check(_soft_timer_list, &_stimer_lock); /* check software timer */
821+
rt_sem_take(&_soft_timer_sem, RT_WAITING_FOREVER);
822822
}
823823
}
824824
#endif /* RT_USING_TIMER_SOFT */
@@ -830,13 +830,16 @@ static void _timer_thread_entry(void *parameter)
830830
*/
831831
void rt_system_timer_init(void)
832832
{
833+
#ifndef RT_USING_TIMER_ALL_SOFT
833834
rt_size_t i;
834835

835836
for (i = 0; i < sizeof(_timer_list) / sizeof(_timer_list[0]); i++)
836837
{
837838
rt_list_init(_timer_list + i);
838839
}
840+
839841
rt_spin_lock_init(&_htimer_lock);
842+
#endif
840843
}
841844

842845
/**
@@ -857,6 +860,7 @@ void rt_system_timer_thread_init(void)
857860
}
858861
rt_spin_lock_init(&_stimer_lock);
859862
rt_sem_init(&_soft_timer_sem, "stimer", 0, RT_IPC_FLAG_PRIO);
863+
rt_sem_control(&_soft_timer_sem, RT_IPC_CMD_SET_VLIMIT, (void*)1);
860864
/* start software timer thread */
861865
rt_thread_init(&_timer_thread,
862866
"timer",

0 commit comments

Comments
 (0)