[libcxx-commits] [PATCH] D148681: [libc++] Make std::mutex use ADAPTIVE_NP intializer if available on gnu

Noah Goldstein via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Apr 18 19:56:13 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: ldionne, Mordante, philnik.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

The most performant pthread_mutex_t configuration in GLIBC is
PTHREAD_MUTEX_ADAPTIVE_NP.

The default is timed mutex which is implemented almost entirely
through the `futex` syscall (no attempt at looping for any amount of
time in userland). This adds has a high context switch cost in almost
all situations.

The ADAPTIVE_NP implementation, on the other hand, progresses through
exponential backoff before going to the `futex` allowing for lower
latency for shorter waits with extra minimal overhead or memory
traffic for longer waits.

On my Icelake machine running the GLIBC mutex benchmarks suite for N=5
runs.

There is a 12% performance improvement for low contention cases
(nthreads <= nprocs / 2) and a 2.5% performance improvement in high
contention cases (nthreads > nprocs / 2)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148681

Files:
  libcxx/include/__threading_support


Index: libcxx/include/__threading_support
===================================================================
--- libcxx/include/__threading_support
+++ libcxx/include/__threading_support
@@ -64,7 +64,11 @@
 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
 // Mutex
 typedef pthread_mutex_t __libcpp_mutex_t;
+#if defined(__USE_GNU) && defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
+#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
+#else
 #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+#endif
 
 typedef pthread_mutex_t __libcpp_recursive_mutex_t;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148681.514811.patch
Type: text/x-patch
Size: 585 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230419/4644fbc3/attachment-0001.bin>


More information about the libcxx-commits mailing list