spinlock: add and optimize recursive spinlock interface for multi-core support #18049
+81
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements comprehensive recursive spinlock (rspinlock) support with platform-specific optimizations. The changes include:
Add Recursive Spinlock Interface: Implements new rspinlock functions to support nested spinlock acquisitions by the same CPU, preventing deadlocks in scenarios requiring recursive locking.
Optimize for Single-Core: Provides CONFIG_SPINLOCK conditional compilation to optimize rspinlock for systems without spinlock support, using simple macro implementations instead of complex lock logic.
Together, these changes enable robust recursive spinlock patterns across all platform configurations.
Changes Made
Commit 1 - Add Recursive Spinlock Interface (spinlock.h):
Commit 2 - Optimize Recursive Spinlock for Single-Core (spinlock.h):
Impact
• Functionality: Enables recursive spinlock patterns for same-CPU nested locking scenarios
• Performance: Optimized code paths for systems without spinlock support
• Code Quality: Separates concerns between lock management and IRQ handling
• Flexibility: Better support for systems with and without spinlock support
• Compatibility: Maintains backward compatibility with existing rspin_lock_irqsave/irqrestore APIs
Testing
Test Environment:
• Host: Linux x86_64
• Board: sim (simulated multi-core environment)
• Configuration: NuttX with and without CONFIG_SPINLOCK
Test Procedure for Recursive Spinlock Interface:
Test Procedure for Single-Core Optimization:
Test Results:
Recursive Spinlock Interface:
Break/Restore Operations:
Spinlock Configuration Testing:
Cross-CPU Protection:
Stress Testing:
Verification Checklist:
• ✅ rspin_lock() correctly handles recursive acquisition
• ✅ Lock count properly managed for nested calls
• ✅ rspin_unlock() returns correct values
• ✅ Owner identification prevents unauthorized operations
• ✅ IRQ state properly saved and restored
• ✅ Breaklock/restorelock functionality working correctly
• ✅ Macro implementations for non-spinlock configs functioning
• ✅ No deadlocks in recursive scenarios
• ✅ Backward compatibility maintained
• ✅ No regressions in existing spinlock operations
• ✅ OSTest passed without issues
Related Issues
Enables robust recursive spinlock support for multi-core NuttX systems with platform-specific optimizations.