[PATCH] nptl: Optimize trylock for high cache contention workloads (BZ #33704)

H.J. Lu hjl.tools@gmail.com
Thu Dec 11 00:42:33 GMT 2025


On Thu, Dec 11, 2025 at 6:09 AM Sunil K Pandey <sunil.k.pandey@intel.com> wrote:
>
> 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.
> ---
>  nptl/pthread_mutex_trylock.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c
> index 94621cc254..e3f83311c1 100644
> --- a/nptl/pthread_mutex_trylock.c
> +++ b/nptl/pthread_mutex_trylock.c
> @@ -60,7 +60,10 @@ ___pthread_mutex_trylock (pthread_mutex_t *mutex)
>      case PTHREAD_MUTEX_TIMED_NP:
>      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.  */
> --
> 2.51.0
>

Please add a bench test.

-- 
H.J.


More information about the Libc-alpha mailing list