hujun260 opened a new pull request, #17957: URL: https://github.com/apache/nuttx/pull/17957
## Summary Replace global critical section (`enter/leave_critical_section()`) with group-level mutex protection (`nxrmutex_lock/unlock()`) in environment variable operations. This optimization reduces interrupt latency and improves system responsiveness on multi-core platforms by using fine-grained locking instead of global interrupt disabling. ## Changes - **sched/environ/env_dup.c**: Replace `enter_critical_section()` with `nxrmutex_lock(&group->tg_mutex)` and `leave_critical_section()` with `nxrmutex_unlock(&group->tg_mutex)`; remove unused `irqstate_t flags` variable. - **sched/environ/env_getenv.c**: Replace critical section with mutex lock, restructure logic to minimize lock scope by checking group validity before acquiring lock, and remove early critical section entry. - **sched/environ/env_setenv.c**: Replace critical section with mutex lock around variable lookup and modification operations; remove `irqstate_t flags` variable. - **sched/environ/env_unsetenv.c**: Replace critical section with mutex lock for environment variable removal; remove unused `irqstate_t flags` variable. ## Benefits & Technical Details - **Reduced interrupt latency**: Mutex-based synchronization allows interrupts to remain enabled, improving real-time responsiveness for other critical tasks. - **SMP scalability**: Fine-grained per-group locking enables concurrent environment access by different task groups without global interrupt disabling. - **Code clarity**: Explicit mutex semantics clarify that protection is at group-level, not system-wide. - **Lock scope optimization**: In env_getenv, lock is acquired only after group existence check, minimizing critical section duration. ## Testing - Verified environment variable get/set/unset operations work correctly in single-threaded and multi-threaded contexts - Confirmed no race conditions when multiple threads access the same group's environment simultaneously - Validated interrupt handling with reduced latency - system responds faster to external interrupts - Tested on SMP systems to ensure per-group mutual exclusion without global interrupt disabling ## Impact - **Performance**: Reduced interrupt latency and improved context switching speed on SMP systems - **Real-time**: Better support for time-sensitive operations by keeping interrupts enabled during environment operations - **Compatibility**: No API changes, fully backward compatible - **Scope**: Affects all getenv/setenv/unsetenv operations and environment duplication in task creation -- 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]
