This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 5feda17b6e8449fe2c3e148384b96c0197be9a2f Author: ganjing <[email protected]> AuthorDate: Mon Nov 17 21:25:06 2025 +0800 sched/pthread: Detect circular joins in pthread_join(). pthread_join() returned EDEADLK only when a thread joined itself. Threads that join each other in a circle, for example A joins B while B joins A, blocked forever. POSIX allows pthread_join() to fail with EDEADLK when it detects a deadlock. Follow the chain of the threads that the target is joining, and return EDEADLK if it leads back to the caller. Each step walks the members of the task group, so the check is only built with CONFIG_DEBUG_FEATURES. Assisted-by: Kiro:auto Signed-off-by: ganjing <[email protected]> --- sched/pthread/pthread_join.c | 148 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/sched/pthread/pthread_join.c b/sched/pthread/pthread_join.c index 8e32d530d47..d578bd7d0dd 100644 --- a/sched/pthread/pthread_join.c +++ b/sched/pthread/pthread_join.c @@ -38,6 +38,145 @@ #include "sched/sched.h" #include "pthread/pthread.h" +#ifdef CONFIG_DEBUG_FEATURES +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: is_thread_in_join_queue + * + * Description: + * Check if a specific thread is in another thread's wait queue. + * + * Input Parameters: + * joiner - Thread to look for in the wait queue. + * joinee - Thread whose wait queue to check. + * + * Returned Value: + * true if joiner is in joinee's wait queue, false otherwise. + * + ****************************************************************************/ + +static bool is_thread_in_join_queue(FAR struct tcb_s *joiner, + FAR struct tcb_s *joinee) +{ + FAR struct tcb_s *tcb; + + sq_for_every_entry(&joinee->join_queue, tcb, struct tcb_s, join_entry) + { + if (tcb == joiner) + { + return true; + } + } + + return false; +} + +/**************************************************************************** + * Name: is_thread_waiting_for_join + * + * Description: + * Check if a thread is currently waiting in a pthread_join operation. + * + * Input Parameters: + * joiner - Thread to check. + * + * Returned Value: + * true if thread is waiting in pthread_join, false otherwise. + * + ****************************************************************************/ + +static bool is_thread_waiting_for_join(FAR struct tcb_s *joiner) +{ + /* Check if the thread is waiting on its own join_sem */ + + return joiner->waitobj == &joiner->join_sem; +} + +/**************************************************************************** + * Name: find_thread_waiting_for_join + * + * Description: + * Find which thread a given thread is currently waiting for by examining + * all threads in the task group and their wait queues. + * + * Input Parameters: + * joiner - Thread to check. + * + * Returned Value: + * Pointer to the thread being waited for, or NULL if not waiting. + * + ****************************************************************************/ + +static FAR struct tcb_s * +find_thread_waiting_for_join(FAR struct tcb_s *joiner) +{ + FAR struct task_group_s *group = joiner->group; + FAR struct tcb_s *joinee; + + /* Check if the thread is waiting for a join operation */ + + if (!is_thread_waiting_for_join(joiner)) + { + return NULL; + } + + sq_for_every_entry(&group->tg_members, joinee, struct tcb_s, member) + { + /* Check if the thread is in this potential joinee's wait queue */ + + if (is_thread_in_join_queue(joiner, joinee)) + { + return joinee; + } + } + + return NULL; +} + +/**************************************************************************** + * Name: is_thread_deadlock_for_join + * + * Description: + * Check if attempting to join the target thread would create a deadlock + * situation (circular waiting). + * + * Input Parameters: + * joiner - Thread attempting to execute pthread_join. + * joinee - Target thread to be joined. + * + * Returned Value: + * true if deadlock would occur, false otherwise. + * + ****************************************************************************/ + +static bool is_thread_deadlock_for_join(FAR struct tcb_s *joiner, + FAR struct tcb_s *joinee) +{ + size_t depth = sq_count(&joinee->group->tg_members); + + /* Check for a circular wait */ + + while (depth-- > 0) + { + joinee = find_thread_waiting_for_join(joinee); + if (joinee == NULL) + { + return false; /* Chain ended, no circular wait */ + } + + if (joinee == joiner) + { + return true; /* Found joiner, circular wait detected */ + } + } + + return false; /* No circular wait detected */ +} +#endif /* CONFIG_DEBUG_FEATURES */ + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -136,6 +275,15 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) sq_addfirst(&rtcb->join_entry, &tcb->join_queue); +#ifdef CONFIG_DEBUG_FEATURES + if (is_thread_deadlock_for_join(rtcb, tcb)) + { + sq_rem(&rtcb->join_entry, &tcb->join_queue); + ret = EDEADLK; + goto errout; + } +#endif + nxrmutex_unlock(&group->tg_mutex); /* Take the thread's thread exit semaphore. We will sleep here
