* @brief Clean up resources when a lightweight process (LWP) terminates * * @param[in,out] lwp The lightweight process structure to clean up * * @note This function handles the cleanup of various resources associated with an LWP * when it terminates, including: * - Job control cleanup * - Signal detachment * - Child process handling * - Parent notification *
| 2130 | * - PID resource release |
| 2131 | */ |
| 2132 | static void _resr_cleanup(struct rt_lwp *lwp) |
| 2133 | { |
| 2134 | int need_cleanup_pid = RT_FALSE; |
| 2135 | lwp_jobctrl_on_exit(lwp); |
| 2136 | |
| 2137 | LWP_LOCK(lwp); |
| 2138 | lwp_signal_detach(&lwp->signal); |
| 2139 | |
| 2140 | /** |
| 2141 | * @brief Detach children from lwp |
| 2142 | * |
| 2143 | * @note Critical Section |
| 2144 | * - the lwp (RW. Release lwp) |
| 2145 | * - the pid resource manager (RW. Release the pid) |
| 2146 | */ |
| 2147 | while (lwp->first_child) |
| 2148 | { |
| 2149 | struct rt_lwp *child; |
| 2150 | |
| 2151 | child = lwp->first_child; |
| 2152 | lwp->first_child = child->sibling; |
| 2153 | |
| 2154 | /** @note safe since the slist node is release */ |
| 2155 | LWP_UNLOCK(lwp); |
| 2156 | LWP_LOCK(child); |
| 2157 | if (child->terminated) |
| 2158 | { |
| 2159 | lwp_pid_put(child); |
| 2160 | } |
| 2161 | else |
| 2162 | { |
| 2163 | child->sibling = RT_NULL; |
| 2164 | /* info: this may cause an orphan lwp */ |
| 2165 | child->parent = RT_NULL; |
| 2166 | } |
| 2167 | |
| 2168 | LWP_UNLOCK(child); |
| 2169 | lwp_ref_dec(child); |
| 2170 | lwp_ref_dec(lwp); |
| 2171 | |
| 2172 | LWP_LOCK(lwp); |
| 2173 | } |
| 2174 | LWP_UNLOCK(lwp); |
| 2175 | |
| 2176 | /** |
| 2177 | * @brief Wakeup parent if it's waiting for this lwp, otherwise a signal |
| 2178 | * will be sent to parent |
| 2179 | * |
| 2180 | * @note Critical Section |
| 2181 | * - the parent lwp (RW.) |
| 2182 | */ |
| 2183 | LWP_LOCK(lwp); |
| 2184 | if (lwp->parent && |
| 2185 | !lwp_sigismember(&lwp->parent->signal.sig_action_nocldwait, SIGCHLD)) |
| 2186 | { |
| 2187 | /* if successfully race to setup lwp->terminated before parent detach */ |
| 2188 | LWP_UNLOCK(lwp); |
| 2189 |
no test coverage detected