hujun260 opened a new pull request, #18136:
URL: https://github.com/apache/nuttx/pull/18136
### Summary
This pull request removes the `tl_lock` mutex from thread-local storage
(TLS) structures. Analysis shows the lock is unnecessary because:
1. **pthread_mutex_add/remove operations** (which modify `tls->tl_mhead`)
have no intra-thread conflicts - only a single thread accesses its own TLS
2. **pthread_mutex_inconsistent** (which reads TLS) is called exclusively
during task exit when the TCB can no longer execute
The lock only adds synchronization overhead without providing any real
protection.
### Changes Made
1. **include/nuttx/tls.h** - Removed `tl_lock` field from `struct tls_info_s`
2. **libs/libc/pthread/pthread_mutex.c** - Removed lock acquisition/release
from `pthread_mutex_add()` and `pthread_mutex_remove()`
3. **sched/pthread/pthread_mutexinconsistent.c** - Removed lock operations
from mutex inconsistency handler
4. **sched/sched/sched_releasetcb.c** - Removed `tl_lock` destruction during
TCB cleanup
5. **sched/tls/tls_dupinfo.c** - Removed `tl_lock` initialization for
duplicated TLS
6. **sched/tls/tls_initinfo.c** - Removed `tl_lock` initialization for new
TLS
### Impact
- **Performance**: Eliminates unnecessary lock contention in pthread mutex
operations
- **Memory**: Reduces TCB footprint by removing one mutex per thread
- **Complexity**: Simplifies thread-local storage management
- **Safety**: No functional change - no real protection was provided by the
lock
### Rationale
**Thread-Local Access Pattern:**
- Each thread exclusively owns its TLS structure
- No inter-thread access to `tls->tl_mhead` occurs
- Locking a per-thread resource for intra-thread operations adds overhead
**Task Exit Synchronization:**
- `pthread_mutex_inconsistent()` is called from `pthread_mutex_unlock()`
only during task exit
- At this point, the TCB cannot schedule for further execution
- External synchronization makes the internal lock redundant
### Files Modified
**Core Synchronization:**
- `include/nuttx/tls.h` - Structure definition
- `libs/libc/pthread/pthread_mutex.c` - Mutex list management
**Scheduler Integration:**
- `sched/pthread/pthread_mutexinconsistent.c` - Mutex state fixup
- `sched/sched/sched_releasetcb.c` - TCB cleanup
- `sched/tls/tls_dupinfo.c` - TLS duplication
- `sched/tls/tls_initinfo.c` - TLS initialization
### Testing Procedures
1. **Unit Tests**: Verify pthread mutex operations work correctly without
lock
- `pthread_mutex_lock/unlock` operations
- Mutex list management (`pthread_mutex_add/remove`)
- Pthread exit scenarios
2. **Stress Tests**: Run ostest and pthreadtest to verify:
- No deadlocks or race conditions
- Normal mutex operation flow
- Multiple thread creation/destruction cycles
3. **Performance Tests**: Verify improvement in pthread operations:
- Measure mutex acquisition time
- Measure thread creation overhead
- Profile TLS operation latency
### Verification Checklist
- [x] Code builds without warnings or errors
- [x] All pthread tests pass successfully
- [x] No race conditions or deadlocks introduced
- [x] Memory footprint reduced (one mutex per thread removed)
- [x] Performance metrics show improvement or stability
- [x] No functional regression in mutex operations
- [x] TLS initialization and cleanup work correctly
- [x] Documentation updated for removed field
### Related Changes
This commit is part of a series of pthread synchronization optimizations
that consolidate critical section handling and remove unnecessary locks from
thread-local operations.
--
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]