[glib] Use adaptive mutexes when available



commit cedc82290f860683d695d0c5326db153893eec21
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Sep 22 00:52:18 2011 -0400

    Use adaptive mutexes when available
    
    These are supposedly better on multi-cpu systems - and who doesn't
    have multiple cpus nowadays. One single-processor systems, they
    are identical to normal mutexes.
    See e.g. http://bugzilla.mozilla.org/show_bug.cgi?id=132089
    
    https://bugzilla.gnome.org/show_bug.cgi?id=659423

 glib/gthread-posix.c |   13 ++++++++++++-
 glib/gthread.h       |    4 ++++
 2 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index fa13dda..e8028d8 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -96,9 +96,20 @@ void
 g_mutex_init (GMutex *mutex)
 {
   gint status;
+  pthread_mutexattr_t *pattr = NULL;
+#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
+  pthread_mutexattr_t attr;
+  pthread_mutexattr_init (&attr);
+  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
+  pattr = &attr;
+#endif
 
-  if G_UNLIKELY ((status = pthread_mutex_init (&mutex->impl, NULL)) != 0)
+  if G_UNLIKELY ((status = pthread_mutex_init (&mutex->impl, pattr)) != 0)
     g_thread_abort (status, "pthread_mutex_init");
+
+#ifdef PTHREAD_ADAPTIVE_MUTEX_NP
+  pthread_mutexattr_destroy (&attr);
+#endif
 }
 
 /**
diff --git a/glib/gthread.h b/glib/gthread.h
index ab2e5cd..1cb4d45 100644
--- a/glib/gthread.h
+++ b/glib/gthread.h
@@ -82,7 +82,11 @@ struct _GCond
 
 #include <pthread.h>
 
+#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
+#define G_MUTEX_INIT { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP }
+#else
 #define G_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER }
+#endif
 struct _GMutex
 {
   pthread_mutex_t impl;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]