diff options
| author | xqyjlj <[email protected]> | 2023-10-25 20:31:25 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-25 20:31:25 +0800 |
| commit | 3283f54c7a0444b2e51ec79a4da57e7871528f05 (patch) | |
| tree | e4bcd748ad2362f16d0dea5c5da62922de466999 /src/clock.c | |
| parent | 91fc52df3635d5dda8285616420f08efa79cea04 (diff) | |
🎈 perf: perf rt_hw_interrupt_disable/enable (#8042)
Signed-off-by: Shell <[email protected]>
Co-authored-by: Shell <[email protected]>
Diffstat (limited to 'src/clock.c')
| -rw-r--r-- | src/clock.c | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/src/clock.c b/src/clock.c index ac146192b7..7c948f3bc1 100644 --- a/src/clock.c +++ b/src/clock.c @@ -15,16 +15,18 @@ * 2018-11-22 Jesven add per cpu tick * 2020-12-29 Meco Man implement rt_tick_get_millisecond() * 2021-06-01 Meco Man add critical section projection for rt_tick_increase() + * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable * 2023-10-16 RiceChen fix: only the main core detection rt_timer_check(), in SMP mode */ #include <rthw.h> #include <rtthread.h> +#include <rtatomic.h> #ifdef RT_USING_SMP #define rt_tick rt_cpu_index(0)->tick #else -static volatile rt_tick_t rt_tick = 0; +static volatile rt_atomic_t rt_tick = 0; #endif /* RT_USING_SMP */ #ifndef __on_rt_tick_hook @@ -67,7 +69,7 @@ void rt_tick_sethook(void (*hook)(void)) rt_tick_t rt_tick_get(void) { /* return the global tick */ - return rt_tick; + return (rt_tick_t)rt_atomic_load(&(rt_tick)); } RTM_EXPORT(rt_tick_get); @@ -78,11 +80,7 @@ RTM_EXPORT(rt_tick_get); */ void rt_tick_set(rt_tick_t tick) { - rt_base_t level; - - level = rt_hw_interrupt_disable(); - rt_tick = tick; - rt_hw_interrupt_enable(level); + rt_atomic_store(&(rt_tick), tick); } /** @@ -93,34 +91,32 @@ void rt_tick_increase(void) { struct rt_thread *thread; rt_base_t level; + rt_atomic_t oldval = 0; RT_OBJECT_HOOK_CALL(rt_tick_hook, ()); - - level = rt_hw_interrupt_disable(); - /* increase the global tick */ #ifdef RT_USING_SMP - rt_cpu_self()->tick ++; + rt_atomic_add(&(rt_cpu_self()->tick), 1); #else - ++ rt_tick; + rt_atomic_add(&(rt_tick), 1); #endif /* RT_USING_SMP */ /* check time slice */ thread = rt_thread_self(); - - -- thread->remaining_tick; - if (thread->remaining_tick == 0) + rt_get_thread_struct(thread); + level = rt_spin_lock_irqsave(&(thread->spinlock)); + rt_atomic_sub(&(thread->remaining_tick), 1); + if (rt_atomic_compare_exchange_strong(&(thread->remaining_tick), &oldval, thread->init_tick)) { - /* change to initialized tick */ - thread->remaining_tick = thread->init_tick; thread->stat |= RT_THREAD_STAT_YIELD; - - rt_hw_interrupt_enable(level); + rt_spin_unlock_irqrestore(&(thread->spinlock), level); + rt_put_thread_struct(thread); rt_schedule(); } else { - rt_hw_interrupt_enable(level); + rt_spin_unlock_irqrestore(&(thread->spinlock), level); + rt_put_thread_struct(thread); } /* check timer */ |
