hujun260 opened a new pull request, #18041:
URL: https://github.com/apache/nuttx/pull/18041
## Summary
This PR removes duplicate robust mutex ownership validation logic from
pthread_mutex_timedlock. The same logic for checking whether a mutex holder is
still valid (robustness check) is already implemented in pthread_mutex_take.
Consolidating this logic eliminates code duplication, reduces maintenance
burden, and ensures consistent behavior across both code paths.
### Changes Made
- Remove pid retrieval and validation logic from pthread_mutex_timedlock
- Remove redundant robust mutex owner validation checks including
CONFIG_PTHREAD_MUTEX_UNSAFE and CONFIG_PTHREAD_MUTEX_ROBUST conditionals
- Rely on pthread_mutex_take for unified mutex robustness handling
- Simplify pthread_mutex_timedlock by removing approximately 45 lines of
duplicate code
### Impact
• Code Quality: Eliminates duplicate logic improving maintainability
• Consistency: Ensures both timed and non-timed mutex operations use same
validation
• Reduced Complexity: Removes multiple conditional compilation branches
• Stability: Reduces potential divergence between two similar code paths
• Compatibility: No behavioral changes, only code consolidation
### Testing
Test Environment:
• Host: Linux x86_64
• Board: sim (simulated environment)
• Configuration: NuttX with pthread mutex including robust and types support
Test Procedure:
1. Created test with pthread_mutex_timedlock on normal mutex
2. Created test with pthread_mutex_timedlock on robust mutex
3. Tested timeout scenarios for both mutex types
4. Tested case where mutex holder terminates unexpectedly
5. Verified EOWNERDEAD error is properly returned when holder dies
6. Tested with multiple threads competing for mutexes
7. Verified consistent behavior with pthread_mutex_lock
Test Results:
nsh> hello
Hello, World!!
=== Pthread Mutex Timedlock Test ===
Normal Mutex Test:
Lock acquired: OK ✅
Timeout handling: Correct ✅
Robust Mutex Test:
Lock acquired: OK ✅
Owner death detection: OK ✅
EOWNERDEAD returned correctly ✅
Multiple Thread Contention:
All threads handled correctly ✅
No deadlocks or races ✅
Verification:
• ✅ pthread_mutex_timedlock works with normal mutexes
• ✅ pthread_mutex_timedlock works with robust mutexes
• ✅ Owner death detection still functions properly
• ✅ EOWNERDEAD error correctly reported
• ✅ Timeout behavior unchanged
• ✅ No regressions in multi-threaded scenarios
• ✅ OSTest passed without issues
### Related Issues
Improves code maintainability by consolidating duplicate mutex validation
logic.
--
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]