[PATCH 1/2] Use GTask instead of GSimpleAsyncResult

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

 



Instead of using GSimpleAsyncResult, use the new GTask API, which is
much more straightforward.
For using the new GTask API, let's bump GLib depency version to 2.36,
what is safe based on major distro support:
- Debian Jessie: glib-2.42
- RHEL-7.1: glib-2.40
- SLES12: glib-2.38
- Ubuntu LTS 14.04: glib-2.40
---
 configure.ac                   |  1 +
 osinfo/osinfo_install_script.c | 46 +++++++++++++++++-------------------------
 osinfo/osinfo_media.c          | 32 +++++++++++------------------
 osinfo/osinfo_tree.c           | 26 +++++++++---------------
 4 files changed, 41 insertions(+), 64 deletions(-)

diff --git a/configure.ac b/configure.ac
index 4154134..ff3eed3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -38,6 +38,7 @@ m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
 
 PKG_CHECK_MODULES([GOBJECT], [gobject-2.0])
 PKG_CHECK_MODULES([GIO], [gio-2.0])
+PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.36])
 PKG_CHECK_MODULES([SOUP], [libsoup-2.4 >= 2.42])
 PKG_CHECK_MODULES([LIBXML], [libxml-2.0 >= 2.6.0])
 PKG_CHECK_MODULES([LIBXSLT], [libxslt >= 1.0.0])
diff --git a/osinfo/osinfo_install_script.c b/osinfo/osinfo_install_script.c
index 9ded116..6e2e581 100644
--- a/osinfo/osinfo_install_script.c
+++ b/osinfo/osinfo_install_script.c
@@ -531,7 +531,7 @@ OsinfoAvatarFormat *osinfo_install_script_get_avatar_format(OsinfoInstallScript
 }
 
 struct _OsinfoInstallScriptGenerateData {
-    GSimpleAsyncResult *res;
+    GTask *res;
     OsinfoOs *os;
     OsinfoMedia *media;
     OsinfoInstallConfig *config;
@@ -551,7 +551,7 @@ static void osinfo_install_script_generate_data_free(OsinfoInstallScriptGenerate
 }
 
 struct _OsinfoInstallScriptGenerateOutputData {
-    GSimpleAsyncResult *res;
+    GTask *res;
     GCancellable *cancellable;
     GError *error;
     GFile *file;
@@ -882,7 +882,7 @@ static void osinfo_install_script_template_loaded(GObject *src,
                                      NULL,
                                      &error)) {
         g_prefix_error(&error, _("Failed to load script template %s: "), uri);
-        g_simple_async_result_take_error(data->res, error);
+        g_task_return_error(data->res, error);
         goto cleanup;
     }
 
@@ -897,15 +897,13 @@ static void osinfo_install_script_template_loaded(GObject *src,
                                               data->config,
                                               &error)) {
         g_prefix_error(&error, _("Failed to apply script template %s: "), uri);
-        g_simple_async_result_take_error(data->res, error);
+        g_task_return_error(data->res, error);
         goto cleanup;
     }
 
-    g_simple_async_result_set_op_res_gpointer(data->res,
-                                              output, NULL);
+    g_task_return_pointer(data->res, output, NULL);
 
  cleanup:
-    g_simple_async_result_complete(data->res);
     osinfo_install_script_generate_data_free(data);
     g_free(uri);
 }
