From 1909d2398ad299b86fb708104d82ce5ab7415d36 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Oct 2011 09:27:08 -0400 Subject: [PATCH] Make thread names useful in a debugger Associate the name with the system thread, so that debuggers can see it. This is currently only implemented for Linux, using prctl. https://bugzilla.gnome.org/show_bug.cgi?id=660635 --- configure.ac | 1 + glib/gthread-posix.c | 12 +++++++++++- glib/gthread-win32.c | 6 ++++++ glib/gthread.c | 3 +++ glib/gthreadprivate.h | 1 + 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index ed6cb2e..6a30ffe 100644 --- a/configure.ac +++ b/configure.ac @@ -907,6 +907,7 @@ AC_CHECK_HEADERS([sys/vfs.h sys/mount.h sys/vmount.h sys/statfs.h sys/statvfs.h] AC_CHECK_HEADERS([mntent.h sys/mnttab.h sys/vfstab.h sys/mntctl.h sys/sysctl.h fstab.h]) AC_CHECK_HEADERS([sys/uio.h sys/mkdev.h]) AC_CHECK_HEADERS([linux/magic.h]) +AC_CHECK_HEADERS([sys/prctl.h]) # check for structure fields AC_CHECK_MEMBERS([struct stat.st_mtimensec, struct stat.st_mtim.tv_nsec, struct stat.st_atimensec, struct stat.st_atim.tv_nsec, struct stat.st_ctimensec, struct stat.st_ctim.tv_nsec]) diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c index c9ccaa1..0423f42 100644 --- a/glib/gthread-posix.c +++ b/glib/gthread-posix.c @@ -63,7 +63,9 @@ #ifdef HAVE_SCHED_H #include #endif - +#ifdef HAVE_SYS_PRCTL_H +#include +#endif static void g_thread_abort (gint status, @@ -1047,5 +1049,13 @@ g_system_thread_equal (gpointer thread1, return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0); } +void +g_system_thread_set_name (const gchar *name) +{ +#ifdef HAVE_SYS_PRCTL_H + prctl (PR_SET_NAME, name, 0, 0, 0, 0); +#endif +} + /* {{{1 Epilogue */ /* vim:set foldmethod=marker: */ diff --git a/glib/gthread-win32.c b/glib/gthread-win32.c index 20d2e21..320dda7 100644 --- a/glib/gthread-win32.c +++ b/glib/gthread-win32.c @@ -621,6 +621,12 @@ g_system_thread_equal (gpointer thread1, return ((GSystemThread*)thread1)->dummy_pointer == ((GSystemThread*)thread2)->dummy_pointer; } +void +g_system_thread_set_name (const gchar *name) +{ + /* FIXME: implement */ +} + /* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */ static CRITICAL_SECTION g_thread_xp_lock; diff --git a/glib/gthread.c b/glib/gthread.c index 49e3060..f9082fd 100644 --- a/glib/gthread.c +++ b/glib/gthread.c @@ -1131,6 +1131,9 @@ g_thread_create_proxy (gpointer data) g_assert (data); + if (thread->name) + g_system_thread_set_name (thread->name); + /* This has to happen before G_LOCK, as that might call g_thread_self */ g_private_set (&g_thread_specific_private, data); diff --git a/glib/gthreadprivate.h b/glib/gthreadprivate.h index d4c814f..57eb975 100644 --- a/glib/gthreadprivate.h +++ b/glib/gthreadprivate.h @@ -46,6 +46,7 @@ G_GNUC_INTERNAL gboolean g_system_thread_equal (gpointer thread1, gpointer thread2); G_GNUC_INTERNAL void g_system_thread_exit (void); +G_GNUC_INTERNAL void g_system_thread_set_name (const gchar *name); G_GNUC_INTERNAL GSystemThread zero_thread; G_GNUC_INTERNAL GMutex g_once_mutex; -- 2.7.4