[Mesa-dev] [PATCH v2 2/2] mesa: Add new fast mtx_t mutex type for basic use cases

Kristian Høgsberg krh at bitplanet.net
Thu Jan 29 20:54:29 PST 2015


While modern pthread mutexes are very fast, they still incur a call to an
external DSO and overhead of the generality and features of pthread mutexes.
Most mutexes in mesa only needs lock/unlock, and the idea here is that we can
inline the atomic operation and make the fast case just two intructions.
Mutexes are subtle and finicky to implement, so we carefully copy the
implementation from Ulrich Dreppers well-written and well-reviewed paper:

  "Futexes Are Tricky"
  http://www.akkadia.org/drepper/futex.pdf

We implement "mutex3", which gives us a mutex that has no syscalls on
uncontended lock or unlock.  Further, the uncontended case boils down to a
cmpxchg and an untaken branch and the uncontended unlock is just a locked decr
and an untaken branch.  We use __builtin_expect() to indicate that contention
is unlikely so that gcc will put the contention code out of the main code
flow.

A fast mutex only supports lock/unlock, can't be recursive or used with
condition variables.  We keep the pthread mutex implementation around as
full_mtx_t for the few places where we use condition variables or recursive
locking.  For platforms or compilers where futex and atomics aren't available,
mtx_t falls back to the pthread mutex.

The pthread mutex lock/unlock overhead shows up on benchmarks for CPU bound
applications.  Most CPU bound cases are helped and some of our internal
bind_buffer_object heavy benchmarks gain up to 10%.

Signed-off-by: Kristian Høgsberg <krh at bitplanet.net>
---
 configure.ac                |  3 ++
 include/c11/threads_posix.h | 98 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index c2d775e..c4b26ee 100644
--- a/configure.ac
+++ b/configure.ac
@@ -639,6 +639,9 @@ dnl to -pthread, which causes problems if we need -lpthread to appear in
 dnl pkgconfig files.
 test -z "$PTHREAD_LIBS" && PTHREAD_LIBS="-lpthread"
 
+dnl Check for futex for fast inline mtx_t.
+AC_CHECK_HEADER([linux/futex.h], [DEFINES="$DEFINES -DHAVE_FUTEX"])
+
 dnl SELinux awareness.
 AC_ARG_ENABLE([selinux],
     [AS_HELP_STRING([--enable-selinux],
diff --git a/include/c11/threads_posix.h b/include/c11/threads_posix.h
index 5119773..a48b15f 100644
--- a/include/c11/threads_posix.h
+++ b/include/c11/threads_posix.h
@@ -60,7 +60,6 @@ Configuration macro:
 
 // FIXME: temporary non-standard hack to ease transition
 #define _FULL_MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
-#define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
 
 /*---------------------------- types ----------------------------*/
 typedef pthread_cond_t  cnd_t;
@@ -252,8 +251,104 @@ full_mtx_unlock(full_mtx_t *mtx)
     return thrd_success;
 }
 
+#if defined(__GNUC__) && defined(HAVE_FUTEX)
+
+/* mtx_t - Fast, simple mutex
+ *
+ * While modern pthread mutexes are very fast (implemented using futex), they
+ * still incur a call to an external DSO and overhead of the generality and
+ * features of pthread mutexes.  Most mutexes in mesa only needs lock/unlock,
+ * and the idea here is that we can inline the atomic operation and make the
+ * fast case just two intructions.  Mutexes are subtle and finicky to
+ * implement, so we carefully copy the implementation from Ulrich Dreppers
+ * well-written and well-reviewed paper:
+ *
+ *   "Futexes Are Tricky"
+ *   http://www.akkadia.org/drepper/futex.pdf
+ *
+ * We implement "mutex3", which gives us a mutex that has no syscalls on
+ * uncontended lock or unlock.  Further, the uncontended case boils down to a
+ * locked cmpxchg and an untaken branch, the uncontended unlock is just a
+ * locked decr and an untaken branch.  We use __builtin_expect() to indicate
+ * that contention is unlikely so that gcc will put the contention code out of
+ * the main code flow.
+ *
+ * A fast mutex only supports lock/unlock, can't be recursive or used with
+ * condition variables.
+ */
+
+typedef struct {
+   uint32_t val;
+} mtx_t;
+
+#define _MTX_INITIALIZER_NP { 0 }
+
+#include <stdint.h>
+#include <values.h>
+#include <linux/futex.h>
+#include <sys/time.h>
+#include <sys/syscall.h>
+
+static inline long sys_futex(void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3)
+{
+   return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
+}
+
+static inline int futex_wake(uint32_t *addr) {
+   return sys_futex(addr, FUTEX_WAKE, 1, NULL, NULL, 0);
+}
+
+static inline int futex_wait(uint32_t *addr, int32_t value) {
+   return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
+}
+
+static inline void
+mtx_init(mtx_t *mtx, int type)
+{
+   assert(type == mtx_plain);
+
+   mtx->val = 0;
+}
+
+static inline void
+mtx_destroy(mtx_t *mtx)
+{
+}
+
+static inline void
+mtx_lock(mtx_t *mtx)
+{
+   uint32_t c;
+
+   c = __sync_val_compare_and_swap(&mtx->val, 0, 1);
+   if (__builtin_expect(c != 0, 0)) {
+      if (c != 2)
+         c = __sync_lock_test_and_set(&mtx->val, 2);
+      while (c != 0) {
+         futex_wait(&mtx->val, 2);
+         c = __sync_lock_test_and_set(&mtx->val, 2);
+      }
+   }
+}
+
+static inline void
+mtx_unlock(mtx_t *mtx)
+{
+   uint32_t c;
+
+   c = __sync_fetch_and_sub(&mtx->val, 1);
+   if (__builtin_expect(c != 1, 0)) {
+      mtx->val = 0;
+      futex_wake(&mtx->val);
+   }
+}
+
+#else
+
 typedef full_mtx_t mtx_t;
 
+#define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER
+
 static inline void
 mtx_init(mtx_t *mtx, int type)
 {
@@ -280,6 +375,7 @@ mtx_unlock(mtx_t *mtx)
    full_mtx_unlock(mtx);
 }
 
+#endif
 
 /*------------------- 7.25.5 Thread functions -------------------*/
 // 7.25.5.1
-- 
2.2.2



More information about the mesa-dev mailing list