public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Noah Goldstein <goldstein.w.n@gmail.com>
To: "H.J. Lu" <hjl.tools@gmail.com>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: Use adaptive mutex with std::mutex
Date: Wed, 19 Apr 2023 15:16:47 -0500	[thread overview]
Message-ID: <CAFUsyf+sbpyS9cP8f8bHk0onX3pp5R2OBBMiNJLqBr3-MOUiNw@mail.gmail.com> (raw)
In-Reply-To: <CAMe9rOr_xfcsrx4jyMZDKiZf_OrA8J5v177+1zSGBSxURG5iSg@mail.gmail.com>

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.

  reply	other threads:[~2023-04-19 20:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-18 21:37 H.J. Lu
2023-04-19 20:16 ` Noah Goldstein [this message]
2023-04-29 15:34   ` Cristian Rodríguez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFUsyf+sbpyS9cP8f8bHk0onX3pp5R2OBBMiNJLqBr3-MOUiNw@mail.gmail.com \
    --to=goldstein.w.n@gmail.com \
    --cc=hjl.tools@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).