Skip to content

Commit d30aeaf

Browse files
committed
[add][thread]Add assertions for duplicate deletion across threads and warnings for incorrect usage.
1 parent db4fb4c commit d30aeaf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/thread.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,30 @@ static rt_err_t _thread_detach(rt_thread_t thread)
482482
{
483483
rt_err_t error;
484484
rt_base_t critical_level;
485+
rt_uint8_t thread_status;
485486

486487
/**
487488
* forbid scheduling on current core before returning since current thread
488489
* may be detached from scheduler.
489490
*/
490491
critical_level = rt_enter_critical();
491492

493+
/* get thread status and warn about unsafe deletion scenarios */
494+
thread_status = rt_sched_thread_get_stat(thread);
495+
496+
/* assert thread is not already closed to prevent duplicate deletion */
497+
RT_ASSERT(thread_status != RT_THREAD_CLOSE);
498+
499+
/* warn if deleting a running or ready thread */
500+
if ((thread_status == RT_THREAD_RUNNING) || (thread_status == RT_THREAD_READY))
501+
{
502+
rt_exit_critical_safe(critical_level);
503+
LOG_W("Warning: Deleting active thread [%s] in %s state may cause resource leak",
504+
thread->parent.name,
505+
(thread_status == RT_THREAD_RUNNING) ? "RUNNING" : "READY");
506+
critical_level = rt_enter_critical();
507+
}
508+
492509
error = rt_thread_close(thread);
493510

494511
_thread_detach_from_mutex(thread);

0 commit comments

Comments
 (0)