hujun260 opened a new pull request, #18210:
URL: https://github.com/apache/nuttx/pull/18210
## Summary
This PR contains two related improvements to the reader-writer semaphore
implementation:
1. Optimize `up_read()` and `up_write()` APIs to reduce unnecessary context
switches
2. Refactor control flow in `up_read()` to eliminate goto statements and
achieve
MISRA HIS compliance
## Motivation and Problem
### Part 1: Performance Optimization
The original implementation had redundant context switch operations in the
reader-writer semaphore release paths. The `up_wait()` function was being
called
multiple times in different code paths, creating unnecessary wake-ups and
context
switches even when no waiters were present, impacting system performance.
### Part 2: MISRA HIS Compliance
The `up_read()` function used goto-based control flow which violates MISRA
HIS
coding standards for safety-critical systems. Structured if-else blocks
improve
code clarity, verifiability, and compliance with automotive safety standards.
## Changes
### Commit 1: Performance Optimization
- Consolidate `up_wait()` calls to reduce unnecessary context switches
- Move `up_wait()` only to paths where waiters actually exist
- Simplify reader count decrement logic with combined condition check
### Commit 2: MISRA HIS Compliance
- Replace goto statements with structured if-else blocks in `up_read()`
- Maintain identical functional behavior with improved code structure
- Single exit point after all conditional paths
## Impact
- **Performance**: Reduced context switch overhead in high-contention
scenarios
- **Compliance**: Achieves MISRA HIS compliance for goto elimination metrics
- **Code Quality**: Improved readability with structured control flow
- **Verifiability**: Single-exit paths improve static analysis
- **Backward Compatibility**: No functional changes; identical runtime
semantics
- **Latency**: Reduced interrupt disable time from fewer unnecessary wake-ups
## Verification
- [x] Code compiles without warnings on ARM GCC 10.x
- [x] Verified on QEMU ARMv7-A simulator with multimedia profile
- [x] Reader-writer semaphore operations verified:
- Single reader access patterns
- Single writer access patterns
- Mixed reader-writer contention scenarios
- Waiter notification behavior verified
- No unnecessary context switches on optimized paths
- [x] Static analysis shows improved compliance metrics
## Testing
Tested with:
- ARM GCC 10.x compiler
- QEMU ARMv7-A simulation
- Reader-writer semaphore scenarios:
- Multiple readers with no writers
- Multiple writers (exclusive access)
- Mixed reader-writer access patterns
- Waiter wake-up correctness
- Lock holder tracking
- Context switch reduction verification
## Related Issues
Addresses performance optimization for high-contention scenarios and MISRA
HIS
compliance for goto elimination.
Reference: https://github.com/apache/nuttx/pull/17909
## Files Changed
- `sched/semaphore/sem_rw.c` (23 lines total: 10 insertions, 13 deletions
across 2 commits)
- Commit 1: 8 lines (3 insertions, 5 deletions) - Performance optimization
- Commit 2: 15 lines (7 insertions, 8 deletions) - HIS_GOTO compliance
--
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]