Use adaptive mutexes when available
authorMatthias Clasen <mclasen@redhat.com>
Thu, 22 Sep 2011 04:52:18 +0000 (00:52 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 22 Sep 2011 04:54:34 +0000 (00:54 -0400)
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
glib/gthread.h

index fa13dda..e8028d8 100644 (file)
@@ -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
 }
 
 /**
index ab2e5cd..1cb4d45 100644 (file)
@@ -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;