Skip to content
Open
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
35 changes: 19 additions & 16 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,24 @@ static ALWAYS_INLINE void update_cache(int preempt_ok)
#endif /* CONFIG_SMP */
}

/**
* Returns pointer to _cpu if the thread is currently running on
* another CPU.
*/
static struct _cpu *thread_active_elsewhere(struct k_thread *thread)
{
/* Returns pointer to _cpu if the thread is currently running on
* another CPU. There are more scalable designs to answer this
* question in constant time, but this is fine for now.
*/
#ifdef CONFIG_SMP
int currcpu = _current_cpu->id;
int thread_cpu_id = thread->base.cpu;
struct _cpu *thread_cpu;

unsigned int num_cpus = arch_num_cpus();
__ASSERT_NO_MSG((thread_cpu_id >= 0) &&
(thread_cpu_id < arch_num_cpus()));

for (int i = 0; i < num_cpus; i++) {
if ((i != currcpu) &&
(_kernel.cpus[i].current == thread)) {
return &_kernel.cpus[i];
}
thread_cpu = &_kernel.cpus[thread_cpu_id];
if ((thread_cpu->current == thread) && (thread_cpu != _current_cpu)) {
return thread_cpu;
}

#endif /* CONFIG_SMP */
ARG_UNUSED(thread);
return NULL;
Expand Down Expand Up @@ -386,11 +387,13 @@ static void thread_halt_spin(struct k_thread *thread, k_spinlock_key_t key)
static ALWAYS_INLINE void z_metairq_preempted_clear(struct k_thread *thread)
{
#if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
for (unsigned int i = 0; i < CONFIG_MP_MAX_NUM_CPUS; i++) {
if (_kernel.cpus[i].metairq_preempted == thread) {
_kernel.cpus[i].metairq_preempted = NULL;
break;
}
unsigned int cpu_id = 0;

#if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
cpu_id = thread->base.cpu;
#endif
if (_kernel.cpus[cpu_id].metairq_preempted == thread) {
_kernel.cpus[cpu_id].metairq_preempted = NULL;
}
#endif
}
Expand Down
1 change: 1 addition & 0 deletions kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ void z_init_thread_base(struct _thread_base *thread_base, int priority,

#ifdef CONFIG_SMP
thread_base->is_idle = 0;
thread_base->cpu = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means the run on cpu 0 last time, should we use a magic number to indicate edge case for thread does not run before?

Additional, there is comment from _thread_base, the semantic is changed now.

/* CPU index on which thread was last run */
uint8_t cpu;

By the way, I think it is used to cpu affinity before but I am not sure we have this feature or not.

#endif /* CONFIG_SMP */

#ifdef CONFIG_TIMESLICE_PER_THREAD
Expand Down
Loading