[glib-networking] Port to GTask



commit d43be45c9c04293854d42af384157b8bd410baad
Author: Dan Winship <danw gnome org>
Date:   Wed Jun 20 16:17:57 2012 -0400

    Port to GTask

 configure.ac                       |    2 +-
 proxy/gnome/gproxyresolvergnome.c  |   47 +++---------
 proxy/libproxy/glibproxyresolver.c |   48 ++++--------
 tls/gnutls/gtlsconnection-gnutls.c |  143 +++++++++++++++---------------------
 tls/tests/mock-interaction.c       |   40 +++-------
 5 files changed, 99 insertions(+), 181 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index cf0a7e4..3aeba9e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -34,7 +34,7 @@ dnl *****************************
 dnl *** Check GLib GIO        ***
 dnl *****************************
 AM_PATH_GLIB_2_0(2.35.0,,AC_MSG_ERROR(GLIB not found),gio)
-GLIB_CFLAGS="$GLIB_CFLAGS -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_34"
+GLIB_CFLAGS="$GLIB_CFLAGS -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_36"
 
 GIO_MODULE_DIR=$($PKG_CONFIG --variable giomoduledir gio-2.0)
 AS_IF([test "x$GIO_MODULE_DIR" = "x"],
diff --git a/proxy/gnome/gproxyresolvergnome.c b/proxy/gnome/gproxyresolvergnome.c
index 3c4d63c..31cdd0d 100644
--- a/proxy/gnome/gproxyresolvergnome.c
+++ b/proxy/gnome/gproxyresolvergnome.c
@@ -546,7 +546,7 @@ got_autoconfig_proxies (GObject      *source,
 			GAsyncResult *result,
 			gpointer      user_data)
 {
-  GSimpleAsyncResult *simple = user_data;
+  GTask *task = user_data;
   GVariant *vproxies;
   char **proxies;
   GError *error = NULL;
@@ -556,17 +556,12 @@ got_autoconfig_proxies (GObject      *source,
   if (vproxies)
     {
       g_variant_get (vproxies, "(^as)", &proxies);
-      g_simple_async_result_set_op_res_gpointer (simple, proxies,
-						 (GDestroyNotify)g_strfreev);
+      g_task_return_pointer (task, proxies, (GDestroyNotify)g_strfreev);
       g_variant_unref (vproxies);
     }
   else
-    {
-      g_simple_async_result_set_from_error (simple, error);
-      g_error_free (error);
-    }
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
+    g_task_return_error (task, error);
+  g_object_unref (task);
 }
 
 static void
@@ -577,11 +572,9 @@ g_proxy_resolver_gnome_lookup_async (GProxyResolver      *proxy_resolver,
 				     gpointer             user_data)
 {
   GProxyResolverGnome *resolver = G_PROXY_RESOLVER_GNOME (proxy_resolver);
-  GSimpleAsyncResult *simple;
+  GTask *task;
 
-  simple = g_simple_async_result_new (G_OBJECT (resolver),
-				      callback, user_data,
-				      g_proxy_resolver_gnome_lookup_async);
+  task = g_task_new (resolver, cancellable, callback, user_data);
 
   if (resolver->pacrunner)
     {
@@ -594,7 +587,7 @@ g_proxy_resolver_gnome_lookup_async (GProxyResolver      *proxy_resolver,
 			 -1,
 			 cancellable,
 			 got_autoconfig_proxies,
-			 simple);
+			 task);
     }
   else
     {
@@ -604,17 +597,10 @@ g_proxy_resolver_gnome_lookup_async (GProxyResolver      *proxy_resolver,
       proxies = g_proxy_resolver_gnome_lookup (proxy_resolver, uri,
 					       cancellable, &error);
       if (proxies)
-	{
-	  g_simple_async_result_set_op_res_gpointer (simple, proxies,
-						     (GDestroyNotify)g_strfreev);
-	}
+	g_task_return_pointer (task, proxies, (GDestroyNotify)g_strfreev);
       else
-	{
-	  g_simple_async_result_set_from_error (simple, error);
-	  g_error_free (error);
-	}
-      g_simple_async_result_complete_in_idle (simple);
-      g_object_unref (simple);
+	g_task_return_error (task, error);
+      g_object_unref (task);
     }
 }
 
@@ -623,18 +609,9 @@ g_proxy_resolver_gnome_lookup_finish (GProxyResolver  *resolver,
 				      GAsyncResult    *result,
 				      GError         **error)
 {
-  GSimpleAsyncResult *simple;
-  gchar **proxies;
-
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (resolver), g_proxy_resolver_gnome_lookup_async), NULL);
-
-  simple = G_SIMPLE_ASYNC_RESULT (result);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    return NULL;
+  g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
 
-  proxies = g_simple_async_result_get_op_res_gpointer (simple);
-  return g_strdupv (proxies);
+  return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 static void
diff --git a/proxy/libproxy/glibproxyresolver.c b/proxy/libproxy/glibproxyresolver.c
index 3133ffe..96a3fbe 100644
--- a/proxy/libproxy/glibproxyresolver.c
+++ b/proxy/libproxy/glibproxyresolver.c
@@ -185,26 +185,22 @@ g_libproxy_resolver_lookup (GProxyResolver  *iresolver,
 }
 
 static void
-_lookup_async (GSimpleAsyncResult *simple,
-	       GObject *object,
+_lookup_async (GTask        *task,
+	       gpointer      object,
+	       gpointer      task_data,
 	       GCancellable *cancellable)
 {
-  GLibProxyResolver *resolver = G_LIBPROXY_RESOLVER (object);
+  GLibProxyResolver *resolver = object;
+  gchar *uri = task_data;
   GError *error = NULL;
   gchar **proxies = NULL;
-  gchar *uri;
-
-  uri = g_simple_async_result_get_op_res_gpointer (simple);
 
   proxies = get_libproxy_proxies (resolver, uri, cancellable, &error);
 
   if (error)
-    {
-      g_simple_async_result_set_from_error (simple, error);
-      g_error_free (error);
-    }
+    g_task_return_error (task, error);
   else
-    g_simple_async_result_set_op_res_gpointer (simple, proxies, (GDestroyNotify)free_libproxy_proxies);
+    g_task_return_pointer (task, proxies, (GDestroyNotify)free_libproxy_proxies);
 }
 
 static void
@@ -214,17 +210,12 @@ g_libproxy_resolver_lookup_async (GProxyResolver      *resolver,
 				  GAsyncReadyCallback  callback,
 				  gpointer             user_data)
 {
-  GSimpleAsyncResult *simple;
-
-  simple = g_simple_async_result_new (G_OBJECT (resolver),
-				      callback, user_data,
-				      g_libproxy_resolver_lookup_async);
-  g_simple_async_result_set_op_res_gpointer (simple,
-					     g_strdup (uri),
-					     (GDestroyNotify) g_free);
-  g_simple_async_result_run_in_thread (simple, _lookup_async,
-				       G_PRIORITY_DEFAULT, cancellable);
-  g_object_unref (simple);
+  GTask *task;
+
+  task = g_task_new (resolver, cancellable, callback, user_data);
+  g_task_set_task_data (task, g_strdup (uri), (GDestroyNotify) g_free);
+  g_task_run_in_thread (task, _lookup_async);
+  g_object_unref (task);
 }
 
 static gchar **
@@ -232,18 +223,9 @@ g_libproxy_resolver_lookup_finish (GProxyResolver     *resolver,
 				   GAsyncResult       *result,
 				   GError            **error)
 {
-  GSimpleAsyncResult *simple;
-  gchar **proxies;
-
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (resolver), g_libproxy_resolver_lookup_async), NULL);
-
-  simple = G_SIMPLE_ASYNC_RESULT (result);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    return NULL;
+  g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
 
-  proxies = g_simple_async_result_get_op_res_gpointer (simple);
-  return copy_proxies (proxies);
+  return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 static void
diff --git a/tls/gnutls/gtlsconnection-gnutls.c b/tls/gnutls/gtlsconnection-gnutls.c
index ccdb59c..b03ecb9 100644
--- a/tls/gnutls/gtlsconnection-gnutls.c
+++ b/tls/gnutls/gtlsconnection-gnutls.c
@@ -65,7 +65,7 @@ static gboolean do_implicit_handshake (GTlsConnectionGnutls  *gnutls,
 				       GCancellable          *cancellable,
 				       GError               **error);
 static gboolean finish_handshake (GTlsConnectionGnutls  *gnutls,
-				  GSimpleAsyncResult    *thread_result,
+				  GTask                 *thread_task,
 				  GError               **error);
 
 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GTlsConnectionGnutls, g_tls_connection_gnutls, G_TYPE_TLS_CONNECTION,
@@ -129,7 +129,7 @@ struct _GTlsConnectionGnutlsPrivate
    */
   gboolean need_handshake, need_finish_handshake;
   gboolean started_handshake, handshaking, ever_handshaked;
-  GSimpleAsyncResult *implicit_handshake;
+  GTask *implicit_handshake;
   GError *handshake_error;
 
   gboolean closing, closed;
@@ -1024,11 +1024,12 @@ g_tls_connection_gnutls_push_func (gnutls_transport_ptr_t  transport_data,
 }
 
 static void
-handshake_thread (GSimpleAsyncResult *result,
-		  GObject            *object,
-		  GCancellable       *cancellable)
+handshake_thread (GTask        *task,
+		  gpointer      object,
+		  gpointer      task_data,
+		  GCancellable *cancellable)
 {
-  GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (object);
+  GTlsConnectionGnutls *gnutls = object;
   gboolean is_client;
   GError *error = NULL;
   int ret;
@@ -1038,7 +1039,7 @@ handshake_thread (GSimpleAsyncResult *result,
   if (!claim_op (gnutls, G_TLS_CONNECTION_GNUTLS_OP_HANDSHAKE,
 		 TRUE, cancellable, &error))
     {
-      g_simple_async_result_take_error (result, error);
+      g_task_return_error (task, error);
       return;
     }
 
@@ -1056,7 +1057,7 @@ handshake_thread (GSimpleAsyncResult *result,
 
       if (error)
 	{
-	  g_simple_async_result_take_error (result, error);
+	  g_task_return_error (task, error);
 	  return;
 	}
     }
@@ -1076,9 +1077,9 @@ handshake_thread (GSimpleAsyncResult *result,
   gnutls->priv->ever_handshaked = TRUE;
 
   if (error)
-    g_simple_async_result_take_error (result, error);
+    g_task_return_error (task, error);
   else
-    g_simple_async_result_set_op_res_gboolean (result, TRUE);
+    g_task_return_boolean (task, TRUE);
 }
 
 static GTlsCertificate *
@@ -1186,7 +1187,7 @@ accept_peer_certificate (GTlsConnectionGnutls *gnutls,
 
 static gboolean
 finish_handshake (GTlsConnectionGnutls  *gnutls,
-		  GSimpleAsyncResult    *result,
+		  GTask                 *task,
 		  GError               **error)
 {
   GTlsCertificate *peer_certificate;
@@ -1194,7 +1195,7 @@ finish_handshake (GTlsConnectionGnutls  *gnutls,
 
   g_assert (error != NULL);
 
-  if (!g_simple_async_result_propagate_error (result, error) &&
+  if (g_task_propagate_boolean (task, error) &&
       gnutls_certificate_type_get (gnutls->priv->session) == GNUTLS_CRT_X509)
     peer_certificate = get_peer_certificate_from_session (gnutls);
   else
@@ -1235,16 +1236,14 @@ g_tls_connection_gnutls_handshake (GTlsConnection   *conn,
 				   GError          **error)
 {
   GTlsConnectionGnutls *gnutls = G_TLS_CONNECTION_GNUTLS (conn);
-  GSimpleAsyncResult *result;
+  GTask *task;
   gboolean success;
   GError *my_error = NULL;
 
-  result = g_simple_async_result_new (G_OBJECT (conn), NULL, NULL,
-				      g_tls_connection_gnutls_handshake);
-  handshake_thread (result, G_OBJECT (conn), cancellable);
-
-  success = finish_handshake (gnutls, result, &my_error);
-  g_object_unref (result);
+  task = g_task_new (conn, cancellable, NULL, NULL);
+  g_task_run_in_thread_sync (task, handshake_thread);
+  success = finish_handshake (gnutls, task, &my_error);
+  g_object_unref (task);
 
   yield_op (gnutls, G_TLS_CONNECTION_GNUTLS_OP_HANDSHAKE);
 
@@ -1253,10 +1252,9 @@ g_tls_connection_gnutls_handshake (GTlsConnection   *conn,
   return success;
 }
 
-/* In the async version we use two GSimpleAsyncResults; one to run
- * handshake_thread() and then call handshake_thread_completed(), and
- * a second to call the caller's original callback after we call
- * finish_handshake().
+/* In the async version we use two GTasks; one to run handshake_thread() and
+ * then call handshake_thread_completed(), and a second to call the caller's
+ * original callback after we call finish_handshake().
  */
 
 static void
@@ -1264,23 +1262,19 @@ handshake_thread_completed (GObject      *object,
 			    GAsyncResult *result,
 			    gpointer      user_data)
 {
-  GTlsConnectionGnutls *gnutls;
-  GSimpleAsyncResult *caller_result = user_data;
+  GTask *caller_task = user_data;
+  GTlsConnectionGnutls *gnutls = g_task_get_source_object (caller_task);
   GError *error = NULL;
   gboolean success;
 
-  gnutls = G_TLS_CONNECTION_GNUTLS (g_async_result_get_source_object (G_ASYNC_RESULT (caller_result)));
-  g_object_unref (gnutls);
-
-  success = finish_handshake (gnutls, G_SIMPLE_ASYNC_RESULT (result), &error);
+  success = finish_handshake (gnutls, G_TASK (result), &error);
   yield_op (gnutls, G_TLS_CONNECTION_GNUTLS_OP_HANDSHAKE);
 
   if (success)
-    g_simple_async_result_set_op_res_gboolean (caller_result, TRUE);
+    g_task_return_boolean (caller_task, TRUE);
   else
-    g_simple_async_result_take_error (caller_result, error);
-  g_simple_async_result_complete (caller_result);
-  g_object_unref (caller_result);
+    g_task_return_error (caller_task, error);
+  g_object_unref (caller_task);
 }
 
 static void
@@ -1290,17 +1284,16 @@ g_tls_connection_gnutls_handshake_async (GTlsConnection       *conn,
 					 GAsyncReadyCallback   callback,
 					 gpointer              user_data)
 {
-  GSimpleAsyncResult *thread_result, *caller_result;
+  GTask *thread_task, *caller_task;
 
-  caller_result = g_simple_async_result_new (G_OBJECT (conn), callback, user_data,
-					     g_tls_connection_gnutls_handshake_async);
+  caller_task = g_task_new (conn, cancellable, callback, user_data);
+  g_task_set_priority (caller_task, io_priority);
 
-  thread_result = g_simple_async_result_new (G_OBJECT (conn),
-					     handshake_thread_completed, caller_result,
-					     g_tls_connection_gnutls_handshake_async);
-  g_simple_async_result_run_in_thread (thread_result, handshake_thread,
-				       io_priority, cancellable);
-  g_object_unref (thread_result);
+  thread_task = g_task_new (conn, cancellable,
+			    handshake_thread_completed, caller_task);
+  g_task_set_priority (thread_task, io_priority);
+  g_task_run_in_thread (thread_task, handshake_thread);
+  g_object_unref (thread_task);
 }
 
 static gboolean
@@ -1308,16 +1301,9 @@ g_tls_connection_gnutls_handshake_finish (GTlsConnection       *conn,
 					  GAsyncResult         *result,
 					  GError              **error)
 {
-  GSimpleAsyncResult *simple;
+  g_return_val_if_fail (g_task_is_valid (result, conn), FALSE);
 
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (conn), g_tls_connection_gnutls_handshake_async), FALSE);
-
-  simple = G_SIMPLE_ASYNC_RESULT (result);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    return FALSE;
-
-  return g_simple_async_result_get_op_res_gboolean (simple);
+  return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 static void
@@ -1342,10 +1328,9 @@ do_implicit_handshake (GTlsConnectionGnutls  *gnutls,
 {
   /* We have op_mutex */
 
-  gnutls->priv->implicit_handshake =
-    g_simple_async_result_new (G_OBJECT (gnutls),
-			       implicit_handshake_completed, NULL,
-			       do_implicit_handshake);
+  gnutls->priv->implicit_handshake = g_task_new (gnutls, cancellable,
+						 implicit_handshake_completed,
+						 NULL);
 
   if (blocking)
     {
@@ -1353,9 +1338,8 @@ do_implicit_handshake (GTlsConnectionGnutls  *gnutls,
       gboolean success;
 
       g_mutex_unlock (&gnutls->priv->op_mutex);
-      handshake_thread (gnutls->priv->implicit_handshake,
-			G_OBJECT (gnutls),
-			cancellable);
+      g_task_run_in_thread_sync (gnutls->priv->implicit_handshake,
+				 handshake_thread);
       success = finish_handshake (gnutls,
 				  gnutls->priv->implicit_handshake,
 				  &my_error);
@@ -1369,9 +1353,8 @@ do_implicit_handshake (GTlsConnectionGnutls  *gnutls,
     }
   else
     {
-      g_simple_async_result_run_in_thread (gnutls->priv->implicit_handshake,
-					   handshake_thread,
-					   G_PRIORITY_DEFAULT, cancellable);
+      g_task_run_in_thread (gnutls->priv->implicit_handshake,
+			    handshake_thread);
 
       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
 			   _("Operation would block"));
@@ -1502,17 +1485,18 @@ g_tls_connection_gnutls_close (GIOStream     *stream,
  * (since handshakes are also done synchronously now).
  */
 static void
-close_thread (GSimpleAsyncResult *result,
-	      GObject            *object,
-	      GCancellable       *cancellable)
+close_thread (GTask        *task,
+	      gpointer      object,
+	      gpointer      task_data,
+	      GCancellable *cancellable)
 {
-  GIOStream *stream = G_IO_STREAM (object);
+  GIOStream *stream = object;
   GError *error = NULL;
 
   if (!g_tls_connection_gnutls_close (stream, cancellable, &error))
-    g_simple_async_result_take_error (result, error);
+    g_task_return_error (task, error);
   else
-    g_simple_async_result_set_op_res_gboolean (result, TRUE);
+    g_task_return_boolean (task, TRUE);
 }
 
 static void
@@ -1522,14 +1506,12 @@ g_tls_connection_gnutls_close_async (GIOStream           *stream,
 				     GAsyncReadyCallback  callback,
 				     gpointer             user_data)
 {
-  GSimpleAsyncResult *result;
-
-  result = g_simple_async_result_new (G_OBJECT (stream),
-				      callback, user_data,
-				      g_tls_connection_gnutls_close_async);
-  g_simple_async_result_run_in_thread (result, close_thread,
-				       io_priority, cancellable);
-  g_object_unref (result);
+  GTask *task;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_set_priority (task, io_priority);
+  g_task_run_in_thread (task, close_thread);
+  g_object_unref (task);
 }
 
 static gboolean
@@ -1537,16 +1519,9 @@ g_tls_connection_gnutls_close_finish (GIOStream           *stream,
 				      GAsyncResult        *result,
 				      GError             **error)
 {
-  GSimpleAsyncResult *simple;
-
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (stream), g_tls_connection_gnutls_close_async), FALSE);
-
-  simple = G_SIMPLE_ASYNC_RESULT (result);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    return FALSE;
+  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
 
-  return g_simple_async_result_get_op_res_gboolean (simple);
+  return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 #ifdef HAVE_PKCS11
diff --git a/tls/tests/mock-interaction.c b/tls/tests/mock-interaction.c
index 578106b..ee518a5 100644
--- a/tls/tests/mock-interaction.c
+++ b/tls/tests/mock-interaction.c
@@ -29,17 +29,6 @@
 G_DEFINE_TYPE (MockInteraction, mock_interaction, G_TYPE_TLS_INTERACTION);
 
 static void
-on_cancellable_cancelled (GCancellable *cancellable,
-                          gpointer user_data)
-{
-  GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
-  GError *error = NULL;
-  if (!g_cancellable_set_error_if_cancelled (cancellable, &error))
-    g_assert_not_reached ();
-  g_simple_async_result_take_error (res, error);
-}
-
-static void
 mock_interaction_ask_password_async (GTlsInteraction    *interaction,
                                      GTlsPassword       *password,
                                      GCancellable       *cancellable,
@@ -47,19 +36,12 @@ mock_interaction_ask_password_async (GTlsInteraction    *interaction,
                                      gpointer            user_data)
 {
   MockInteraction *self = MOCK_INTERACTION (interaction);
-  GSimpleAsyncResult *res;
-
-  res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
-                                   mock_interaction_ask_password_async);
+  GTask *task;
 
-  if (cancellable)
-    g_cancellable_connect (cancellable,
-                           G_CALLBACK (on_cancellable_cancelled),
-                           g_object_ref (res),
-                           g_object_unref);
+  task = g_task_new (interaction, cancellable, callback, user_data);
 
   g_tls_password_set_value (password, (const guchar *)self->static_password, -1);
-  g_simple_async_result_complete_in_idle (res);
+  g_task_return_boolean (task, TRUE);
 }
 
 static GTlsInteractionResult
@@ -67,14 +49,16 @@ mock_interaction_ask_password_finish (GTlsInteraction    *interaction,
                                       GAsyncResult       *result,
                                       GError            **error)
 {
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
-                                                        mock_interaction_ask_password_async),
-                                                        G_TLS_INTERACTION_UNHANDLED);
-
-  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
-    return G_TLS_INTERACTION_FAILED;
-
-  return G_TLS_INTERACTION_HANDLED;
+  g_return_val_if_fail (g_task_is_valid (result, interaction),
+			G_TLS_INTERACTION_UNHANDLED);
+
+  if (g_task_had_error (G_TASK (result)))
+    {
+      g_task_propagate_boolean (G_TASK (result), error);
+      return G_TLS_INTERACTION_FAILED;
+    }
+  else
+    return G_TLS_INTERACTION_HANDLED;
 }
 
 static GTlsInteractionResult



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]