libs/libc/pthread: Optimize mutex robustness configuration and remove unnecessary tl_lock#18136
Merged
xiaoxiang781216 merged 2 commits intoapache:masterfrom Jan 26, 2026
Merged
Conversation
xiaoxiang781216
previously approved these changes
Jan 24, 2026
f197911 to
c903ee4
Compare
xiaoxiang781216
approved these changes
Jan 25, 2026
2c85b81 to
f411224
Compare
xiaoxiang781216
previously approved these changes
Jan 26, 2026
anchao
previously requested changes
Jan 26, 2026
Contributor
|
@hujun260 please fix the conflict |
This lock is currently used in three places, mainly to protect tls->tl_mhead. Among them, pthread_mutex_add and pthread_mutex_remove involve writing to tls->tl_mhead, and there is certainly no conflict within the same thread. As for pthread_mutex_inconsistent, it involves reading. Currently, it can only be called when the TCB task corresponding to this tls exits, and the TCB corresponding to the tls can no longer continue to run. It seems that adding the lock serves no real purpose. Signed-off-by: hujun5 <hujun5@xiaomi.com>
Change the default pthread mutex robustness from ROBUST to UNSAFE when CONFIG_PTHREAD_MUTEX_BOTH is enabled. This reflects the practical default and allows removal of tracking code for non-robust mutexes, improving performance and reducing memory overhead. Signed-off-by: hujun5 <hujun5@xiaomi.com>
f411224 to
45e622c
Compare
Contributor
Author
ok |
jerpelea
approved these changes
Jan 26, 2026
xiaoxiang781216
approved these changes
Jan 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 pull request optimizes NuttX pthread mutex implementation by:
These changes improve performance, reduce memory overhead, and simplify synchronization logic without compromising safety.
Changes Made
Commit 1: Remove tl_lock
tl_lockfield fromstruct tls_info_spthread_mutex_add()andpthread_mutex_remove()pthread_mutex_inconsistent()Commit 2: Change Default Mutex Robustness
PTHREAD_MUTEX_DEFAULT_ROBUSTtoPTHREAD_MUTEX_DEFAULT_UNSAFEpthread_mutex_add/removefunctions to optimize hot pathsImpact
Performance:
Memory:
Compatibility:
Files Modified
Core Synchronization (Commit 1):
include/nuttx/tls.h- Remove tl_lock fieldlibs/libc/pthread/pthread_mutex.c- Remove lock operationssched/pthread/pthread_mutexinconsistent.c- Remove lock operationssched/sched/sched_releasetcb.c- Remove lock destructionsched/tls/tls_dupinfo.c- Remove lock initializationsched/tls/tls_initinfo.c- Remove lock initializationConfiguration (Commit 2):
libs/libc/pthread/pthread_mutex.c- Inline functions, conditional trackingsched/Kconfig- Change default robustness modeTechnical Justification
Why is tl_lock unnecessary?
tls->tl_mheadhave no intra-thread conflictspthread_mutex_inconsistentcalled only during task exitWhy change default to UNSAFE?
Testing Procedures
Functionality Tests:
ostestandpthreadtestto verify normal operationPerformance Tests:
Stress Tests:
Configuration Tests:
CONFIG_PTHREAD_MUTEX_ROBUSTonlyCONFIG_PTHREAD_MUTEX_UNSAFEonlyCONFIG_PTHREAD_MUTEX_BOTH(default=UNSAFE)Verification Checklist
Related Changes
This optimization series improves pthread synchronization by: