From d343c6578a0c12c4bdc98e7634907fb9779ba0d7 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Tue, 13 Nov 2018 11:32:01 +0800 Subject: [kernel]Change 'rt_timer_timeout_hook' function to 'rt_timer_enter_hook' and add 'rt_timer_exit_hook' hook function. --- src/timer.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/timer.c') diff --git a/src/timer.c b/src/timer.c index dc489a2d0d..fd938065db 100644 --- a/src/timer.c +++ b/src/timer.c @@ -43,7 +43,8 @@ static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE]; #ifdef RT_USING_HOOK extern void (*rt_object_take_hook)(struct rt_object *object); extern void (*rt_object_put_hook)(struct rt_object *object); -static void (*rt_timer_timeout_hook)(struct rt_timer *timer); +static void (*rt_timer_enter_hook)(struct rt_timer *timer); +static void (*rt_timer_exit_hook)(struct rt_timer *timer); /** * @addtogroup Hook @@ -52,14 +53,25 @@ static void (*rt_timer_timeout_hook)(struct rt_timer *timer); /**@{*/ /** - * This function will set a hook function, which will be invoked when timer - * is timeout. + * This function will set a hook function, which will be invoked when enter + * timer timeout callback function. * * @param hook the hook function */ -void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer)) +void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer)) { - rt_timer_timeout_hook = hook; + rt_timer_enter_hook = hook; +} + +/** + * This function will set a hook function, which will be invoked when exit + * timer timeout callback function. + * + * @param hook the hook function + */ +void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer)) +{ + rt_timer_exit_hook = hook; } /**@}*/ @@ -503,7 +515,7 @@ void rt_timer_check(void) */ if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2) { - RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t)); + RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t)); /* remove timer from timer list firstly */ _rt_timer_remove(t); @@ -514,6 +526,7 @@ void rt_timer_check(void) /* re-get tick */ current_tick = rt_tick_get(); + RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t)); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick)); if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) && @@ -578,7 +591,7 @@ void rt_soft_timer_check(void) */ if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2) { - RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t)); + RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t)); /* move node to the next */ n = n->next; @@ -594,6 +607,7 @@ void rt_soft_timer_check(void) /* re-get tick */ current_tick = rt_tick_get(); + RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t)); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick)); /* lock scheduler */ -- cgit v1.3.1 From de624c52c140f9b6e6d782025398b0c0a250d666 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sun, 13 Jan 2019 11:23:08 +0800 Subject: [Kernel] Enable RUNNING status in thread. --- src/scheduler.c | 13 +++++++++---- src/thread.c | 14 +++++++++----- src/timer.c | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src/timer.c') diff --git a/src/scheduler.c b/src/scheduler.c index 850c70306d..0ced21105e 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -265,6 +265,7 @@ void rt_system_scheduler_start(void) #endif /*RT_USING_SMP*/ rt_schedule_remove_thread(to_thread); + to_thread->stat = RT_THREAD_RUNNING; /* switch to new thread */ #ifdef RT_USING_SMP @@ -311,7 +312,7 @@ void rt_schedule(void) int cpu_id; /* disable interrupt */ - level = rt_hw_interrupt_disable(); + level = rt_hw_interrupt_disable(); cpu_id = rt_hw_cpu_id(); pcpu = rt_cpu_index(cpu_id); @@ -330,7 +331,7 @@ void rt_schedule(void) { to_thread = _get_highest_priority_thread(&highest_ready_priority); current_thread->oncpu = RT_CPU_DETACHED; - if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { if (current_thread->current_priority < highest_ready_priority) { @@ -350,6 +351,7 @@ void rt_schedule(void) RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread)); rt_schedule_remove_thread(to_thread); + to_thread->stat = RT_THREAD_RUNNING; /* switch to new thread */ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, @@ -414,7 +416,7 @@ void rt_schedule(void) to_thread = _get_highest_priority_thread(&highest_ready_priority); - if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { if (rt_current_thread->current_priority < highest_ready_priority) { @@ -441,6 +443,7 @@ void rt_schedule(void) } rt_schedule_remove_thread(to_thread); + to_thread->stat = RT_THREAD_RUNNING; /* switch to new thread */ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, @@ -482,6 +485,7 @@ void rt_schedule(void) else { rt_schedule_remove_thread(rt_current_thread); + rt_current_thread->stat = RT_THREAD_RUNNING; } } } @@ -531,7 +535,7 @@ void rt_scheduler_do_irq_switch(void *context) { to_thread = _get_highest_priority_thread(&highest_ready_priority); current_thread->oncpu = RT_CPU_DETACHED; - if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING) { if (current_thread->current_priority < highest_ready_priority) { @@ -552,6 +556,7 @@ void rt_scheduler_do_irq_switch(void *context) RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread)); rt_schedule_remove_thread(to_thread); + to_thread->stat = RT_THREAD_RUNNING; #ifdef RT_USING_OVERFLOW_CHECK _rt_scheduler_stack_check(to_thread); diff --git a/src/thread.c b/src/thread.c index df9f151834..b23d2fb6b9 100644 --- a/src/thread.c +++ b/src/thread.c @@ -655,6 +655,7 @@ RTM_EXPORT(rt_thread_control); */ rt_err_t rt_thread_suspend(rt_thread_t thread) { + register rt_base_t stat; register rt_base_t temp; /* thread check */ @@ -663,22 +664,25 @@ rt_err_t rt_thread_suspend(rt_thread_t thread) RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name)); - if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY) + stat = thread->stat & RT_THREAD_STAT_MASK; + if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING)) { RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n", thread->stat)); - return -RT_ERROR; } - RT_ASSERT(thread == rt_thread_self()); - /* disable interrupt */ temp = rt_hw_interrupt_disable(); + if (stat == RT_THREAD_RUNNING) + { + /* not suspend running status thread on other core */ + RT_ASSERT(thread == rt_thread_self()); + } /* change thread stat */ - thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK); rt_schedule_remove_thread(thread); + thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK); /* stop thread timer anyway */ rt_timer_stop(&(thread->thread_timer)); diff --git a/src/timer.c b/src/timer.c index fd938065db..3992cf6ff4 100644 --- a/src/timer.c +++ b/src/timer.c @@ -399,7 +399,7 @@ rt_err_t rt_timer_start(rt_timer_t timer) if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER) { /* check whether timer thread is ready */ - if ((timer_thread.stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY) + if ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND) { /* resume timer thread to check soft timer */ rt_thread_resume(&timer_thread); -- cgit v1.3.1 From 3b87cb8ded4437316b74d220217c4440fdda249f Mon Sep 17 00:00:00 2001 From: xieyangrun Date: Wed, 15 Jan 2020 09:34:47 +0800 Subject: [src/timer.c]fixed rt_timer_list_next_timeout multi-task safe --- src/timer.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/timer.c') diff --git a/src/timer.c b/src/timer.c index 3992cf6ff4..d85ee080b5 100644 --- a/src/timer.c +++ b/src/timer.c @@ -108,14 +108,23 @@ static void _rt_timer_init(rt_timer_t timer, static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[]) { struct rt_timer *timer; + register rt_base_t level; + rt_tick_t timeout_tick = RT_TICK_MAX; - if (rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1])) - return RT_TICK_MAX; + /* disable interrupt */ + level = rt_hw_interrupt_disable(); - timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next, - struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]); + if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1])) + { + timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next, + struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]); + timeout_tick = timer->timeout_tick; + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); - return timer->timeout_tick; + return timeout_tick; } rt_inline void _rt_timer_remove(rt_timer_t timer) -- cgit v1.3.1 From 720d8479a574746020747ef36bc33a8159206289 Mon Sep 17 00:00:00 2001 From: longtengmcu Date: Fri, 7 Feb 2020 16:43:39 +0800 Subject: 给软件定时器增加定时器工作状态查询的功能 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 1 + src/timer.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) (limited to 'src/timer.c') diff --git a/include/rtdef.h b/include/rtdef.h index c8184a5a4b..8b86213082 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -431,6 +431,7 @@ struct rt_object_information #define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */ #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */ #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */ +#define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/ #ifndef RT_TIMER_SKIP_LIST_LEVEL #define RT_TIMER_SKIP_LIST_LEVEL 1 diff --git a/src/timer.c b/src/timer.c index d85ee080b5..0034f0246c 100644 --- a/src/timer.c +++ b/src/timer.c @@ -488,6 +488,19 @@ rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg) case RT_TIMER_CTRL_SET_PERIODIC: timer->parent.flag |= RT_TIMER_FLAG_PERIODIC; break; + /* zhaoshimin 20191204 add query the timer state */ + case RT_TIMER_CTRL_GET_STATE: + if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) + { + /*timer is start and run*/ + *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED; + } + else + { + /*timer is stop*/ + *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED; + } + break; } return RT_EOK; -- cgit v1.3.1