From 3aa19af30120341a4344d69f8bd500682db2b8c5 Mon Sep 17 00:00:00 2001 From: xqyjlj Date: Mon, 13 Nov 2023 22:46:48 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix(src/idle.c):=20fix=20thread?= =?UTF-8?q?=20exit=20(#8239)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/idle.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/idle.c b/src/idle.c index 524b45d3b3..1c36814e1e 100644 --- a/src/idle.c +++ b/src/idle.c @@ -17,6 +17,7 @@ * combine the code of primary and secondary cpu * 2021-11-15 THEWON Remove duplicate work between idle and _thread_exit * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable + * 2023-11-07 xqyjlj fix thread exit */ #include @@ -43,7 +44,7 @@ #define _CPUS_NR RT_CPUS_NR static rt_list_t _rt_thread_defunct = RT_LIST_OBJECT_INIT(_rt_thread_defunct); -static struct rt_mutex _defunct_mutex; +static struct rt_spinlock _spinlock = RT_SPINLOCK_INIT; static rt_atomic_t _idle_inited = 0; static struct rt_thread idle_thread[_CPUS_NR]; rt_align(RT_ALIGN_SIZE) @@ -130,13 +131,14 @@ rt_err_t rt_thread_idle_delhook(void (*hook)(void)) */ void rt_thread_defunct_enqueue(rt_thread_t thread) { + rt_base_t level; if (rt_atomic_load(&_idle_inited) == 0) { return; } - rt_mutex_take(&_defunct_mutex, RT_WAITING_FOREVER); + level = rt_spin_lock_irqsave(&_spinlock); rt_list_insert_after(&_rt_thread_defunct, &thread->tlist); - rt_mutex_release(&_defunct_mutex); + rt_spin_unlock_irqrestore(&_spinlock, level); #ifdef RT_USING_SMP rt_sem_release(&system_sem); #endif @@ -152,7 +154,7 @@ rt_thread_t rt_thread_defunct_dequeue(void) rt_list_t *l = &_rt_thread_defunct; #ifdef RT_USING_SMP - rt_mutex_take(&_defunct_mutex, RT_WAITING_FOREVER); + level = rt_spin_lock_irqsave(&_spinlock); if (l->next != l) { thread = rt_list_entry(l->next, @@ -160,8 +162,7 @@ rt_thread_t rt_thread_defunct_dequeue(void) tlist); rt_list_remove(&(thread->tlist)); } - rt_mutex_release(&_defunct_mutex); - RT_UNUSED(level); + rt_spin_unlock_irqrestore(&_spinlock, level); #else if (l->next != l) { @@ -364,7 +365,7 @@ void rt_thread_idle_init(void) /* startup */ rt_thread_startup(&rt_system_thread); #endif - rt_mutex_init(&_defunct_mutex, "defunct_mutex", RT_IPC_FLAG_FIFO); + rt_spin_lock_init(&_spinlock); rt_atomic_store(&(_idle_inited), 1); }