Add g_thread_get_name

This is meant to improve debugging of thread-related problems outside
of GLib. This commit adds an implementation for Posix. The Win32
implementation is a stub.

Test included.

Fixes: #3546
This commit is contained in:
Matthias Clasen 2024-12-03 08:06:17 +00:00 committed by Philip Withnall
parent 309ef1977f
commit 5b53337432
6 changed files with 59 additions and 10 deletions

View File

@ -732,7 +732,8 @@ g_system_thread_new (GThreadFunc proxy,
base_thread->thread.joinable = TRUE;
base_thread->thread.func = func;
base_thread->thread.data = data;
base_thread->name = g_strdup (name);
if (name)
g_strlcpy (base_thread->name, name, sizeof (base_thread->name));
thread->proxy = proxy;
posix_check_cmd (pthread_attr_init (&attr));
@ -766,7 +767,6 @@ g_system_thread_new (GThreadFunc proxy,
{
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
"Error creating thread: %s", g_strerror (ret));
g_free (thread->thread.name);
g_slice_free (GThreadPosix, thread);
return NULL;
}
@ -827,6 +827,18 @@ g_system_thread_set_name (const gchar *name)
#endif
}
void
g_system_thread_get_name (char *buffer,
gsize length)
{
#ifdef HAVE_PTHREAD_GETNAME_NP
pthread_getname_np (pthread_self (), buffer, length);
#else
g_assert (length >= 1);
buffer[0] = '\0';
#endif
}
/* {{{1 GMutex and GCond futex implementation */
#if defined(USE_NATIVE_MUTEX)

View File

@ -464,7 +464,8 @@ g_system_thread_new (GThreadFunc proxy,
base_thread->thread.joinable = TRUE;
base_thread->thread.func = func;
base_thread->thread.data = data;
base_thread->name = g_strdup (name);
if (name)
g_strlcpy (base_thread->name, name, 16);
thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread,
CREATE_SUSPENDED, &ignore);
@ -649,6 +650,15 @@ g_system_thread_set_name (const gchar *name)
SetThreadName ((DWORD) -1, name);
}
void
g_system_thread_get_name (char *buffer,
gsize length)
{
/* FIXME: Not implemented yet */
g_assert (length >= 1);
buffer[0] = '\0';
}
/* {{{1 Epilogue */
void

View File

@ -882,12 +882,10 @@ g_thread_proxy (gpointer data)
TRACE (GLIB_THREAD_SPAWNED (thread->thread.func, thread->thread.data,
thread->name));
if (thread->name)
{
g_system_thread_set_name (thread->name);
g_free (thread->name);
thread->name = NULL;
}
if (thread->name[0] != '\0')
g_system_thread_set_name (thread->name);
else
g_system_thread_get_name (thread->name, sizeof (thread->name));
thread->retval = thread->thread.func (thread->thread.data);
@ -1106,6 +1104,26 @@ g_thread_self (void)
return (GThread*) thread;
}
/**
* g_thread_get_name:
* @thread: a thread
*
* Gets the name of the thread.
*
* This function is intended for debugging purposes.
*
* Returns: the name of the thread
*
* Since: 2.84
*/
const char *
g_thread_get_name (GThread *thread)
{
GRealThread *real = (GRealThread*) thread;
return real->name;
}
/**
* g_get_num_processors:
*

View File

@ -165,6 +165,8 @@ gpointer g_thread_join (GThread *thread);
GLIB_AVAILABLE_IN_ALL
void g_thread_yield (void);
GLIB_AVAILABLE_IN_2_84
const char * g_thread_get_name (GThread *thread);
GLIB_AVAILABLE_IN_2_32
void g_mutex_init (GMutex *mutex);

View File

@ -34,7 +34,7 @@ struct _GRealThread
gint ref_count;
gboolean ours;
gchar *name;
char name[16];
gpointer retval;
};
@ -159,6 +159,8 @@ void g_system_thread_free (GRealThread *thread);
G_NORETURN void g_system_thread_exit (void);
void g_system_thread_set_name (const gchar *name);
void g_system_thread_get_name (char *buffer,
gsize length);
/* gthread.c */
GThread *g_thread_new_internal (const gchar *name,

View File

@ -194,10 +194,15 @@ thread6_func (gpointer data)
{
#if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
char name[16];
const char *name2;
pthread_getname_np (pthread_self(), name, 16);
g_assert_cmpstr (name, ==, data);
name2 = g_thread_get_name (g_thread_self ());
g_assert_cmpstr (name2, ==, data);
#endif
return NULL;