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

Wilco Dijkstra Wilco.Dijkstra@arm.com
Wed Dec 17 12:27:06 GMT 2025


Hi Florian,

>> as lll_trylock() is a macro, could the relaxed load be inserted into
>> it, providing the improvement to other user of lll_trylock() ?
>
> Direct CAS for an uncontended lock is better because it avoids a shared
> → exclusive cacheline transition, so it's less clear if the change is
> beneficial at the lll_trylock layer.  This is not a concern for
> pthread_mutex_trylock because it reads the type member first, which is
> with some likelihood on the same cacheline (but not guaranteed to be).

Yes, the lll_trylock is a low level interface, so best kept like that - we could
add new variants that add the extra test, but it would have to be explicit.

The gain comes from not writing and taking ownership from the thread
that owns the lock (so that it then has to fight to regain ownership just to
release it).

Also note that the pthread locks will always read from the mutex first via
PTHREAD_MUTEX_TYPE. You'd need to split a mutex over 2 cachelines to
avoid this.

Cheers,
Wilco


More information about the Libc-alpha mailing list