hujun260 opened a new pull request, #17956:
URL: https://github.com/apache/nuttx/pull/17956
## Summary
This PR optimizes the hostname operations by replacing global critical
section
locks with a lightweight spinlock. The hostname is a shared system property
that
needs synchronization, but using global critical sections causes unnecessary
interrupt latency throughout the system. A dedicated spinlock provides
better
performance and scalability.
## Changes
The optimization involves synchronization changes in two files:
1. **libs/libc/unistd/lib_gethostname.c** - Add spinlock for hostname
reading:
- Add `#include <nuttx/spinlock.h>` header
- Define static spinlock: `spinlock_t g_hostname_lock = SP_UNLOCKED`
- Replace `enter_critical_section()` with
`spin_lock_irqsave(&g_hostname_lock)`
- Replace `leave_critical_section()` with
`spin_unlock_irqrestore(&g_hostname_lock, flags)`
- Protects hostname reading during string copy
2. **libs/libc/unistd/lib_sethostname.c** - Add spinlock for hostname
writing:
- Add `#include <nuttx/spinlock.h>` header
- Declare external spinlock: `extern spinlock_t g_hostname_lock`
- Replace `enter_critical_section()` with
`spin_lock_irqsave(&g_hostname_lock)`
- Replace `leave_critical_section()` with
`spin_unlock_irqrestore(&g_hostname_lock, flags)`
- Protects hostname writing during string copy
## Benefits
- **Performance**: Reduces interrupt latency by using spinlock instead of
global critical section
- **Scalability**: Per-subsystem spinlock allows other code to proceed
without delay
- **Correctness**: Maintains proper synchronization for the shared hostname
resource
- **Efficiency**: Lightweight spinlock is appropriate for short critical
sections
## Technical Details
**Why spinlock instead of critical section?**
- Critical sections disable interrupts globally, blocking all interrupt
handlers
- Spinlock only protects access to the specific shared resource
- Reduces latency for unrelated operations
**Synchronization scope:**
- `gethostname()`: Protects reading g_hostname during copy to user buffer
- `sethostname()`: Protects writing g_hostname from user-provided buffer
## Testing
Tested on:
- **Platform**: NuttX with multi-core support
- **Configuration**: User-mode and kernel-mode builds
- **Test scenarios**:
- Concurrent reads from multiple tasks
- Concurrent writes with various hostname lengths
- Signal handling during hostname operations
- Network stack hostname access patterns
- Interrupt latency measurements
- **Result**:
- Hostname operations work correctly with spinlock
- Reduced interrupt latency vs. critical sections
- No data corruption or race conditions
- Improved system responsiveness
## Impact
- **Performance**: Improved system-wide interrupt latency
- **Stability**: Maintains correct hostname synchronization
- **Compatibility**: No API changes; internal optimization only
- **Code Quality**: Cleaner synchronization model
--
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]