* @brief Allocates a new PID while holding the PID management lock * * @return pid_t The newly allocated PID, or 0 if allocation failed * * @note This function attempts to allocate a new process ID (PID) from either: * 1. The free list (lwp_pid_free_head) if available * 2. The PID array (lwp_pid_ary) if within maximum limits * * It then searches for an unused PID in two r
| 217 | * and current_pid is updated to the new PID. |
| 218 | */ |
| 219 | static pid_t lwp_pid_get_locked(void) |
| 220 | { |
| 221 | struct lwp_avl_struct *p; |
| 222 | pid_t pid = 0; |
| 223 | |
| 224 | p = lwp_pid_free_head; |
| 225 | if (p) |
| 226 | { |
| 227 | lwp_pid_free_head = (struct lwp_avl_struct *)p->avl_right; |
| 228 | } |
| 229 | else if (lwp_pid_ary_alloced < RT_LWP_MAX_NR) |
| 230 | { |
| 231 | p = lwp_pid_ary + lwp_pid_ary_alloced; |
| 232 | lwp_pid_ary_alloced++; |
| 233 | } |
| 234 | if (p) |
| 235 | { |
| 236 | int found_noused = 0; |
| 237 | |
| 238 | RT_ASSERT(p->data == RT_NULL); |
| 239 | for (pid = current_pid + 1; pid < PID_MAX; pid++) |
| 240 | { |
| 241 | if (!lwp_avl_find(pid, lwp_pid_root)) |
| 242 | { |
| 243 | found_noused = 1; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | if (!found_noused) |
| 248 | { |
| 249 | for (pid = 1; pid <= current_pid; pid++) |
| 250 | { |
| 251 | if (!lwp_avl_find(pid, lwp_pid_root)) |
| 252 | { |
| 253 | found_noused = 1; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | p->avl_key = pid; |
| 259 | lwp_avl_insert(p, &lwp_pid_root); |
| 260 | current_pid = pid; |
| 261 | } |
| 262 | return pid; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @brief Release a PID back to the free list while holding the PID lock |
no test coverage detected