Skip to content

Commit 6977cf9

Browse files
authored
[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]>
1 parent 02e0334 commit 6977cf9

File tree

7 files changed

+50
-13
lines changed

7 files changed

+50
-13
lines changed

include/rtthread.h

+26-3
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,19 @@ void rt_interrupt_leave(void);
676676

677677
rt_base_t rt_cpus_lock(void);
678678
void rt_cpus_unlock(rt_base_t level);
679+
void rt_cpus_lock_status_restore(struct rt_thread *thread);
679680

680681
struct rt_cpu *rt_cpu_self(void);
681682
struct rt_cpu *rt_cpu_index(int index);
682683

683-
void rt_cpus_lock_status_restore(struct rt_thread *thread);
684+
#ifdef RT_USING_DEBUG
685+
rt_base_t rt_cpu_get_id(void);
686+
#else /* !RT_USING_DEBUG */
687+
#define rt_cpu_get_id rt_hw_cpu_id
688+
#endif /* RT_USING_DEBUG */
689+
690+
#else /* !RT_USING_SMP */
691+
#define rt_cpu_get_id() (0)
684692

685693
#endif /* RT_USING_SMP */
686694

@@ -781,7 +789,6 @@ while (0)
781789
* 1) the scheduler has been started.
782790
* 2) not in interrupt context.
783791
* 3) scheduler is not locked.
784-
* 4) interrupt is not disabled.
785792
*/
786793
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) \
787794
do \
@@ -809,11 +816,27 @@ rt_inline rt_bool_t rt_in_thread_context(void)
809816
return rt_thread_self() != RT_NULL && rt_interrupt_get_nest() == 0;
810817
}
811818

819+
/* is scheduler available */
812820
rt_inline rt_bool_t rt_scheduler_is_available(void)
813821
{
814-
return !rt_hw_interrupt_is_disabled() && rt_critical_level() == 0 && rt_in_thread_context();
822+
return rt_critical_level() == 0 && rt_in_thread_context();
823+
}
824+
825+
#ifdef RT_USING_SMP
826+
/* is thread bond on core */
827+
rt_inline rt_bool_t rt_sched_thread_is_binding(rt_thread_t thread)
828+
{
829+
if (thread == RT_NULL)
830+
{
831+
thread = rt_thread_self();
832+
}
833+
return !thread || RT_SCHED_CTX(thread).bind_cpu != RT_CPUS_NR;
815834
}
816835

836+
#else
837+
#define rt_sched_thread_is_binding(thread) (RT_TRUE)
838+
#endif
839+
817840
/**@}*/
818841

819842
#ifdef __cplusplus

src/clock.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void rt_tick_increase(void)
101101

102102
/* check timer */
103103
#ifdef RT_USING_SMP
104-
if (rt_hw_cpu_id() != 0)
104+
if (rt_cpu_get_id() != 0)
105105
{
106106
return;
107107
}

src/cpu_mp.c

+17
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,20 @@ void rt_cpus_lock_status_restore(struct rt_thread *thread)
216216
rt_sched_post_ctx_switch(thread);
217217
}
218218
RTM_EXPORT(rt_cpus_lock_status_restore);
219+
220+
/* A safe API with debugging feature to be called in most codes */
221+
222+
/**
223+
* @brief Get logical CPU ID
224+
*
225+
* @return logical CPU ID
226+
*/
227+
rt_base_t rt_cpu_get_id(void)
228+
{
229+
230+
RT_ASSERT(rt_sched_thread_is_binding(RT_NULL) ||
231+
rt_hw_interrupt_is_disabled() ||
232+
!rt_scheduler_is_available());
233+
234+
return rt_hw_cpu_id();
235+
}

src/idle.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static void idle_thread_entry(void *parameter)
261261
{
262262
RT_UNUSED(parameter);
263263
#ifdef RT_USING_SMP
264-
if (rt_hw_cpu_id() != 0)
264+
if (rt_cpu_get_id() != 0)
265265
{
266266
while (1)
267267
{
@@ -380,11 +380,7 @@ void rt_thread_idle_init(void)
380380
*/
381381
rt_thread_t rt_thread_idle_gethandler(void)
382382
{
383-
#ifdef RT_USING_SMP
384-
int id = rt_hw_cpu_id();
385-
#else
386-
int id = 0;
387-
#endif /* RT_USING_SMP */
383+
int id = rt_cpu_get_id();
388384

389385
return (rt_thread_t)(&idle_thread[id]);
390386
}

src/signal.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static void _signal_deliver(rt_thread_t tid)
142142
int cpu_id;
143143

144144
cpu_id = RT_SCHED_CTX(tid).oncpu;
145-
if ((cpu_id != RT_CPU_DETACHED) && (cpu_id != rt_hw_cpu_id()))
145+
if ((cpu_id != RT_CPU_DETACHED) && (cpu_id != rt_cpu_get_id()))
146146
{
147147
rt_uint32_t cpu_mask;
148148

@@ -181,7 +181,7 @@ void *rt_signal_check(void* context)
181181

182182
level = rt_spin_lock_irqsave(&_thread_signal_lock);
183183

184-
cpu_id = rt_hw_cpu_id();
184+
cpu_id = rt_cpu_get_id();
185185
pcpu = rt_cpu_index(cpu_id);
186186
current_thread = pcpu->current_thread;
187187

src/timer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ void rt_timer_check(void)
684684

685685
#ifdef RT_USING_SMP
686686
/* Running on core 0 only */
687-
if (rt_hw_cpu_id() != 0)
687+
if (rt_cpu_get_id() != 0)
688688
{
689689
rt_spin_unlock_irqrestore(&_htimer_lock, level);
690690
return;

tools/ci/cpp_check.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def check(self):
2727
[
2828
'cppcheck',
2929
'-DRT_ASSERT(x)=',
30+
'-DRTM_EXPORT(x)=',
3031
'-Drt_list_for_each_entry(a,b,c)=a=(void*)b;',
3132
'-I include',
3233
'-I thread/components/finsh',

0 commit comments

Comments
 (0)