mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-16 12:45:13 +01:00
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:
parent
309ef1977f
commit
5b53337432
@ -732,7 +732,8 @@ g_system_thread_new (GThreadFunc proxy,
|
|||||||
base_thread->thread.joinable = TRUE;
|
base_thread->thread.joinable = TRUE;
|
||||||
base_thread->thread.func = func;
|
base_thread->thread.func = func;
|
||||||
base_thread->thread.data = data;
|
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;
|
thread->proxy = proxy;
|
||||||
|
|
||||||
posix_check_cmd (pthread_attr_init (&attr));
|
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,
|
g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
|
||||||
"Error creating thread: %s", g_strerror (ret));
|
"Error creating thread: %s", g_strerror (ret));
|
||||||
g_free (thread->thread.name);
|
|
||||||
g_slice_free (GThreadPosix, thread);
|
g_slice_free (GThreadPosix, thread);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -827,6 +827,18 @@ g_system_thread_set_name (const gchar *name)
|
|||||||
#endif
|
#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 */
|
/* {{{1 GMutex and GCond futex implementation */
|
||||||
|
|
||||||
#if defined(USE_NATIVE_MUTEX)
|
#if defined(USE_NATIVE_MUTEX)
|
||||||
|
@ -464,7 +464,8 @@ g_system_thread_new (GThreadFunc proxy,
|
|||||||
base_thread->thread.joinable = TRUE;
|
base_thread->thread.joinable = TRUE;
|
||||||
base_thread->thread.func = func;
|
base_thread->thread.func = func;
|
||||||
base_thread->thread.data = data;
|
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,
|
thread->handle = (HANDLE) _beginthreadex (NULL, stack_size, g_thread_win32_proxy, thread,
|
||||||
CREATE_SUSPENDED, &ignore);
|
CREATE_SUSPENDED, &ignore);
|
||||||
@ -649,6 +650,15 @@ g_system_thread_set_name (const gchar *name)
|
|||||||
SetThreadName ((DWORD) -1, 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 */
|
/* {{{1 Epilogue */
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -882,12 +882,10 @@ g_thread_proxy (gpointer data)
|
|||||||
TRACE (GLIB_THREAD_SPAWNED (thread->thread.func, thread->thread.data,
|
TRACE (GLIB_THREAD_SPAWNED (thread->thread.func, thread->thread.data,
|
||||||
thread->name));
|
thread->name));
|
||||||
|
|
||||||
if (thread->name)
|
if (thread->name[0] != '\0')
|
||||||
{
|
g_system_thread_set_name (thread->name);
|
||||||
g_system_thread_set_name (thread->name);
|
else
|
||||||
g_free (thread->name);
|
g_system_thread_get_name (thread->name, sizeof (thread->name));
|
||||||
thread->name = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
thread->retval = thread->thread.func (thread->thread.data);
|
thread->retval = thread->thread.func (thread->thread.data);
|
||||||
|
|
||||||
@ -1106,6 +1104,26 @@ g_thread_self (void)
|
|||||||
return (GThread*) thread;
|
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:
|
* g_get_num_processors:
|
||||||
*
|
*
|
||||||
|
@ -165,6 +165,8 @@ gpointer g_thread_join (GThread *thread);
|
|||||||
GLIB_AVAILABLE_IN_ALL
|
GLIB_AVAILABLE_IN_ALL
|
||||||
void g_thread_yield (void);
|
void g_thread_yield (void);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_84
|
||||||
|
const char * g_thread_get_name (GThread *thread);
|
||||||
|
|
||||||
GLIB_AVAILABLE_IN_2_32
|
GLIB_AVAILABLE_IN_2_32
|
||||||
void g_mutex_init (GMutex *mutex);
|
void g_mutex_init (GMutex *mutex);
|
||||||
|
@ -34,7 +34,7 @@ struct _GRealThread
|
|||||||
|
|
||||||
gint ref_count;
|
gint ref_count;
|
||||||
gboolean ours;
|
gboolean ours;
|
||||||
gchar *name;
|
char name[16];
|
||||||
gpointer retval;
|
gpointer retval;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -159,6 +159,8 @@ void g_system_thread_free (GRealThread *thread);
|
|||||||
|
|
||||||
G_NORETURN void g_system_thread_exit (void);
|
G_NORETURN void g_system_thread_exit (void);
|
||||||
void g_system_thread_set_name (const gchar *name);
|
void g_system_thread_set_name (const gchar *name);
|
||||||
|
void g_system_thread_get_name (char *buffer,
|
||||||
|
gsize length);
|
||||||
|
|
||||||
/* gthread.c */
|
/* gthread.c */
|
||||||
GThread *g_thread_new_internal (const gchar *name,
|
GThread *g_thread_new_internal (const gchar *name,
|
||||||
|
@ -194,10 +194,15 @@ thread6_func (gpointer data)
|
|||||||
{
|
{
|
||||||
#if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
|
#if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
|
||||||
char name[16];
|
char name[16];
|
||||||
|
const char *name2;
|
||||||
|
|
||||||
pthread_getname_np (pthread_self(), name, 16);
|
pthread_getname_np (pthread_self(), name, 16);
|
||||||
|
|
||||||
g_assert_cmpstr (name, ==, data);
|
g_assert_cmpstr (name, ==, data);
|
||||||
|
|
||||||
|
name2 = g_thread_get_name (g_thread_self ());
|
||||||
|
|
||||||
|
g_assert_cmpstr (name2, ==, data);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user