Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[src] add rt_hw_cpu_id() wrapper API #8894

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions include/rtthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,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 */

Expand Down Expand Up @@ -780,7 +788,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 \
Expand Down Expand Up @@ -808,11 +815,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
Expand Down
2 changes: 1 addition & 1 deletion src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions src/cpu_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 2 additions & 6 deletions src/idle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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]);
}
4 changes: 2 additions & 2 deletions src/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tools/ci/cpp_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def check(self):
[
'cppcheck',
'-DRT_ASSERT(x)=',
'-DRTM_EXPORT(x)=',
'-Drt_list_for_each_entry(a,b,c)=a=(void*)b;',
'-I include',
'-I thread/components/finsh',
Expand Down
Loading