hujun260 opened a new pull request, #18223:
URL: https://github.com/apache/nuttx/pull/18223
## Summary
Refactor the scheduler setting function to improve code quality by
eliminating goto statements and consolidating multiple return paths. The
implementation extracts sporadic scheduling logic into a dedicated function and
restructures parameter validation, resulting in improved code maintainability
and MISRA HIS compliance.
## Motivation and Problem
The original nxsched_set_scheduler() function used goto statements for error
handling and had multiple return paths scattered throughout the code, violating
MISRA HIS standards that require structured control flow and single exit point
patterns.
### Safety Standard Compliance
- **MISRA HIS Standard**: Eliminates goto statements; enforces
single-exit-point pattern
- **Coverity Defect**: HIS_metric_violation (RETURN) - Multiple return paths
and goto detected
- **Code Restructuring**: Moves early validations and error returns into
structured code blocks
- **Benefit**: Enhanced code verifiability, reduced control flow complexity,
improved maintainability
## Changes Made
1. **Function Extraction**: Extract sporadic scheduling logic into separate
`process_sporadic()` function
- Reduces nesting complexity in main scheduler function
- Improves code readability and reusability
2. **Parameter Validation Restructuring**: Move early policy and priority
validation into conditional block
- Consolidated validation checks into unified decision point
- Reduced early return statements
3. **Goto Elimination**: Replace error handling goto statements with
structured if-else blocks
- Removed `errout_with_irq:` label and associated goto jumps
- Maintains same functional behavior with cleaner control flow
4. **Single Exit Point**: Consolidate all code paths to use single return
statement
- All error conditions set return value
- Uniform cleanup and resource release
5. **Code Reorganization**:
- Move sporadic scheduling code to dedicated helper function
- Restructure policy switch statement within validation block
- Improve indentation and code clarity
### File Statistics
- **File Modified**: `sched/sched/sched_setscheduler.c`
- **Lines Changed**: 312 (154 insertions, 158 deletions)
- **Type**: Major code restructuring for compliance improvement
## Impact Analysis
✅ **Backward Compatibility**: MAINTAINED - Function behavior unchanged
✅ **Code Quality**: SIGNIFICANTLY IMPROVED - Goto elimination, single exit
point
✅ **MISRA Compliance**: RESOLVED - HIS_metric_violation defect fixed
✅ **Readability**: ENHANCED - Clearer code structure and logic flow
✅ **Maintainability**: IMPROVED - Extracted sporadic function, reduced
complexity
✅ **Performance**: NO IMPACT - Same scheduling logic, improved structure
## Verification Checklist
- [x] Compilation successful with all warning flags enabled
- [x] All scheduling policies correctly handled (SCHED_FIFO, SCHED_RR,
SCHED_SPORADIC, SCHED_OTHER)
- [x] Parameter validation preserved and restructured correctly
- [x] Sporadic scheduling extracted to separate function without behavioral
changes
- [x] No goto statements in refactored code
- [x] Single exit point pattern implemented and verified
- [x] Error return codes preserved (-EINVAL, -ESRCH)
- [x] Coverity HIS_metric_violation (RETURN) defect resolved
- [x] Code review completed - all logic paths validated
- [x] Integration testing completed
## Testing Scenarios
1. **Policy Validation**: Test invalid policy values properly rejected
2. **Priority Validation**: Test boundary conditions (SCHED_PRIORITY_MIN,
SCHED_PRIORITY_MAX)
3. **Task ID Validation**: Test invalid task IDs return -ESRCH
4. **FIFO Scheduling**: Verify SCHED_FIFO policy correctly applied
5. **Round-Robin Scheduling**: Test SCHED_RR with CONFIG_RR_INTERVAL settings
6. **Sporadic Scheduling**: Validate sporadic scheduling parameters and
initialization
7. **Sporadic Parameter Extraction**: Test process_sporadic() function
independently
8. **Context Switching**: Verify critical section protection during policy
change
9. **Priority Update**: Confirm nxsched_reprioritize() called after policy
change
10. **Edge Cases**: Test with concurrent policy changes across multiple tasks
## Technical Notes
- Sporadic scheduling parameter validation moved to `process_sporadic()`
function
- All validation paths consolidated into single nested conditional structure
- Critical section (enter/leave_critical_section) usage preserved and
maintained
- Task lookup and locking sequence unchanged for consistency
- Error handling now uses structured if-else instead of goto labels
- All configuration-dependent code (#ifdef) preserved in switch statement
## Related Issues
- **Category**: Code Quality & Safety Standard Compliance
- **Standard**: MISRA HIS - Single Exit Point Pattern & Goto Elimination
- **Defect**: Coverity HIS_metric_violation (RETURN)
- **Subsystem**: Kernel scheduler - policy management (sched/sched/)
## Build Information
- **Compiler**: ARM GCC 10.x (primary test environment)
- **Architectures**: ARMv7-A, ARMv7-R, x86_64
- **Target**: NuttX kernel - scheduler subsystem
- **Build Flags**: `-Wall -Wextra -Werror`
- **Features**: CONFIG_SCHED_SPORADIC, CONFIG_RR_INTERVAL configurable
## Files Changed
--
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]