hujun260 opened a new pull request, #18201:
URL: https://github.com/apache/nuttx/pull/18201
## Summary
This PR includes a series of code quality improvements and MISRA HIS
compliance fixes in the semaphore implementation. The changes eliminate goto
statements, remove dead code, consolidate duplicate logic, and optimize control
flow while maintaining complete functional equivalence and backward
compatibility.
**Changes:**
- Eliminate goto statements for MISRA HIS compliance
- Remove dead code from CONFIG_PRIORITY_PROTECT handling
- Consolidate macro definitions to usage locations
- Optimize sem_trywait() implementation
- Reduce code duplication across semaphore functions
- Maintain all functional behavior
## Motivation
**Code Quality Issues:**
1. Multiple goto statements violate MISRA HIS standards
2. Dead code in CONFIG_PRIORITY_PROTECT conditions
3. Duplicate macro definitions across files
4. Redundant logic in sem_trywait() implementation
**Issues:**
- Reduced maintainability and verifiability
- Complex control flow
- Code duplication risks
- MISRA HIS non-compliance
**Solution:**
- Replace goto with structured control flow
- Consolidate macro definitions
- Remove unused code paths
- Optimize redundant logic
## Impact
| Aspect | Status |
|--------|--------|
| **Functionality** | No change; identical behavior |
| **API** | 100% backward compatible |
| **Performance** | Maintained/Improved |
| **Maintainability** | Significantly improved |
| **Compliance** | MISRA HIS compliant |
| **Code Quality** | Enhanced |
## Commits Included
### 1. Eliminate goto statements (sem_trywait.c)
- Refactor goto-based flow into structured conditionals
- Maintain identical logic and behavior
- Improve code clarity
### 2. Remove dead code (CONFIG_PRIORITY_PROTECT)
- Move macro definitions to usage sites
- Remove duplicate definitions in semaphore.h
- Consolidate code in 3 files (sem_wait, sem_trywait, sem_post)
### 3. Optimize sem_trywait()
- Remove redundant logic
- Simplify control flow
- Reduce cyclomatic complexity
## Testing
| Test | Result |
|------|--------|
| Functional Tests | ✅ PASS |
| Semaphore Operations | ✅ PASS |
| Priority Protection | ✅ PASS |
| Thread Safety | ✅ PASS |
| Code Quality Analysis | ✅ PASS |
| Regression Suite | ✅ PASS |
**Metrics:**
- Code quality: Improved
- Cyclomatic complexity: Reduced
- Goto statements: Eliminated
- Dead code: Removed
- Code duplication: Reduced
**Build:** ARM GCC 10.x, 0 warnings, Static Analysis PASS
## Code Changes
**Files Modified:**
- `sched/semaphore/sem_trywait.c` - Remove goto, remove dead code, optimize
logic
- `sched/semaphore/sem_wait.c` - Add CONFIG_PRIORITY_PROTECT macro
- `sched/semaphore/sem_post.c` - Add CONFIG_PRIORITY_PROTECT macro
- `sched/semaphore/semaphore.h` - Remove duplicate macro definitions
**Key patterns:**
**Before (goto-based):**
```c
if (condition) {
goto error;
}
// ... code ...
return result;
error:
return -EINVAL;
--
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]