@@ -934,10 +932,10 @@ static void osinfo_install_script_generate_async_common(OsinfoInstallScript *scr
         data->media = g_object_ref(media);
     data->config = g_object_ref(config);
     data->script = g_object_ref(script);
-    data->res = g_simple_async_result_new(G_OBJECT(script),
-                                          callback,
-                                          user_data,
-                                          osinfo_install_script_generate_async_common);
+    data->res = g_task_new(G_OBJECT(script),
+                           cancellable,
+                           callback,
+                           user_data);
 
     if (templateData) {
         GError *error = NULL;
@@ -952,14 +950,11 @@ static void osinfo_install_script_generate_async_common(OsinfoInstallScript *scr
                                                   data->config,
                                                   &error)) {
             g_prefix_error(&error, "%s", _("Failed to apply script template: "));
-            g_simple_async_result_take_error(data->res, error);
-            g_simple_async_result_complete(data->res);
+            g_task_return_error(data->res, error);
             osinfo_install_script_generate_data_free(data);
             return;
         }
-        g_simple_async_result_set_op_res_gpointer(data->res,
-                                                  output, NULL);
-        g_simple_async_result_complete_in_idle(data->res);
+        g_task_return_pointer(data->res, output, NULL);
         osinfo_install_script_generate_data_free(data);
     } else {
         GFile *file = g_file_new_for_uri(templateUri);
@@ -1007,14 +1002,11 @@ static gpointer osinfo_install_script_generate_finish_common(OsinfoInstallScript
                                                              GAsyncResult *res,
                                                              GError **error)
 {
-    GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res);
+    GTask *task = G_TASK(res);
 
     g_return_val_if_fail(error == NULL || *error == NULL, NULL);
 
-    if (g_simple_async_result_propagate_error(simple, error))
-        return NULL;
-
-    return g_simple_async_result_get_op_res_gpointer(simple);
+    return g_task_propagate_pointer(task, error);
 }
 
 /**
@@ -1132,9 +1124,7 @@ static void osinfo_install_script_generate_output_close_file(GObject *src,
                                  res,
                                  &data->error);
 
-    g_simple_async_result_set_op_res_gpointer(data->res,
-                                              data->file, NULL);
-    g_simple_async_result_complete_in_idle(data->res);
+    g_task_return_pointer(data->res, data->file, NULL);
 
     osinfo_install_script_generate_output_data_free(data);
 }
@@ -1338,10 +1328,10 @@ static void osinfo_install_script_generate_output_async_common(OsinfoInstallScri
 
     OsinfoInstallScriptGenerateSyncData *data_sync = user_data;
 
-    data->res = g_simple_async_result_new(G_OBJECT(script),
-                                          callback,
-                                          user_data,
-                                          osinfo_install_script_generate_output_async_common);
+    data->res = g_task_new(G_OBJECT(script),
+                           cancellable,
+                           callback,
+                           user_data);
 
     data->cancellable = cancellable;
     data->error = data_sync->error;
diff --git a/osinfo/osinfo_media.c b/osinfo/osinfo_media.c
index 7ff48a9..6c4ade0 100644
--- a/osinfo/osinfo_media.c
+++ b/osinfo/osinfo_media.c
@@ -78,7 +78,7 @@ struct _CreateFromLocationAsyncData {
     gint priority;
     GCancellable *cancellable;
 
-    GSimpleAsyncResult *res;
+    GTask *res;
 
     PrimaryVolumeDescriptor pvd;
     SupplementaryVolumeDescriptor svd;
@@ -760,10 +760,9 @@ static void on_svd_read(GObject *source,
 
 EXIT:
     if (error != NULL)
-        g_simple_async_result_take_error(data->res, error);
+        g_task_return_error(data->res, error);
     else
-        g_simple_async_result_set_op_res_gpointer(data->res, media, NULL);
-    g_simple_async_result_complete(data->res);
+        g_task_return_pointer(data->res, media, NULL);
 
     g_object_unref(stream);
     create_from_location_async_data_free(data);
@@ -834,8 +833,7 @@ static void on_pvd_read(GObject *source,
     return;
 
 ON_ERROR:
-    g_simple_async_result_take_error(data->res, error);
-    g_simple_async_result_complete(data->res);
+    g_task_return_error(data->res, error);
     create_from_location_async_data_free(data);
 }
 
@@ -857,8 +855,7 @@ static void on_location_skipped(GObject *source,
                         OSINFO_MEDIA_ERROR,
                         OSINFO_MEDIA_ERROR_NO_DESCRIPTORS,
                         _("No volume descriptors"));
-        g_simple_async_result_take_error(data->res, error);
-        g_simple_async_result_complete(data->res);
+        g_task_return_error(data->res, error);
         create_from_location_async_data_free(data);
 
         return;
@@ -889,8 +886,7 @@ static void on_location_read(GObject *source,
     stream = g_file_read_finish(G_FILE(source), res, &error);
     if (error != NULL) {
         g_prefix_error(&error, _("Failed to open file"));
-        g_simple_async_result_take_error(data->res, error);
-        g_simple_async_result_complete(data->res);
+        g_task_return_error(data->res, error);
         create_from_location_async_data_free(data);
 
         return;
@@ -925,11 +921,10 @@ void osinfo_media_create_from_location_async(const gchar *location,
     g_return_if_fail(location != NULL);
 
     data = g_slice_new0(CreateFromLocationAsyncData);
-    data->res = g_simple_async_result_new
-                                (NULL,
-                                 callback,
-                                 user_data,
-                                 osinfo_media_create_from_location_async);
+    data->res = g_task_new(NULL,
+                           cancellable,
+                           callback,
+                           user_data);
     data->file = g_file_new_for_commandline_arg(location);
     data->priority = priority;
     data->cancellable = cancellable;
@@ -953,14 +948,11 @@ void osinfo_media_create_from_location_async(const gchar *location,
 OsinfoMedia *osinfo_media_create_from_location_finish(GAsyncResult *res,
                                                       GError **error)
 {
-    GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res);
+    GTask *task = G_TASK(res);
 
     g_return_val_if_fail(error == NULL || *error == NULL, NULL);
 
-    if (g_simple_async_result_propagate_error(simple, error))
-        return NULL;
-
-    return g_simple_async_result_get_op_res_gpointer(simple);
+    return g_task_propagate_pointer(task, error);
 }
 
 /**
diff --git a/osinfo/osinfo_tree.c b/osinfo/osinfo_tree.c
index 55c572e..a608edc 100644
--- a/osinfo/osinfo_tree.c
+++ b/osinfo/osinfo_tree.c
@@ -39,7 +39,7 @@ struct _CreateFromLocationAsyncData {
     gint priority;
     GCancellable *cancellable;
 
-    GSimpleAsyncResult *res;
+    GTask *res;
 
     OsinfoTree *tree;
 };
@@ -604,8 +604,7 @@ static void on_location_read(GObject *source,
                                      NULL,
                                      &error)) {
         g_prefix_error(&error, _("Failed to load .treeinfo file: "));
-        g_simple_async_result_take_error(data->res, error);
-        g_simple_async_result_complete(data->res);
+        g_task_return_error(data->res, error);
         create_from_location_async_data_free(data);
         return;
     }
@@ -615,14 +614,13 @@ static void on_location_read(GObject *source,
                              length,
                              &error))) {
         g_prefix_error(&error, _("Failed to process keyinfo file: "));
-        g_simple_async_result_take_error(data->res, error);
+        g_task_return_error(data->res, error);
         goto cleanup;
     }
 
-    g_simple_async_result_set_op_res_gpointer(data->res, ret, NULL);
+    g_task_return_pointer(data->res, ret, NULL);
 
  cleanup:
-    g_simple_async_result_complete(data->res);
     create_from_location_async_data_free(data);
     g_free(content);
 }
@@ -651,11 +649,10 @@ void osinfo_tree_create_from_location_async(const gchar *location,
     treeinfo = g_strdup_printf("%s/.treeinfo", location);
 
     data = g_slice_new0(CreateFromLocationAsyncData);
-    data->res = g_simple_async_result_new
-        (NULL,
-         callback,
-         user_data,
-         osinfo_tree_create_from_location_async);
+    data->res = g_task_new(NULL,
+                           cancellable,
+                           callback,
+                           user_data);
     data->file = g_file_new_for_uri(treeinfo);
     data->location = g_strdup(location);
     data->priority = priority;
@@ -685,14 +682,11 @@ void osinfo_tree_create_from_location_async(const gchar *location,
 OsinfoTree *osinfo_tree_create_from_location_finish(GAsyncResult *res,
                                                     GError **error)
 {
-    GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res);
+    GTask *task = G_TASK(res);
 
     g_return_val_if_fail(error == NULL || *error == NULL, NULL);
 
-    if (g_simple_async_result_propagate_error(simple, error))
-        return NULL;
-
-    return g_simple_async_result_get_op_res_gpointer(simple);
+    return g_task_propagate_pointer(task, error);
 }
 
 /**
-- 
2.5.0

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux