hujun260 opened a new pull request, #18148:
URL: https://github.com/apache/nuttx/pull/18148

   ## Summary
   
   This pull request extends the recursive spinlock (rspinlock_t) functionality 
with two key improvements:
   
   1. **Try-lock variants** for non-blocking spinlock acquisition with optional 
IRQ protection and preemption control
   2. **Count/owner tracking** even when CONFIG_SPINLOCK is disabled, providing 
unified semantics across all configurations
   
   These changes enable fine-grained synchronization patterns in nested 
contexts and improve code portability across different platform configurations.
   
   ## Changes
   
   **Commit 1: Add rspin_trylock variants**
   - `rspin_trylock()`: Non-blocking try-lock with recursive count support
   - `rspin_trylock_irqsave()`: Try-lock with IRQ save/restore
   - `rspin_trylock_irqsave_nopreempt()`: Try-lock with IRQ and scheduler 
locking
   - Supports UINT16_MAX maximum nesting depth
   - Returns true on success, false if lock is already held by another CPU
   
   **Commit 2: Add rspinlock tracking when CONFIG_SPINLOCK=n**
   - `rspin_lock_irqsave()`: Implement count/owner tracking via inline function
   - `rspin_trylock_irqsave()`: Implement count/owner tracking for try-lock
   - `rspin_unlock_irqrestore()`: Implement count/owner decrement with IRQ 
restore
   - `rspin_breaklock()`/`rspin_restorelock()`: Implement lock break/restore 
for nested contexts
   - Maintains consistent interface regardless of CONFIG_SPINLOCK setting
   
   **Statistics**:
   - Commit 1: 70 lines added (new try-lock interface)
   - Commit 2: 36 insertions(+), 5 deletions(-) (non-SMP tracking)
   
   ## Impact
   
   - **Non-blocking operations**: Enable try-lock patterns for 
timeout-sensitive code
   - **Configuration consistency**: Same interface with or without 
CONFIG_SPINLOCK
   - **Portability**: Code written for rspinlock works identically on SMP and 
non-SMP systems
   - **Nesting support**: Full UINT16_MAX nesting depth for recursive lock 
patterns
   - **Performance**: Atomic CAS operations for efficient try-lock in SMP mode
   - **Scheduler safety**: Optional preemption control via sched_lock() 
integration
   
   ## Technical Details
   
   **Try-lock semantics:**
   - Recursive: Allow same CPU to re-acquire without deadlock
   - Non-blocking: Return immediately with success/failure status
   - IRQ-safe: Optional IRQ save/restore wrapper
   - Preemption control: Optional scheduler lock during critical section
   
   **Non-SMP implementation:**
   - When CONFIG_SPINLOCK disabled: Use count/owner fields for recursive 
tracking
   - Maintains same interface as SMP version for code portability
   - Count tracks recursion depth, owner tracks CPU that acquired lock
   - Enables code to work on all platforms without CONFIG_SPINLOCK detection
   


-- 
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]

Reply via email to