diff --git a/include/rtthread.h b/include/rtthread.h index b73f63f38a..9cc0c4d879 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -676,11 +676,19 @@ void rt_interrupt_leave(void); rt_base_t rt_cpus_lock(void); void rt_cpus_unlock(rt_base_t level); +void rt_cpus_lock_status_restore(struct rt_thread *thread); struct rt_cpu *rt_cpu_self(void); struct rt_cpu *rt_cpu_index(int index); -void rt_cpus_lock_status_restore(struct rt_thread *thread); +#ifdef RT_USING_DEBUG + rt_base_t rt_cpu_get_id(void); +#else /* !RT_USING_DEBUG */ + #define rt_cpu_get_id rt_hw_cpu_id +#endif /* RT_USING_DEBUG */ + +#else /* !RT_USING_SMP */ +#define rt_cpu_get_id() (0) #endif /* RT_USING_SMP */ @@ -781,7 +789,6 @@ while (0) * 1) the scheduler has been started. * 2) not in interrupt context. * 3) scheduler is not locked. - * 4) interrupt is not disabled. */ #define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) \ do \ @@ -809,11 +816,27 @@ rt_inline rt_bool_t rt_in_thread_context(void) return rt_thread_self() != RT_NULL && rt_interrupt_get_nest() == 0; } +/* is scheduler available */ rt_inline rt_bool_t rt_scheduler_is_available(void) { - return !rt_hw_interrupt_is_disabled() && rt_critical_level() == 0 && rt_in_thread_context(); + return rt_critical_level() == 0 && rt_in_thread_context(); } +#ifdef RT_USING_SMP +/* is thread bond on core */ +rt_inline rt_bool_t rt_sched_thread_is_binding(rt_thread_t thread) +{ + if (thread == RT_NULL) + { + thread = rt_thread_self(); + } + return !thread || RT_SCHED_CTX(thread).bind_cpu != RT_CPUS_NR; +} + +#else +#define rt_sched_thread_is_binding(thread) (RT_TRUE) +#endif + /**@}*/ #ifdef __cplusplus diff --git a/src/clock.c b/src/clock.c index 42e361226f..c2fe321c33 100644 --- a/src/clock.c +++ b/src/clock.c @@ -101,7 +101,7 @@ void rt_tick_increase(void) /* check timer */ #ifdef RT_USING_SMP - if (rt_hw_cpu_id() != 0) + if (rt_cpu_get_id() != 0) { return; } diff --git a/src/cpu_mp.c b/src/cpu_mp.c index c27374707c..8da4c3e180 100644 --- a/src/cpu_mp.c +++ b/src/cpu_mp.c @@ -216,3 +216,20 @@ void rt_cpus_lock_status_restore(struct rt_thread *thread) rt_sched_post_ctx_switch(thread); } RTM_EXPORT(rt_cpus_lock_status_restore); + +/* A safe API with debugging feature to be called in most codes */ + +/** + * @brief Get logical CPU ID + * + * @return logical CPU ID + */ +rt_base_t rt_cpu_get_id(void) +{ + + RT_ASSERT(rt_sched_thread_is_binding(RT_NULL) || + rt_hw_interrupt_is_disabled() || + !rt_scheduler_is_available()); + + return rt_hw_cpu_id(); +} diff --git a/src/idle.c b/src/idle.c index 054e525ef3..a428a649c1 100644 --- a/src/idle.c +++ b/src/idle.c @@ -261,7 +261,7 @@ static void idle_thread_entry(void *parameter) { RT_UNUSED(parameter); #ifdef RT_USING_SMP - if (rt_hw_cpu_id() != 0) + if (rt_cpu_get_id() != 0) { while (1) { @@ -380,11 +380,7 @@ void rt_thread_idle_init(void) */ rt_thread_t rt_thread_idle_gethandler(void) { -#ifdef RT_USING_SMP - int id = rt_hw_cpu_id(); -#else - int id = 0; -#endif /* RT_USING_SMP */ + int id = rt_cpu_get_id(); return (rt_thread_t)(&idle_thread[id]); } diff --git a/src/signal.c b/src/signal.c index 28d0fd1c9f..552d2a89cd 100644 --- a/src/signal.c +++ b/src/signal.c @@ -142,7 +142,7 @@ static void _signal_deliver(rt_thread_t tid) int cpu_id; cpu_id = RT_SCHED_CTX(tid).oncpu; - if ((cpu_id != RT_CPU_DETACHED) && (cpu_id != rt_hw_cpu_id())) + if ((cpu_id != RT_CPU_DETACHED) && (cpu_id != rt_cpu_get_id())) { rt_uint32_t cpu_mask; @@ -181,7 +181,7 @@ void *rt_signal_check(void* context) level = rt_spin_lock_irqsave(&_thread_signal_lock); - cpu_id = rt_hw_cpu_id(); + cpu_id = rt_cpu_get_id(); pcpu = rt_cpu_index(cpu_id); current_thread = pcpu->current_thread; diff --git a/src/timer.c b/src/timer.c index 4d7b352d60..143bbebb12 100644 --- a/src/timer.c +++ b/src/timer.c @@ -684,7 +684,7 @@ void rt_timer_check(void) #ifdef RT_USING_SMP /* Running on core 0 only */ - if (rt_hw_cpu_id() != 0) + if (rt_cpu_get_id() != 0) { rt_spin_unlock_irqrestore(&_htimer_lock, level); return; diff --git a/tools/ci/cpp_check.py b/tools/ci/cpp_check.py index e7b2847c4a..5e7e100525 100644 --- a/tools/ci/cpp_check.py +++ b/tools/ci/cpp_check.py @@ -27,6 +27,7 @@ class CPPCheck: [ 'cppcheck', '-DRT_ASSERT(x)=', + '-DRTM_EXPORT(x)=', '-Drt_list_for_each_entry(a,b,c)=a=(void*)b;', '-I include', '-I thread/components/finsh',