Use adaptive mutex with std::mutex

Noah Goldstein goldstein.w.n@gmail.com
Wed Apr 19 20:16:47 GMT 2023


On Tue, Apr 18, 2023 at 4:37 PM H.J. Lu via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> Hi,
>
> Adaptive mutex works much better than normal mutex on machines with many cores.
> However PTHREAD_MUTEX_ADAPTIVE_NP is a GNU extension and not supported
> by std::mutex.  Is there a way to use adaptive mutex with std::mutex?  Glibc has
>
> #define PTHREAD_MUTEX_INITIALIZER \
>  { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_TIMED_NP) } }
> #ifdef __USE_GNU
> # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
>  { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_RECURSIVE_NP) } }
> # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
>  { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ERRORCHECK_NP) } }
> # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
>  { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ADAPTIVE_NP) } }
> #endif
>
> Can we add a macro to conditonally define PTHREAD_MUTEX_INITIALIZER with
> PTHREAD_MUTEX_ADAPTIVE_NP?

I think it would make sense (if its feasible) to make ADAPTIVE_NP the default
std::mutex.

They have the same low/no contention case: Just a single CAS.

But Adaptive NP covers the most usage cases the best. Rather than just
immediately
entering the futex (how TIMED_NP which is default handle contention),
it attempts
to spin <= 100 times which can dramatically improve cases where the critical
section is small without affecting performance of longer waits.

i.e
Futex = O(10^4) cycles
Spin = O(10^2) cycles

TIMED_NP Short Critical Section uses Futex: O(10^4) Cycles
TIMED_NP Long Critical Section uses futex: O(10^4) Cycles


ADAPTIVE_NP Short Critical Section uses spin: O(10^2) Cycles
ADAPTIVE_NP Long Critical Section uses spin + futex: O(10^2) + O(10^4)
= O(10^4) Cycles


> --
> H.J.


More information about the Libc-alpha mailing list