hujun260 opened a new pull request, #18009: URL: https://github.com/apache/nuttx/pull/18009
## Summary Convert the task group child count field (`tg_nchildren`) from a regular uint16_t integer to atomic_t type, replacing direct read/write operations with atomic operations (atomic_read, atomic_set, atomic_fetch_add, atomic_fetch_sub). This eliminates potential race conditions in multi-threaded/SMP scenarios where child process counts may be modified concurrently without explicit synchronization. ## Changes **File modified:** - `include/nuttx/sched.h` - `binfmt/binfmt_execmodule.c` - `sched/sched/sched_waitid.c` - `sched/sched/sched_waitpid.c` - `sched/task/task_exithook.c` - `sched/task/task_reparent.c` - `sched/task/task_setup.c` **Key changes:** - Changed `tg_nchildren` field type from `uint16_t` to `atomic_t` in task_group_s structure - Converted all direct reads to `atomic_read(&group->tg_nchildren)` - Converted all direct writes to `atomic_set(&group->tg_nchildren, value)` - Converted increment operations to `atomic_fetch_add(&group->tg_nchildren, 1)` - Converted decrement operations to `atomic_fetch_sub(&group->tg_nchildren, 1)` - Updated DEBUGASSERT conditions to use atomic_read for consistency ## Benefits & Technical Details **Race Condition Elimination**: Prevents data races where multiple contexts (tasks, interrupt handlers) might simultaneously read and modify the child count, which could lead to: - Lost increments or decrements - Stale count values - Incorrect parent-child relationship accounting - Potential use-after-free if child count goes negative **Lock-Free Synchronization**: Uses atomic operations instead of spinlocks for the child count counter, providing: - Better performance than mutex/spinlock-based synchronization - Reduced latency for process management operations - Natural fit for simple integer counter operations - No risk of deadlock **SMP Safety**: Ensures correct operation in multi-core systems where parent and child process state changes may occur on different CPUs simultaneously. **No Behavioral Change**: Atomic operations maintain identical semantics to the original code while providing thread-safe access guarantees. ## Testing - [x] Verified all child count operations converted to atomic equivalents - [x] Confirmed no direct access to tg_nchildren field remains in codebase - [x] Tested process creation/termination under concurrent load - [x] Validated wait operations with multiple child processes - [x] Tested task reparenting with atomic counter operations - [x] Verified SMP builds with race condition detection enabled ## Impact - **Stability**: Eliminates potential race conditions in child process accounting - **Compatibility**: Fully backward compatible; no API changes visible to application code - **Performance**: Slight improvement due to lock-free atomic operations vs potential future mutex protection - **Scope**: Affects all child process lifecycle management (creation, exit, reparenting, waiting) --- **Stats**: 7 files changed, 33 insertions(+), 19 deletions(-) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
