[gvfs/wip/oholy/gtask: 37/56] client: Port GDaemonFileEnumerator to GTask



commit 002398b019bd57a51c1412d31e83d6d44a63d2af
Author: Ondrej Holy <oholy redhat com>
Date:   Mon Jun 13 11:57:13 2016 +0200

    client: Port GDaemonFileEnumerator to GTask
    
    GSimpleAsyncResult is deprecated in favour of GTask and should be replaced.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=747412

 client/gdaemonfileenumerator.c |  117 ++++++++++++++++------------------------
 1 files changed, 46 insertions(+), 71 deletions(-)
---
diff --git a/client/gdaemonfileenumerator.c b/client/gdaemonfileenumerator.c
index d056359..6eb3239 100644
--- a/client/gdaemonfileenumerator.c
+++ b/client/gdaemonfileenumerator.c
@@ -58,7 +58,7 @@ struct _GDaemonFileEnumerator
   int async_requested_files;
   gulong cancelled_tag;
   guint timeout_tag;
-  GSimpleAsyncResult *async_res;
+  GTask *task;
   GMainLoop *next_files_mainloop;
   GMainContext *next_files_context;
   GSource *next_files_sync_timeout_source;
@@ -362,34 +362,44 @@ add_metadata (GFileInfo *info,
   g_free (path);
 }
 
-static GCancellable *
-simple_async_result_get_cancellable (GSimpleAsyncResult *res)
+typedef struct {
+  GTask *task;
+  gpointer result;
+  GDestroyNotify notify;
+} IdleData;
+
+static gboolean
+_g_task_return_pointer_idle_cb (IdleData *data)
 {
-  return g_object_get_data (G_OBJECT (res), "file-enumerator-cancellable");
+  g_task_return_pointer (data->task, data->result, data->notify);
+  g_object_unref (data->task);
+  g_free (data);
+
+  return FALSE;
 }
 
 static void
