public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Use adaptive mutex with std::mutex
@ 2023-04-18 21:37 H.J. Lu
  2023-04-19 20:16 ` Noah Goldstein
  0 siblings, 1 reply; 3+ messages in thread
From: H.J. Lu @ 2023-04-18 21:37 UTC (permalink / raw)
  To: GNU C Library

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?

-- 
H.J.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Use adaptive mutex with std::mutex
  2023-04-18 21:37 Use adaptive mutex with std::mutex H.J. Lu
@ 2023-04-19 20:16 ` Noah Goldstein
  2023-04-29 15:34   ` Cristian Rodríguez
  0 siblings, 1 reply; 3+ messages in thread
From: Noah Goldstein @ 2023-04-19 20:16 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Use adaptive mutex with std::mutex
  2023-04-19 20:16 ` Noah Goldstein
@ 2023-04-29 15:34   ` Cristian Rodríguez
  0 siblings, 0 replies; 3+ messages in thread
From: Cristian Rodríguez @ 2023-04-29 15:34 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: H.J. Lu, GNU C Library

[-- Attachment #1: Type: text/plain, Size: 743 bytes --]

On Wed, Apr 19, 2023 at 4:17 PM Noah Goldstein via Libc-alpha <
libc-alpha@sourceware.org> wrote:

> On Tue, Apr 18, 2023 at 4:37 PM H.J. Lu via Libc-alpha
> <libc-alpha@sourceware.org> wrote:
>
> 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
>
>
>
If this is indeed the case.. Why is it not the default.. ? A quick read of
docs says  PTHREAD_MUTEX_INITIALIZER and PTHREAD_MUTEX_NORMAL are up to the
implementation to choose..

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-29 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-18 21:37 Use adaptive mutex with std::mutex H.J. Lu
2023-04-19 20:16 ` Noah Goldstein
2023-04-29 15:34   ` Cristian Rodríguez

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).