From 6977cf91013724b8bf632cf4ad08f8dbb0392fb2 Mon Sep 17 00:00:00 2001 From: Shell Date: Wed, 8 May 2024 09:22:09 +0800 Subject: [src] add rt_hw_cpu_id() wrapper API (#8894) * [src] add rt_hw_cpu_id() wrapper API rt_hw_cpu_id() is an unsafe API which should not be used by most codes directly. It's error-prone because it must be used in proper context, otherwise it can lead to errors and unpredictable behavior. This patch adds a wrapper API for rt_hw_cpu_id() to address this risk. It includes the context-checking functionality and provides a safer alternative for obtaining CPU IDs, ensuring that it is used correctly within the appropriate context. Signed-off-by: Shell * fixup UMP * update API & comment * ci: cpp_check --------- Signed-off-by: Shell --- src/clock.c | 2 +- src/cpu_mp.c | 17 +++++++++++++++++ src/idle.c | 8 ++------ src/signal.c | 4 ++-- src/timer.c | 2 +- 5 files changed, 23 insertions(+), 10 deletions(-) (limited to 'src') 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; -- cgit v1.3.1