-simple_async_result_set_cancellable (GSimpleAsyncResult *res,
-                                     GCancellable       *cancellable)
+_g_task_return_pointer_idle (GTask *task, gpointer result, GDestroyNotify notify)
 {
-  if (!cancellable)
-    return;
+  IdleData *data;
+
+  data = g_new0 (IdleData, 1);
+  data->task = g_object_ref (task);
+  data->result = result;
+  data->notify = notify;
 
-  g_object_set_data_full (G_OBJECT (res),
-                          "file-enumerator-cancellable",
-                          g_object_ref (cancellable),
-                          g_object_unref);
+  g_idle_add ((GSourceFunc )_g_task_return_pointer_idle_cb, data);
 }
 
 /* Called with infos lock held */
 static void
 trigger_async_done (GDaemonFileEnumerator *daemon, gboolean ok)
 {
-  GList *rest, *l;
-  
+  GList *rest, *l = NULL;
+
   if (daemon->cancelled_tag != 0)
     {
-      GCancellable *cancellable = simple_async_result_get_cancellable (daemon->async_res);
+      GCancellable *cancellable = g_task_get_cancellable (daemon->task);
 
       /* If ok, we're a normal callback on the main thread,
         ensure protection against a thread cancelling and
@@ -432,14 +442,11 @@ trigger_async_done (GDaemonFileEnumerator *daemon, gboolean ok)
       daemon->infos = rest;
 
       g_list_foreach (l, (GFunc)add_metadata, daemon);
-
-      g_simple_async_result_set_op_res_gpointer (daemon->async_res,
-                                                l,
-                                                (GDestroyNotify)free_info_list);
     }
 
-  g_simple_async_result_complete_in_idle (daemon->async_res);
-  
+  /* Result has to be returned in idle in order to avoid deadlock */
+  _g_task_return_pointer_idle (daemon->task, l, (GDestroyNotify)free_info_list);
+
   daemon->cancelled_tag = 0;
 
   if (daemon->timeout_tag != 0)
@@ -447,9 +454,8 @@ trigger_async_done (GDaemonFileEnumerator *daemon, gboolean ok)
   daemon->timeout_tag = 0;
   
   daemon->async_requested_files = 0;
-  
-  g_object_unref (daemon->async_res);
-  daemon->async_res = NULL;
+
+  g_clear_object (&daemon->task);
 }
 
 char  *
@@ -547,10 +553,6 @@ static void
 async_cancelled (GCancellable *cancellable,
                 GDaemonFileEnumerator *daemon)
 {
-  g_simple_async_result_set_error (daemon->async_res,
-                                  G_IO_ERROR,
-                                  G_IO_ERROR_CANCELLED,
-                                  _("Operation was cancelled"));
   G_LOCK (infos);
   trigger_async_done (daemon, FALSE);
   G_UNLOCK (infos);
@@ -576,6 +578,9 @@ g_daemon_file_enumerator_next_files_async (GFileEnumerator     *enumerator,
                                           gpointer             user_data)
 {
   GDaemonFileEnumerator *daemon = G_DAEMON_FILE_ENUMERATOR (enumerator);
+  GTask *task;
+
+  task = g_task_new (enumerator, cancellable, callback, user_data);
 
   if (daemon->sync_connection != NULL)
     {
@@ -585,21 +590,17 @@ g_daemon_file_enumerator_next_files_async (GFileEnumerator     *enumerator,
        * We could possibly pump it ourselves in this case, but i'm not sure
        * how much sense this makes, so we don't for now.
        */
-      g_simple_async_report_error_in_idle  (G_OBJECT (enumerator),
-                                           callback,
-                                           user_data,
-                                           G_IO_ERROR, G_IO_ERROR_FAILED,
-                                           "Can't do asynchronous next_files() on a file enumerator created 
synchronously");
+      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               "Can't do asynchronous next_files() on a file enumerator created 
synchronously");
+      g_object_unref (task);
       return;
     }
-  
+
   G_LOCK (infos);
   daemon->cancelled_tag = 0;
   daemon->timeout_tag = 0;
   daemon->async_requested_files = num_files;
-  daemon->async_res = g_simple_async_result_new (G_OBJECT (enumerator), callback, user_data,
-                                                g_daemon_file_enumerator_next_files_async);
-  simple_async_result_set_cancellable (daemon->async_res, cancellable);
+  daemon->task = task;
 
   /* Maybe we already have enough info to fulfill the requeust already */
   if (daemon->done ||
@@ -623,23 +624,9 @@ g_daemon_file_enumerator_next_files_finish (GFileEnumerator  *enumerator,
                                            GAsyncResult     *res,
                                            GError          **error)
 {
-  GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (res);
-  GCancellable *cancellable;
-  GList *l;
-
-  cancellable = simple_async_result_get_cancellable (result);
-  if (g_cancellable_is_cancelled (cancellable))
-    {
-      g_set_error (error,
-                   G_IO_ERROR,
-                   G_IO_ERROR_CANCELLED,
-                   "%s", _("Operation was cancelled"));
-      return NULL;
-    }
+  g_return_val_if_fail (g_task_is_valid (res, enumerator), NULL);
 
-  l = g_simple_async_result_get_op_res_gpointer (result);
-  g_list_foreach (l, (GFunc)g_object_ref, NULL);
-  return g_list_copy (l);
+  return g_task_propagate_pointer (G_TASK (res), error);
 }
 
 static gboolean
@@ -662,13 +649,11 @@ g_daemon_file_enumerator_close_async (GFileEnumerator      *enumerator,
                                      GAsyncReadyCallback   callback,
                                      gpointer              user_data)
 {
-  GSimpleAsyncResult *res;
+  GTask *task;
 
-  res = g_simple_async_result_new (G_OBJECT (enumerator), callback, user_data,
-                                  g_daemon_file_enumerator_close_async);
-  simple_async_result_set_cancellable (res, cancellable);
-  g_simple_async_result_complete_in_idle (res);
-  g_object_unref (res);
+  task = g_task_new (enumerator, cancellable, callback, user_data);
+  g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
 }
 
 static gboolean
@@ -676,17 +661,7 @@ g_daemon_file_enumerator_close_finish (GFileEnumerator      *enumerator,
                                       GAsyncResult         *result,
                                       GError              **error)
 {
-  GCancellable *cancellable;
-  
-  cancellable = simple_async_result_get_cancellable (G_SIMPLE_ASYNC_RESULT (result));
-  if (g_cancellable_is_cancelled (cancellable))
-    {
-      g_set_error (error,
-                   G_IO_ERROR,
-                   G_IO_ERROR_CANCELLED,
-                   "%s", _("Operation was cancelled"));
-      return FALSE;
-    }
+  g_return_val_if_fail (g_task_is_valid (result, enumerator), FALSE);
 
-  return TRUE;
+  return g_task_propagate_boolean (G_TASK (result), error);
 }


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