summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShell <[email protected]>2024-05-08 09:22:09 +0800
committerGitHub <[email protected]>2024-05-08 09:22:09 +0800
commit6977cf91013724b8bf632cf4ad08f8dbb0392fb2 (patch)
tree00bb4f5b9bf00714b8176db4fced2e09e096d6f5 /src
parent02e0334ac3dacca463c36bcbb7f71b0f35c2af85 (diff)
[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 <[email protected]> * fixup UMP * update API & comment * ci: cpp_check --------- Signed-off-by: Shell <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/clock.c2
-rw-r--r--src/cpu_mp.c17
-rw-r--r--src/idle.c8
-rw-r--r--src/signal.c4
-rw-r--r--src/timer.c2
5 files changed, 23 insertions, 10 deletions
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;