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
This commit is contained in:
Matthias Clasen
2011-10-02 10:01:57 -04:00
parent bc67c23bf9
commit 0d1a92ca3d
8 changed files with 91 additions and 36 deletions

View File

@@ -302,7 +302,8 @@ _g_dbus_shared_thread_ref (void)
data->context = g_main_context_new (); data->context = g_main_context_new ();
data->loop = g_main_loop_new (data->context, FALSE); data->loop = g_main_loop_new (data->context, FALSE);
data->thread = g_thread_create (gdbus_shared_thread_func, data->thread = g_thread_new ("gdbus",
gdbus_shared_thread_func,
data, data,
TRUE, TRUE,
&error); &error);

View File

@@ -126,6 +126,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: * g_thread_create_full:
* @func: a function to execute in the new thread. * @func: a function to execute in the new thread.
@@ -140,7 +172,7 @@ g_thread_set_priority (GThread *thread,
* This function creates a new thread. * This function creates a new thread.
* *
* Deprecated:2.32: The @bound and @priority arguments are now ignored. * 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 * GThread *
g_thread_create_full (GThreadFunc func, g_thread_create_full (GThreadFunc func,
@@ -151,7 +183,7 @@ g_thread_create_full (GThreadFunc func,
GThreadPriority priority, GThreadPriority priority,
GError **error) 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 ------------------------------------------------------ */ /* GStaticMutex {{{1 ------------------------------------------------------ */

View File

@@ -99,6 +99,10 @@ GLIB_VAR guint64 (*g_thread_gettime) (void);
/* internal function for fallback static mutex implementation */ /* internal function for fallback static mutex implementation */
GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex); 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, GThread* g_thread_create_full (GThreadFunc func,
gpointer data, gpointer data,
gulong stack_size, gulong stack_size,

View File

@@ -1087,20 +1087,21 @@ g_pointer_bit_unlock
g_once_impl g_once_impl
g_once_init_enter_impl g_once_init_enter_impl
g_once_init_leave g_once_init_leave
g_thread_init_glib
g_once_init_enter g_once_init_enter
g_thread_functions_for_glib_use
g_threads_got_initialized g_threads_got_initialized
g_thread_use_default_impl
g_thread_gettime g_thread_gettime
g_thread_create g_thread_create
g_thread_create_with_stack_size
g_thread_create_full g_thread_create_full
g_thread_error_quark g_thread_error_quark
g_thread_exit g_thread_exit
g_thread_functions_for_glib_use
g_thread_init_glib
g_thread_join g_thread_join
g_thread_new
g_thread_new_full
g_thread_self g_thread_self
g_thread_set_priority g_thread_set_priority
g_thread_use_default_impl
g_thread_yield g_thread_yield
g_static_mutex_free g_static_mutex_free
g_static_mutex_get_mutex_impl g_static_mutex_get_mutex_impl

View File

@@ -4757,7 +4757,7 @@ g_get_worker_context (void)
GError *error = NULL; GError *error = NULL;
glib_worker_context = g_main_context_new (); 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_error ("Creating GLib worker thread failed: %s\n", error->message);
g_once_init_leave (&initialised, TRUE); g_once_init_leave (&initialised, TRUE);

View File

@@ -584,6 +584,7 @@ struct _GRealThread
GThread thread; GThread thread;
GArray *private_data; GArray *private_data;
GRealThread *next; GRealThread *next;
const gchar *name;
gpointer retval; gpointer retval;
GSystemThread system_thread; GSystemThread system_thread;
}; };
@@ -1116,7 +1117,6 @@ g_thread_cleanup (gpointer data)
} }
} }
G_UNLOCK (g_thread); G_UNLOCK (g_thread);
/* Just to make sure, this isn't used any more */ /* Just to make sure, this isn't used any more */
g_system_thread_assign (thread->system_thread, zero_thread); g_system_thread_assign (thread->system_thread, zero_thread);
g_free (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 * @func: a function to execute in the new thread
* @data: an argument to supply to the new thread * @data: an argument to supply to the new thread
* @joinable: should this thread be joinable? * @joinable: should this thread be joinable?
* @error: return location for error, or %NULL * @error: return location for error
* *
* This function creates a new thread. * 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 * If @joinable is %TRUE, you can wait for this threads termination
* calling g_thread_join(). Otherwise the thread will just disappear * calling g_thread_join(). Otherwise the thread will just disappear
* when it terminates. * 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. * The error is set, if and only if the function returns %NULL.
* *
* Returns: the new #GThread on success * Returns: the new #GThread on success
*
* Since: 2.32
*/ */
GThread * GThread *
g_thread_create (GThreadFunc func, g_thread_new (const gchar *name,
GThreadFunc func,
gpointer data, gpointer data,
gboolean joinable, gboolean joinable,
GError **error) 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 * @func: a function to execute in the new thread
* @data: an argument to supply to the new thread * @data: an argument to supply to the new thread
* @joinable: should this thread be joinable? * @joinable: should this thread be joinable?
* @stack_size: a stack size for the new thread * @stack_size: a stack size for the new thread
* @error: return location for error * @error: return location for error
* *
* This function creates a new thread. If the underlying thread * This function creates a new thread.
* implementation supports it, the thread gets a stack size of *
* @stack_size or the default value for the current platform, if * The @name can be useful for discriminating threads in
* @stack_size is 0. * 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 * If @joinable is %TRUE, you can wait for this threads termination
* calling g_thread_join(). Otherwise the thread will just disappear * 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. * @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. * The error is set, if and only if the function returns %NULL.
* *
* <note><para>Only use g_thread_create_with_stack_size() if you * <note><para>Only use a non-zero @stack_size if you
* really can't use g_thread_create() instead. g_thread_create() * really can't use the default instead. g_thread_new()
* does not take @stack_size, as it should only be used in cases * does not take @stack_size, as it should only be used in cases
* in which it is unavoidable.</para></note> * in which it is unavoidable.</para></note>
* *
@@ -1207,8 +1221,9 @@ g_thread_create (GThreadFunc func,
* *
* Since: 2.32 * Since: 2.32
*/ */
GThread* GThread *
g_thread_create_with_stack_size (GThreadFunc func, g_thread_new_full (const gchar *name,
GThreadFunc func,
gpointer data, gpointer data,
gboolean joinable, gboolean joinable,
gsize stack_size, gsize stack_size,
@@ -1224,6 +1239,7 @@ g_thread_create_with_stack_size (GThreadFunc func,
result->thread.func = func; result->thread.func = func;
result->thread.data = data; result->thread.data = data;
result->private_data = NULL; result->private_data = NULL;
result->name = name;
G_LOCK (g_thread); G_LOCK (g_thread);
g_system_thread_create (g_thread_create_proxy, result, g_system_thread_create (g_thread_create_proxy, result,
stack_size, joinable, stack_size, joinable,
@@ -1316,7 +1332,6 @@ g_thread_join (GThread* thread)
} }
} }
G_UNLOCK (g_thread); G_UNLOCK (g_thread);
/* Just to make sure, this isn't used any more */ /* Just to make sure, this isn't used any more */
thread->joinable = 0; thread->joinable = 0;
g_system_thread_assign (real->system_thread, zero_thread); g_system_thread_assign (real->system_thread, zero_thread);

View File

@@ -134,12 +134,14 @@ GLIB_VAR gboolean g_threads_got_initialized;
GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex); 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, gpointer data,
gboolean joinable, gboolean joinable,
GError **error); GError **error);
GThread *g_thread_create_with_stack_size (GThreadFunc func, GThread *g_thread_new_full (const gchar *name,
GThreadFunc func,
gpointer data, gpointer data,
gboolean joinable, gboolean joinable,
gsize stack_size, gsize stack_size,

View File

@@ -414,7 +414,7 @@ g_thread_pool_start_thread (GRealThreadPool *pool,
GError *local_error = NULL; GError *local_error = NULL;
/* No thread was found, we have to start a new one */ /* 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); g_propagate_error (error, local_error);
return FALSE; return FALSE;