* @brief Waits for sibling threads to exit during process termination * * @param[in] lwp Pointer to the lightweight process structure * @param[in] curr_thread Current thread context making the termination request * * @note Details for this function: * - Broadcast Exit Request: Broadcasts exit requests to all sibling threads. * - Wake Suspended Threads: Wakes up any suspended thr
| 1976 | * and deletes their thread control blocks. |
| 1977 | */ |
| 1978 | static void _wait_sibling_exit(rt_lwp_t lwp, rt_thread_t curr_thread) |
| 1979 | { |
| 1980 | rt_sched_lock_level_t slvl; |
| 1981 | rt_list_t *list; |
| 1982 | rt_thread_t thread; |
| 1983 | rt_size_t expected = LWP_EXIT_REQUEST_NONE; |
| 1984 | |
| 1985 | /* broadcast exit request for sibling threads */ |
| 1986 | LWP_LOCK(lwp); |
| 1987 | for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next) |
| 1988 | { |
| 1989 | thread = rt_list_entry(list, struct rt_thread, sibling); |
| 1990 | |
| 1991 | atomic_compare_exchange_strong(&thread->exit_request, &expected, |
| 1992 | LWP_EXIT_REQUEST_TRIGGERED); |
| 1993 | |
| 1994 | rt_sched_lock(&slvl); |
| 1995 | /* dont release, otherwise thread may have been freed */ |
| 1996 | if (rt_sched_thread_is_suspended(thread)) |
| 1997 | { |
| 1998 | thread->error = RT_EINTR; |
| 1999 | rt_sched_unlock(slvl); |
| 2000 | |
| 2001 | rt_thread_wakeup(thread); |
| 2002 | } |
| 2003 | else |
| 2004 | { |
| 2005 | rt_sched_unlock(slvl); |
| 2006 | } |
| 2007 | } |
| 2008 | LWP_UNLOCK(lwp); |
| 2009 | |
| 2010 | while (1) |
| 2011 | { |
| 2012 | int subthread_is_terminated; |
| 2013 | LOG_D("%s: wait for subthread exiting", __func__); |
| 2014 | |
| 2015 | /** |
| 2016 | * Brief: wait for all *running* sibling threads to exit |
| 2017 | * |
| 2018 | * Note: Critical Section |
| 2019 | * - sibling list of lwp (RW. It will clear all siblings finally) |
| 2020 | */ |
| 2021 | LWP_LOCK(lwp); |
| 2022 | subthread_is_terminated = (int)(curr_thread->sibling.prev == &lwp->t_grp); |
| 2023 | if (!subthread_is_terminated) |
| 2024 | { |
| 2025 | rt_sched_lock_level_t slvl; |
| 2026 | rt_thread_t sub_thread; |
| 2027 | rt_list_t *list; |
| 2028 | int all_subthread_in_init = 1; |
| 2029 | |
| 2030 | /* check all subthread is in init state */ |
| 2031 | for (list = curr_thread->sibling.prev; list != &lwp->t_grp; list = list->prev) |
| 2032 | { |
| 2033 | rt_sched_lock(&slvl); |
| 2034 | sub_thread = rt_list_entry(list, struct rt_thread, sibling); |
| 2035 | if (rt_sched_thread_get_stat(sub_thread) != RT_THREAD_INIT) |
no test coverage detected