mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-02-06 09:02:20 +08:00
[timer]add all soft timer config (#9048)
* add all soft timer * update wq * add timer test * shield LOG_D
This commit is contained in:
@@ -94,7 +94,7 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue,
|
||||
struct rt_work *work, rt_tick_t ticks)
|
||||
{
|
||||
rt_base_t level;
|
||||
rt_err_t err;
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
level = rt_spin_lock_irqsave(&(queue->spinlock));
|
||||
|
||||
@@ -113,13 +113,7 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue,
|
||||
{
|
||||
/* resume work thread, and do a re-schedule if succeed */
|
||||
rt_thread_resume(queue->work_thread);
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
else if (ticks < RT_TICK_MAX / 2)
|
||||
{
|
||||
@@ -139,12 +133,14 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue,
|
||||
rt_list_insert_after(queue->delayed_list.prev, &(work->list));
|
||||
|
||||
err = rt_timer_start(&(work->timer));
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
|
||||
return err;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = - RT_ERROR;
|
||||
}
|
||||
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
return -RT_ERROR;
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
|
||||
@@ -160,14 +156,14 @@ static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_wor
|
||||
{
|
||||
if ((err = rt_timer_stop(&(work->timer))) != RT_EOK)
|
||||
{
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
return err;
|
||||
goto exit;
|
||||
}
|
||||
rt_timer_detach(&(work->timer));
|
||||
work->flags &= ~RT_WORK_STATE_SUBMITTING;
|
||||
}
|
||||
err = queue->work_current != work ? RT_EOK : -RT_EBUSY;
|
||||
work->workqueue = RT_NULL;
|
||||
exit:
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
return err;
|
||||
}
|
||||
@@ -200,12 +196,9 @@ static void _delayed_work_timeout_handler(void *parameter)
|
||||
{
|
||||
/* resume work thread, and do a re-schedule if succeed */
|
||||
rt_thread_resume(queue->work_thread);
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,13 +351,9 @@ rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *wo
|
||||
{
|
||||
/* resume work thread, and do a re-schedule if succeed */
|
||||
rt_thread_resume(queue->work_thread);
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
}
|
||||
|
||||
rt_spin_unlock_irqrestore(&(queue->spinlock), level);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -579,14 +579,13 @@ struct rt_object_information
|
||||
*/
|
||||
#define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
|
||||
#define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
|
||||
#define RT_TIMER_FLAG_PROCESSING 0x2 /**< timer's timeout fuction is processing */
|
||||
#define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
|
||||
#define RT_TIMER_FLAG_PERIODIC 0x4 /**< periodic timer */
|
||||
#define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
|
||||
|
||||
#define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
|
||||
#define RT_TIMER_FLAG_SOFT_TIMER 0x8 /**< soft timer,the timer's callback function will be called in timer thread. */
|
||||
#define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
|
||||
#define RT_TIMER_FLAG_THREAD_TIMER \
|
||||
(0x10 | RT_TIMER_FLAG_HARD_TIMER) /**< thread timer that cooperates with scheduler directly */
|
||||
(0x8 | RT_TIMER_FLAG_HARD_TIMER) /**< thread timer that cooperates with scheduler directly */
|
||||
|
||||
#define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
|
||||
#define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
|
||||
|
||||
@@ -176,6 +176,10 @@ if RT_USING_TIMER_SOFT
|
||||
int "The stack size of timer thread"
|
||||
default 2048 if ARCH_CPU_64BIT
|
||||
default 512
|
||||
|
||||
config RT_USING_TIMER_ALL_SOFT
|
||||
bool "Set all timer as soft timer"
|
||||
default n
|
||||
endif
|
||||
|
||||
config RT_USING_CPU_USAGE_TRACER
|
||||
|
||||
92
src/timer.c
92
src/timer.c
@@ -31,9 +31,11 @@
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
#ifndef RT_USING_TIMER_ALL_SOFT
|
||||
/* hard timer list */
|
||||
static rt_list_t _timer_list[RT_TIMER_SKIP_LIST_LEVEL];
|
||||
static struct rt_spinlock _htimer_lock;
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
|
||||
@@ -93,6 +95,9 @@ void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
|
||||
|
||||
rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
|
||||
{
|
||||
#ifdef RT_USING_TIMER_ALL_SOFT
|
||||
return &_stimer_lock;
|
||||
#else
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
||||
{
|
||||
@@ -103,6 +108,7 @@ rt_inline struct rt_spinlock* _timerlock_idx(struct rt_timer *timer)
|
||||
{
|
||||
return &_htimer_lock;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,6 +136,10 @@ static void _timer_init(rt_timer_t timer,
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifdef RT_USING_TIMER_ALL_SOFT
|
||||
flag |= RT_TIMER_FLAG_SOFT_TIMER;
|
||||
#endif
|
||||
|
||||
/* set flag */
|
||||
timer->parent.flag = flag;
|
||||
|
||||
@@ -403,11 +413,6 @@ static rt_err_t _timer_start(rt_list_t *timer_list, rt_timer_t timer)
|
||||
unsigned int tst_nr;
|
||||
static unsigned int random_nr;
|
||||
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_PROCESSING)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* remove timer from list */
|
||||
_timer_remove(timer);
|
||||
/* change status of timer */
|
||||
@@ -516,7 +521,6 @@ static void _timer_check(rt_list_t *timer_list, struct rt_spinlock *lock)
|
||||
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
||||
}
|
||||
|
||||
t->parent.flag |= RT_TIMER_FLAG_PROCESSING;
|
||||
/* add timer to temporary list */
|
||||
rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
|
||||
|
||||
@@ -529,8 +533,6 @@ static void _timer_check(rt_list_t *timer_list, struct rt_spinlock *lock)
|
||||
|
||||
level = rt_spin_lock_irqsave(lock);
|
||||
|
||||
t->parent.flag &= ~RT_TIMER_FLAG_PROCESSING;
|
||||
|
||||
/* Check whether the timer object is detached or started again */
|
||||
if (rt_list_isempty(&list))
|
||||
{
|
||||
@@ -570,6 +572,10 @@ rt_err_t rt_timer_start(rt_timer_t timer)
|
||||
RT_ASSERT(timer != RT_NULL);
|
||||
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
|
||||
|
||||
#ifdef RT_USING_TIMER_ALL_SOFT
|
||||
timer_list = _soft_timer_list;
|
||||
spinlock = &_stimer_lock;
|
||||
#else
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
||||
{
|
||||
@@ -582,6 +588,7 @@ rt_err_t rt_timer_start(rt_timer_t timer)
|
||||
timer_list = _timer_list;
|
||||
spinlock = &_htimer_lock;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (timer->parent.flag & RT_TIMER_FLAG_THREAD_TIMER)
|
||||
{
|
||||
@@ -598,13 +605,6 @@ rt_err_t rt_timer_start(rt_timer_t timer)
|
||||
|
||||
err = _timer_start(timer_list, timer);
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
if (err == RT_EOK && (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER))
|
||||
{
|
||||
rt_sem_release(&_soft_timer_sem);
|
||||
}
|
||||
#endif /* RT_USING_TIMER_SOFT */
|
||||
|
||||
rt_spin_unlock_irqrestore(spinlock, level);
|
||||
|
||||
if (is_thread_timer)
|
||||
@@ -756,7 +756,20 @@ void rt_timer_check(void)
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
rt_err_t ret = RT_ERROR;
|
||||
rt_tick_t next_timeout;
|
||||
|
||||
ret = _timer_list_next_timeout(_soft_timer_list, &next_timeout);
|
||||
if ((ret == RT_EOK) && (next_timeout <= rt_tick_get()))
|
||||
{
|
||||
rt_sem_release(&_soft_timer_sem);
|
||||
}
|
||||
#endif
|
||||
#ifndef RT_USING_TIMER_ALL_SOFT
|
||||
_timer_check(_timer_list, &_htimer_lock);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -767,13 +780,21 @@ void rt_timer_check(void)
|
||||
rt_tick_t rt_timer_next_timeout_tick(void)
|
||||
{
|
||||
rt_base_t level;
|
||||
rt_tick_t next_timeout = RT_TICK_MAX;
|
||||
rt_tick_t htimer_next_timeout = RT_TICK_MAX, stimer_next_timeout = RT_TICK_MAX;
|
||||
|
||||
#ifndef RT_USING_TIMER_ALL_SOFT
|
||||
level = rt_spin_lock_irqsave(&_htimer_lock);
|
||||
_timer_list_next_timeout(_timer_list, &next_timeout);
|
||||
_timer_list_next_timeout(_timer_list, &htimer_next_timeout);
|
||||
rt_spin_unlock_irqrestore(&_htimer_lock, level);
|
||||
#endif
|
||||
|
||||
return next_timeout;
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
level = rt_spin_lock_irqsave(&_stimer_lock);
|
||||
_timer_list_next_timeout(_soft_timer_list, &stimer_next_timeout);
|
||||
rt_spin_unlock_irqrestore(&_stimer_lock, level);
|
||||
#endif
|
||||
|
||||
return htimer_next_timeout < stimer_next_timeout ? htimer_next_timeout : stimer_next_timeout;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_TIMER_SOFT
|
||||
@@ -784,41 +805,12 @@ rt_tick_t rt_timer_next_timeout_tick(void)
|
||||
*/
|
||||
static void _timer_thread_entry(void *parameter)
|
||||
{
|
||||
rt_err_t ret = RT_ERROR;
|
||||
rt_tick_t next_timeout;
|
||||
rt_base_t level;
|
||||
|
||||
RT_UNUSED(parameter);
|
||||
|
||||
rt_sem_control(&_soft_timer_sem, RT_IPC_CMD_SET_VLIMIT, (void*)1);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* get the next timeout tick */
|
||||
level = rt_spin_lock_irqsave(&_stimer_lock);
|
||||
ret = _timer_list_next_timeout(_soft_timer_list, &next_timeout);
|
||||
rt_spin_unlock_irqrestore(&_stimer_lock, level);
|
||||
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
rt_sem_take(&_soft_timer_sem, RT_WAITING_FOREVER);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_tick_t current_tick;
|
||||
|
||||
/* get current tick */
|
||||
current_tick = rt_tick_get();
|
||||
|
||||
if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
|
||||
{
|
||||
/* get the delta timeout tick */
|
||||
next_timeout = next_timeout - current_tick;
|
||||
rt_sem_take(&_soft_timer_sem, next_timeout);
|
||||
}
|
||||
}
|
||||
|
||||
_timer_check(_soft_timer_list, &_stimer_lock); /* check software timer */
|
||||
rt_sem_take(&_soft_timer_sem, RT_WAITING_FOREVER);
|
||||
}
|
||||
}
|
||||
#endif /* RT_USING_TIMER_SOFT */
|
||||
@@ -830,13 +822,16 @@ static void _timer_thread_entry(void *parameter)
|
||||
*/
|
||||
void rt_system_timer_init(void)
|
||||
{
|
||||
#ifndef RT_USING_TIMER_ALL_SOFT
|
||||
rt_size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(_timer_list) / sizeof(_timer_list[0]); i++)
|
||||
{
|
||||
rt_list_init(_timer_list + i);
|
||||
}
|
||||
|
||||
rt_spin_lock_init(&_htimer_lock);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -857,6 +852,7 @@ void rt_system_timer_thread_init(void)
|
||||
}
|
||||
rt_spin_lock_init(&_stimer_lock);
|
||||
rt_sem_init(&_soft_timer_sem, "stimer", 0, RT_IPC_FLAG_PRIO);
|
||||
rt_sem_control(&_soft_timer_sem, RT_IPC_CMD_SET_VLIMIT, (void*)1);
|
||||
/* start software timer thread */
|
||||
rt_thread_init(&_timer_thread,
|
||||
"timer",
|
||||
|
||||
Reference in New Issue
Block a user