From 6c283ca3d01f3d0232d0df0fe2e21972cc7cad39 Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Fri, 16 Aug 2019 22:09:30 -0700 Subject: [PATCH] kernel/thread: Must always initialize is_idle field Our thread struct gets initialized piecewise in a bunch of locations (this is sort of a design flaw). The is_idle field, which was introduced to identify idle threads in SMP (where there can be more than one), was correctly set for idle threads but was being left uninitialized elsewhere, and in a tiny handful of cases was turning up nonzero. The case in pipes. was particularly vexsome, as that isn't a thread at all but one of the "dummy" threads used for timeouts (another design flaw IMHO). Get this right everywhere. Signed-off-by: Andy Ross --- kernel/init.c | 8 ++++---- kernel/pipes.c | 3 +++ kernel/thread.c | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/kernel/init.c b/kernel/init.c index e2900e6b28a..bfbebea3d61 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -310,14 +310,14 @@ void __weak main(void) #if defined(CONFIG_MULTITHREADING) static void init_idle_thread(struct k_thread *thr, k_thread_stack_t *stack) { -#ifdef CONFIG_SMP - thr->base.is_idle = 1U; -#endif - z_setup_new_thread(thr, stack, IDLE_STACK_SIZE, idle, NULL, NULL, NULL, K_LOWEST_THREAD_PRIO, K_ESSENTIAL, IDLE_THREAD_NAME); z_mark_thread_as_started(thr); + +#ifdef CONFIG_SMP + thr->base.is_idle = 1U; +#endif } #endif /* CONFIG_MULTITHREADING */ diff --git a/kernel/pipes.c b/kernel/pipes.c index 83a1c5b1461..e48fa9b1450 100644 --- a/kernel/pipes.c +++ b/kernel/pipes.c @@ -767,6 +767,9 @@ void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, async_desc->desc.copy_block = *block; async_desc->desc.sem = sem; async_desc->thread.prio = k_thread_priority_get(_current); +#ifdef CONFIG_SMP + async_desc->thread.is_idle = 0; +#endif (void) z_pipe_put_internal(pipe, async_desc, block->data, bytes_to_write, &dummy_bytes_written, diff --git a/kernel/thread.c b/kernel/thread.c index b0a2bca138a..b6b8a70969c 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -775,6 +775,10 @@ void z_init_thread_base(struct _thread_base *thread_base, int priority, thread_base->sched_locked = 0U; +#ifdef CONFIG_SMP + thread_base->is_idle = 0; +#endif + /* swap_data does not need to be initialized */ z_init_thread_timeout(thread_base);