From 0d1a92ca3d234a4291ef3ecbf7df2d57442a63e5 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 2 Oct 2011 10:01:57 -0400 Subject: [PATCH] Add new thread creation API Deprecate both g_thread_create functions and add g_thread_new() and g_thread_new_full(). The new functions expect a name for the thread. Change GThreadPool, GMainContext and GDBus to create named threads. https://bugzilla.gnome.org/show_bug.cgi?id=660635 --- gio/gdbusprivate.c | 9 +++--- glib/deprecated/gthread-deprecated.c | 36 ++++++++++++++++++++-- glib/deprecated/gthread.h | 4 +++ glib/glib.symbols | 9 +++--- glib/gmain.c | 2 +- glib/gthread.c | 59 ++++++++++++++++++++++-------------- glib/gthread.h | 6 ++-- glib/gthreadpool.c | 2 +- 8 files changed, 91 insertions(+), 36 deletions(-) diff --git a/gio/gdbusprivate.c b/gio/gdbusprivate.c index 87a3b6b..5584d51 100644 --- a/gio/gdbusprivate.c +++ b/gio/gdbusprivate.c @@ -302,10 +302,11 @@ _g_dbus_shared_thread_ref (void) data->context = g_main_context_new (); data->loop = g_main_loop_new (data->context, FALSE); - data->thread = g_thread_create (gdbus_shared_thread_func, - data, - TRUE, - &error); + data->thread = g_thread_new ("gdbus", + gdbus_shared_thread_func, + data, + TRUE, + &error); g_assert_no_error (error); /* We can cast between gsize and gpointer safely */ g_once_init_leave (&shared_thread_data, (gsize) data); diff --git a/glib/deprecated/gthread-deprecated.c b/glib/deprecated/gthread-deprecated.c index 2c9dad0..16ab732 100644 --- a/glib/deprecated/gthread-deprecated.c +++ b/glib/deprecated/gthread-deprecated.c @@ -127,6 +127,38 @@ g_thread_set_priority (GThread *thread, } /** + * g_thread_create: + * @func: a function to execute in the new thread + * @data: an argument to supply to the new thread + * @joinable: should this thread be joinable? + * @error: return location for error, or %NULL + * + * This function creates a new thread. + * + * If @joinable is %TRUE, you can wait for this threads termination + * calling g_thread_join(). Otherwise the thread will just disappear + * when it terminates. + * + * The new thread executes the function @func with the argument @data. + * If the thread was created successfully, it is returned. + * + * @error can be %NULL to ignore errors, or non-%NULL to report errors. + * The error is set, if and only if the function returns %NULL. + * + * Returns: the new #GThread on success + * + * Deprecated:2.32: Use g_thread_new() instead + */ +GThread * +g_thread_create (GThreadFunc func, + gpointer data, + gboolean joinable, + GError **error) +{ + return g_thread_new_full (NULL, func, data, joinable, 0, error); +} + +/** * g_thread_create_full: * @func: a function to execute in the new thread. * @data: an argument to supply to the new thread. @@ -140,7 +172,7 @@ g_thread_set_priority (GThread *thread, * This function creates a new thread. * * Deprecated:2.32: The @bound and @priority arguments are now ignored. - * Use g_thread_create() or g_thread_create_with_stack_size() instead. + * Use g_thread_new() or g_thread_new_full() instead. */ GThread * g_thread_create_full (GThreadFunc func, @@ -151,7 +183,7 @@ g_thread_create_full (GThreadFunc func, GThreadPriority priority, GError **error) { - return g_thread_create_with_stack_size (func, data, joinable, stack_size, error); + return g_thread_new_full (NULL, func, data, joinable, stack_size, error); } /* GStaticMutex {{{1 ------------------------------------------------------ */ diff --git a/glib/deprecated/gthread.h b/glib/deprecated/gthread.h index fd9319d..2d0b3ae 100644 --- a/glib/deprecated/gthread.h +++ b/glib/deprecated/gthread.h @@ -99,6 +99,10 @@ GLIB_VAR guint64 (*g_thread_gettime) (void); /* internal function for fallback static mutex implementation */ GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex); +GThread* g_thread_create (GThreadFunc func, + gpointer data, + gboolean joinable, + GError **error); GThread* g_thread_create_full (GThreadFunc func, gpointer data, gulong stack_size, diff --git a/glib/glib.symbols b/glib/glib.symbols index 2fd5a62..8f1992a 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1087,20 +1087,21 @@ g_pointer_bit_unlock g_once_impl g_once_init_enter_impl g_once_init_leave -g_thread_init_glib g_once_init_enter -g_thread_functions_for_glib_use g_threads_got_initialized -g_thread_use_default_impl g_thread_gettime g_thread_create -g_thread_create_with_stack_size g_thread_create_full g_thread_error_quark g_thread_exit +g_thread_functions_for_glib_use +g_thread_init_glib g_thread_join +g_thread_new +g_thread_new_full g_thread_self g_thread_set_priority +g_thread_use_default_impl g_thread_yield g_static_mutex_free g_static_mutex_get_mutex_impl diff --git a/glib/gmain.c b/glib/gmain.c index e631db2..797aea9 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -4757,7 +4757,7 @@ g_get_worker_context (void) GError *error = NULL; glib_worker_context = g_main_context_new (); - if (g_thread_create (glib_worker_main, NULL, FALSE, &error) == NULL) + if (g_thread_new ("gmain", glib_worker_main, NULL, FALSE, &error) == NULL) g_error ("Creating GLib worker thread failed: %s\n", error->message); g_once_init_leave (&initialised, TRUE); diff --git a/glib/gthread.c b/glib/gthread.c index ebacbf7..49e3060 100644 --- a/glib/gthread.c +++ b/glib/gthread.c @@ -584,6 +584,7 @@ struct _GRealThread GThread thread; GArray *private_data; GRealThread *next; + const gchar *name; gpointer retval; GSystemThread system_thread; }; @@ -1116,7 +1117,6 @@ g_thread_cleanup (gpointer data) } } G_UNLOCK (g_thread); - /* Just to make sure, this isn't used any more */ g_system_thread_assign (thread->system_thread, zero_thread); g_free (thread); @@ -1146,14 +1146,19 @@ g_thread_create_proxy (gpointer data) } /** - * g_thread_create: + * g_thread_new: + * @name: a name for the new thread * @func: a function to execute in the new thread * @data: an argument to supply to the new thread * @joinable: should this thread be joinable? - * @error: return location for error, or %NULL + * @error: return location for error * * This function creates a new thread. * + * The @name can be useful for discriminating threads in + * a debugger. Some systems restrict the length of @name to + * 16 bytes. + * * If @joinable is %TRUE, you can wait for this threads termination * calling g_thread_join(). Otherwise the thread will just disappear * when it terminates. @@ -1165,28 +1170,37 @@ g_thread_create_proxy (gpointer data) * The error is set, if and only if the function returns %NULL. * * Returns: the new #GThread on success + * + * Since: 2.32 */ GThread * -g_thread_create (GThreadFunc func, - gpointer data, - gboolean joinable, - GError **error) +g_thread_new (const gchar *name, + GThreadFunc func, + gpointer data, + gboolean joinable, + GError **error) { - return g_thread_create_with_stack_size (func, data, joinable, 0, error); + return g_thread_new_full (name, func, data, joinable, 0, error); } /** - * g_thread_create_with_stack_size: + * g_thread_new_full: + * @name: a name for the new thread * @func: a function to execute in the new thread * @data: an argument to supply to the new thread * @joinable: should this thread be joinable? * @stack_size: a stack size for the new thread * @error: return location for error * - * This function creates a new thread. If the underlying thread - * implementation supports it, the thread gets a stack size of - * @stack_size or the default value for the current platform, if - * @stack_size is 0. + * This function creates a new thread. + * + * The @name can be useful for discriminating threads in + * a debugger. Some systems restrict the length of @name to + * 16 bytes. + * + * If the underlying thread implementation supports it, the thread + * gets a stack size of @stack_size or the default value for the + * current platform, if @stack_size is 0. * * If @joinable is %TRUE, you can wait for this threads termination * calling g_thread_join(). Otherwise the thread will just disappear @@ -1198,8 +1212,8 @@ g_thread_create (GThreadFunc func, * @error can be %NULL to ignore errors, or non-%NULL to report errors. * The error is set, if and only if the function returns %NULL. * - * Only use g_thread_create_with_stack_size() if you - * really can't use g_thread_create() instead. g_thread_create() + * Only use a non-zero @stack_size if you + * really can't use the default instead. g_thread_new() * does not take @stack_size, as it should only be used in cases * in which it is unavoidable. * @@ -1207,12 +1221,13 @@ g_thread_create (GThreadFunc func, * * Since: 2.32 */ -GThread* -g_thread_create_with_stack_size (GThreadFunc func, - gpointer data, - gboolean joinable, - gsize stack_size, - GError **error) +GThread * +g_thread_new_full (const gchar *name, + GThreadFunc func, + gpointer data, + gboolean joinable, + gsize stack_size, + GError **error) { GRealThread* result; GError *local_error = NULL; @@ -1224,6 +1239,7 @@ g_thread_create_with_stack_size (GThreadFunc func, result->thread.func = func; result->thread.data = data; result->private_data = NULL; + result->name = name; G_LOCK (g_thread); g_system_thread_create (g_thread_create_proxy, result, stack_size, joinable, @@ -1316,7 +1332,6 @@ g_thread_join (GThread* thread) } } G_UNLOCK (g_thread); - /* Just to make sure, this isn't used any more */ thread->joinable = 0; g_system_thread_assign (real->system_thread, zero_thread); diff --git a/glib/gthread.h b/glib/gthread.h index c481c0e..18f9740 100644 --- a/glib/gthread.h +++ b/glib/gthread.h @@ -134,12 +134,14 @@ GLIB_VAR gboolean g_threads_got_initialized; GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex); -GThread *g_thread_create (GThreadFunc func, +GThread *g_thread_new (const gchar *name, + GThreadFunc func, gpointer data, gboolean joinable, GError **error); -GThread *g_thread_create_with_stack_size (GThreadFunc func, +GThread *g_thread_new_full (const gchar *name, + GThreadFunc func, gpointer data, gboolean joinable, gsize stack_size, diff --git a/glib/gthreadpool.c b/glib/gthreadpool.c index 56356d2..ca3c4f9 100644 --- a/glib/gthreadpool.c +++ b/glib/gthreadpool.c @@ -414,7 +414,7 @@ g_thread_pool_start_thread (GRealThreadPool *pool, GError *local_error = NULL; /* No thread was found, we have to start a new one */ - if (!g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error)) + if (!g_thread_new ("pool", g_thread_pool_thread_proxy, pool, FALSE, &local_error)) { g_propagate_error (error, local_error); return FALSE; -- 2.7.4