summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim-Philipp Müller <tim@centricular.com>2014-12-12 01:38:24 +0000
committerRyan Lortie <desrt@desrt.ca>2015-03-12 21:01:36 -0400
commit627a145e16abe92dc5fe94bff6a379a9a1379837 (patch)
tree9c9324af86726c34cc3e27d515dc49bcbe13fbab
parent6fffce2588b19e5c80915cc9f713fc51d6dd3879 (diff)
downloadglib-627a145e16abe92dc5fe94bff6a379a9a1379837.tar.gz
threads: use FUTEX_WAIT_PRIVATE and FUTEX_WAKE_PRIVATE if possible
This avoids some expensive code paths in the kernel, see http://lwn.net/Articles/229668/ https://bugzilla.gnome.org/show_bug.cgi?id=741442
-rw-r--r--glib/gbitlock.c9
-rw-r--r--glib/gthread-posix.c17
2 files changed, 18 insertions, 8 deletions
diff --git a/glib/gbitlock.c b/glib/gbitlock.c
index 572c2d187..b0b53504e 100644
--- a/glib/gbitlock.c
+++ b/glib/gbitlock.c
@@ -52,6 +52,11 @@ static GSList *g_futex_address_list = NULL;
#include <sys/syscall.h>
#include <unistd.h>
+#ifndef FUTEX_WAIT_PRIVATE
+#define FUTEX_WAIT_PRIVATE FUTEX_WAIT
+#define FUTEX_WAKE_PRIVATE FUTEX_WAKE
+#endif
+
/* < private >
* g_futex_wait:
* @address: a pointer to an integer
@@ -73,7 +78,7 @@ static void
g_futex_wait (const volatile gint *address,
gint value)
{
- syscall (__NR_futex, address, (gsize) FUTEX_WAIT, (gsize) value, NULL);
+ syscall (__NR_futex, address, (gsize) FUTEX_WAIT_PRIVATE, (gsize) value, NULL);
}
/* < private >
@@ -90,7 +95,7 @@ g_futex_wait (const volatile gint *address,
static void
g_futex_wake (const volatile gint *address)
{
- syscall (__NR_futex, address, (gsize) FUTEX_WAKE, (gsize) 1, NULL);
+ syscall (__NR_futex, address, (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
}
#else
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index 53c994e39..ae5d8050b 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -1239,6 +1239,11 @@ g_system_thread_set_name (const gchar *name)
#include <linux/futex.h>
#include <sys/syscall.h>
+#ifndef FUTEX_WAIT_PRIVATE
+#define FUTEX_WAIT_PRIVATE FUTEX_WAIT
+#define FUTEX_WAKE_PRIVATE FUTEX_WAKE
+#endif
+
/* We should expand the set of operations available in gatomic once we
* have better C11 support in GCC in common distributions (ie: 4.9).
*
@@ -1305,7 +1310,7 @@ g_mutex_lock_slowpath (GMutex *mutex)
* Otherwise, sleep for as long as the 2 remains...
*/
while (exchange_acquire (&mutex->i[0], 2) != 0)
- syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAIT, (gsize) 2, NULL);
+ syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) 2, NULL);
}
static void __attribute__((noinline))
@@ -1321,7 +1326,7 @@ g_mutex_unlock_slowpath (GMutex *mutex,
abort ();
}
- syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAKE, (gsize) 1, NULL);
+ syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
}
void
@@ -1387,7 +1392,7 @@ g_cond_wait (GCond *cond,
guint sampled = g_atomic_int_get (&cond->i[0]);
g_mutex_unlock (mutex);
- syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT, (gsize) sampled, NULL);
+ syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, NULL);
g_mutex_lock (mutex);
}
@@ -1396,7 +1401,7 @@ g_cond_signal (GCond *cond)
{
g_atomic_int_inc (&cond->i[0]);
- syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE, (gsize) 1, NULL);
+ syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
}
void
@@ -1404,7 +1409,7 @@ g_cond_broadcast (GCond *cond)
{
g_atomic_int_inc (&cond->i[0]);
- syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE, (gsize) INT_MAX, NULL);
+ syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) INT_MAX, NULL);
}
gboolean
@@ -1434,7 +1439,7 @@ g_cond_wait_until (GCond *cond,
sampled = cond->i[0];
g_mutex_unlock (mutex);
- res = syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT, (gsize) sampled, &span);
+ res = syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, &span);
g_mutex_lock (mutex);
return (res < 0 && errno == ETIMEDOUT) ? FALSE : TRUE;