diff options
author | Ryan Lortie <desrt@desrt.ca> | 2011-10-13 01:00:57 -0400 |
---|---|---|
committer | Ryan Lortie <desrt@desrt.ca> | 2011-10-13 01:00:57 -0400 |
commit | 430c5635f245ca485f09035f1b6c3a59dd69758c (patch) | |
tree | a3b745d9c991565a087d157522ebf3b3175497e6 /glib | |
parent | 015f4b4513279c4be40c03121473ffcea347ed84 (diff) | |
download | glib-430c5635f245ca485f09035f1b6c3a59dd69758c.tar.gz |
g_thread_new: never fail
Remove the GError argument from g_thread_new() and abort on failure.
Introduce g_thread_try() for those who want to handle failure.
Diffstat (limited to 'glib')
-rw-r--r-- | glib/glib.symbols | 1 | ||||
-rw-r--r-- | glib/gmain.c | 6 | ||||
-rw-r--r-- | glib/gthread.c | 40 | ||||
-rw-r--r-- | glib/gthread.h | 3 | ||||
-rw-r--r-- | glib/gthreadpool.c | 2 | ||||
-rw-r--r-- | glib/tests/once.c | 2 | ||||
-rw-r--r-- | glib/tests/thread.c | 6 |
7 files changed, 46 insertions, 14 deletions
diff --git a/glib/glib.symbols b/glib/glib.symbols index 29d43a40a..b156bccad 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1103,6 +1103,7 @@ g_thread_new_full g_thread_ref g_thread_self g_thread_set_priority +g_thread_try g_thread_use_default_impl g_thread_unref g_thread_yield diff --git a/glib/gmain.c b/glib/gmain.c index 1a3c67a32..ef9ac46b4 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -4757,12 +4757,8 @@ g_get_worker_context (void) if (g_once_init_enter (&initialised)) { - GError *error = NULL; - glib_worker_context = g_main_context_new (); - if (g_thread_new ("gmain", glib_worker_main, NULL, &error) == NULL) - g_error ("Creating GLib worker thread failed: %s\n", error->message); - + g_thread_new ("gmain", glib_worker_main, NULL); g_once_init_leave (&initialised, TRUE); } diff --git a/glib/gthread.c b/glib/gthread.c index 1567b5faf..2cdac5354 100644 --- a/glib/gthread.c +++ b/glib/gthread.c @@ -731,17 +731,48 @@ g_thread_proxy (gpointer data) * a debugger. Some systems restrict the length of @name to * 16 bytes. * - * @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. + * If the thread can not be created the program aborts. See + * g_thread_try() if you want to attempt to deal with failures. * - * You must + * Returns: the new #GThread + * + * Since: 2.32 + */ +GThread * +g_thread_new (const gchar *name, + GThreadFunc func, + gpointer data) +{ + GError *error = NULL; + GThread *thread; + + thread = g_thread_new_internal (name, g_thread_proxy, func, data, 0, &error); + + if G_UNLIKELY (thread == NULL) + g_error ("creating thread '%s': %s", name ? name : "", error->message); + + return thread; +} + +/** + * g_thread_try: + * @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 + * @error: return location for error + * + * This function is the same as g_thread_new() except that it allows for + * the possibility of failure. + * + * If a thread can not be created (due to resource limits), @error is + * set and %NULL is returned. * * Returns: the new #GThread, or %NULL if an error occurred * * Since: 2.32 */ GThread * -g_thread_new (const gchar *name, +g_thread_try (const gchar *name, GThreadFunc func, gpointer data, GError **error) @@ -749,6 +780,7 @@ g_thread_new (const gchar *name, return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error); } + /** * g_thread_new_full: * @name: a name for the new thread diff --git a/glib/gthread.h b/glib/gthread.h index 3b08016b0..6400f69ba 100644 --- a/glib/gthread.h +++ b/glib/gthread.h @@ -141,6 +141,9 @@ GThread * g_thread_ref (GThread *thread); void g_thread_unref (GThread *thread); GThread * g_thread_new (const gchar *name, GThreadFunc func, + gpointer data); +GThread * g_thread_try (const gchar *name, + GThreadFunc func, gpointer data, GError **error); GThread * g_thread_new_full (const gchar *name, diff --git a/glib/gthreadpool.c b/glib/gthreadpool.c index d2402f845..a07628fe2 100644 --- a/glib/gthreadpool.c +++ b/glib/gthreadpool.c @@ -414,7 +414,7 @@ g_thread_pool_start_thread (GRealThreadPool *pool, GThread *thread; /* No thread was found, we have to start a new one */ - thread = g_thread_new ("pool", g_thread_pool_thread_proxy, pool, error); + thread = g_thread_try ("pool", g_thread_pool_thread_proxy, pool, error); if (thread == NULL) return FALSE; diff --git a/glib/tests/once.c b/glib/tests/once.c index 06e811c8b..b11e577f6 100644 --- a/glib/tests/once.c +++ b/glib/tests/once.c @@ -104,7 +104,7 @@ test_once3 (void) shared = 0; for (i = 0; i < THREADS; i++) - threads[i] = g_thread_new ("once3", thread_func, NULL, NULL); + threads[i] = g_thread_new ("once3", thread_func, NULL); for (i = 0; i < THREADS; i++) g_thread_join (threads[i]); diff --git a/glib/tests/thread.c b/glib/tests/thread.c index 4ac93747f..e418d016b 100644 --- a/glib/tests/thread.c +++ b/glib/tests/thread.c @@ -50,7 +50,7 @@ test_thread1 (void) GThread *thread; GError *error = NULL; - thread = g_thread_new ("test", thread1_func, NULL, &error); + thread = g_thread_try ("test", thread1_func, NULL, &error); g_assert_no_error (error); result = g_thread_join (thread); @@ -71,7 +71,7 @@ test_thread2 (void) gpointer result; GThread *thread; - thread = g_thread_new ("test", thread2_func, NULL, NULL); + thread = g_thread_new ("test", thread2_func, NULL); g_assert (g_thread_self () != thread); @@ -135,7 +135,7 @@ test_thread4 (void) g_error ("prlimit failed: %s\n", g_strerror (ret)); error = NULL; - thread = g_thread_new ("a", thread1_func, NULL, &error); + thread = g_thread_try ("a", thread1_func, NULL, &error); g_assert (thread == NULL); g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN); g_error_free (error); |