Skip to content

Conversation

@hujun260
Copy link
Contributor

Summary

Consolidate multiple return statements in the down_write_trylock() function
into a single exit point to improve code quality and comply with MISRA HIS
coding standards for safety-critical systems.

Motivation and Problem

The original implementation had multiple return points (early return for failure
and return at the end for success), which violates MISRA HIS metric rules for
safety-critical code. This increases cyclomatic complexity and makes code
verification and testing more difficult. Consolidating returns to a single exit
point improves maintainability and compliance with automotive safety standards.

Changes

  • Introduce a result variable (ret) initialized to 1 (success)
  • Replace early error return with assignment to ret = 0
  • Wrap the success path in an else block to maintain control flow clarity
  • Perform single return at function end with the result variable

Impact

  • Code Quality: Reduced cyclomatic complexity
  • Compliance: Achieves MISRA HIS compliance for return statement metrics
  • Verifiability: Single exit point improves static analysis and code verification
  • Backward Compatibility: No functional changes; identical runtime behavior
  • Performance: No performance impact; compiler optimizations identical

Verification

  • Code compiles without warnings on ARM GCC 10.x
  • Verified on QEMU ARMv7-A simulator with multimedia profile
  • Reader-writer semaphore trylock behavior unchanged
  • Success path (return 1) verified
  • Failure path (return 0) verified
  • Mutex protection and holder tracking verified
  • Static analysis shows improved complexity metrics

Testing

Tested with:

  • ARM GCC 10.x compiler
  • QEMU ARMv7-A simulation
  • Reader-writer semaphore operations:
    • Write lock acquisition attempts
    • Concurrent reader detection
    • Recursive writer holder detection
    • Lock state transitions

Files Changed

  • sched/semaphore/sem_rw.c (17 lines: 10 insertions, 7 deletions)

This change consolidates multiple return statements in down_write_trylock()
into a single exit point to reduce cyclomatic complexity and comply with MISRA
HIS coding standards for safety-critical embedded systems.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
@github-actions github-actions bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants