diff options
| author | Sunil K Pandey <sunil.k.pandey@intel.com> | 2025-12-09 08:57:44 -0800 |
|---|---|---|
| committer | Sunil K Pandey <sunil.k.pandey@intel.com> | 2025-12-18 11:06:59 -0800 |
| commit | 2ca35051530486b0695b007814ddf3db73d603a9 (patch) | |
| tree | da2fb674e08b5464188b063ec2d938a910468b40 | |
| parent | 92d25389c255b0da9b56bc05694f0702cd22921a (diff) | |
| download | glibc-release/2.28/master.zip glibc-release/2.28/master.tar.gz glibc-release/2.28/master.tar.bz2 | |
nptl: Optimize trylock for high cache contention workloads (BZ #33704)release/2.28/master
Check lock availability before acquisition to reduce cache line
bouncing. Significantly improves trylock throughput on multi-core
systems under heavy contention.
Tested on x86_64.
Fixes BZ #33704.
Co-authored-by: Alex M Wells <alex.m.wells@intel.com>
Reviewed-by: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
(cherry picked from commit 63716823dbad9482e09972907ae98e9cb00f9b86)
| -rw-r--r-- | nptl/pthread_mutex_trylock.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c index 8e01113..9cdb325 100644 --- a/nptl/pthread_mutex_trylock.c +++ b/nptl/pthread_mutex_trylock.c @@ -56,7 +56,8 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex) return 0; } - if (lll_trylock (mutex->__data.__lock) == 0) + if (atomic_load_relaxed (&(mutex->__data.__lock)) == 0 + && lll_trylock (mutex->__data.__lock) == 0) { /* Record the ownership. */ mutex->__data.__owner = id; @@ -79,7 +80,10 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex) /*FALL THROUGH*/ case PTHREAD_MUTEX_ADAPTIVE_NP: case PTHREAD_MUTEX_ERRORCHECK_NP: - if (lll_trylock (mutex->__data.__lock) != 0) + /* Mutex type is already loaded, lock check overhead should + be minimal. */ + if (atomic_load_relaxed (&(mutex->__data.__lock)) != 0 + || lll_trylock (mutex->__data.__lock) != 0) break; /* Record the ownership. */ |
