[libvirt] [libvirt-glib] Add async variant of gvir_domain_get_info()

From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> --- libvirt-gobject/libvirt-gobject-domain.c | 74 ++++++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-domain.h | 7 +++ libvirt-gobject/libvirt-gobject.sym | 2 + 3 files changed, 83 insertions(+), 0 deletions(-) diff --git a/libvirt-gobject/libvirt-gobject-domain.c b/libvirt-gobject/libvirt-gobject-domain.c index 0bafa7e..896aae1 100644 --- a/libvirt-gobject/libvirt-gobject-domain.c +++ b/libvirt-gobject/libvirt-gobject-domain.c @@ -572,6 +572,80 @@ GVirDomainInfo *gvir_domain_get_info(GVirDomain *dom, return ret; } +static void +gvir_domain_get_info_helper(GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable G_GNUC_UNUSED) +{ + GVirDomain *dom = GVIR_DOMAIN(object); + GVirDomainInfo *info; + GError *err = NULL; + + info = gvir_domain_get_info(dom, &err); + if (err) + g_simple_async_result_take_error(res, err); + else + g_simple_async_result_set_op_res_gpointer(res, info, NULL); +} + +/** + * gir_domain_get_info_async: + * @dom: the domain + * @cancellable: (allow-none)(transfer none): cancellation object + * @callback: (scope async): completion callback + * @user_data: (closure): opaque data for callback + * + * Asynchronous variant of #gvir_domain_get_info. + */ +void gvir_domain_get_info_async(GVirDomain *dom, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *res; + + g_return_if_fail(GVIR_IS_DOMAIN(dom)); + + res = g_simple_async_result_new(G_OBJECT(dom), + callback, + user_data, + gvir_domain_get_info_async); + g_simple_async_result_run_in_thread(res, + gvir_domain_get_info_helper, + G_PRIORITY_DEFAULT, + cancellable); + g_object_unref(res); +} + +/** + * gir_domain_get_info_finish: + * @dom: the domain + * @result: (transfer none): async method result + * @err: Place-holder for possible errors + * + * Finishes the operation started by #gvir_domain_get_info_async. + * + * Returns: (transfer full): the info + */ +GVirDomainInfo *gvir_domain_get_info_finish(GVirDomain *dom, + GAsyncResult *result, + GError **err) +{ + GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT(result); + + g_return_val_if_fail(GVIR_IS_DOMAIN(dom), FALSE); + g_return_val_if_fail + (g_simple_async_result_is_valid(result, + G_OBJECT(dom), + gvir_domain_get_info_async), + FALSE); + + if (g_simple_async_result_propagate_error(res, err)) + return NULL; + + return g_simple_async_result_get_op_res_gpointer(res); +} + /** * gvir_domain_screenshot: * @stream: stream to use as output diff --git a/libvirt-gobject/libvirt-gobject-domain.h b/libvirt-gobject/libvirt-gobject-domain.h index 8a4836e..677fbe6 100644 --- a/libvirt-gobject/libvirt-gobject-domain.h +++ b/libvirt-gobject/libvirt-gobject-domain.h @@ -141,6 +141,13 @@ gboolean gvir_domain_reboot(GVirDomain *dom, GVirDomainInfo *gvir_domain_get_info(GVirDomain *dom, GError **err); +void gvir_domain_get_info_async(GVirDomain *dom, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +GVirDomainInfo *gvir_domain_get_info_finish(GVirDomain *dom, + GAsyncResult *result, + GError **err); GVirConfigDomain *gvir_domain_get_config(GVirDomain *dom, guint flags, diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 64c91cc..00b32d4 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -70,6 +70,8 @@ LIBVIRT_GOBJECT_0.0.7 { gvir_domain_set_config; gvir_domain_get_devices; gvir_domain_get_info; + gvir_domain_get_info_async; + gvir_domain_get_info_finish; gvir_domain_get_persistent; gvir_domain_get_saved; gvir_domain_screenshot; -- 1.7.7.6

On Thu, Apr 19, 2012 at 2:12 AM, Zeeshan Ali (Khattak) <zeeshanak@gnome.org> wrote:
+/** + * gir_domain_get_info_async:
You have a couple of s/gir/gvir to do
+ * gir_domain_get_info_finish:
here too
+++ b/libvirt-gobject/libvirt-gobject.sym @@ -70,6 +70,8 @@ LIBVIRT_GOBJECT_0.0.7 { gvir_domain_set_config; gvir_domain_get_devices; gvir_domain_get_info; + gvir_domain_get_info_async; + gvir_domain_get_info_finish; gvir_domain_get_persistent; gvir_domain_get_saved; gvir_domain_screenshot;
Shouldn 't it fall in LIBVIRT_GOBJECT_0.0.8 ? looks good otherwise -- Marc-André Lureau

On Thu, Apr 19, 2012 at 03:12:01AM +0300, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
--- libvirt-gobject/libvirt-gobject-domain.c | 74 ++++++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-domain.h | 7 +++ libvirt-gobject/libvirt-gobject.sym | 2 + 3 files changed, 83 insertions(+), 0 deletions(-)
diff --git a/libvirt-gobject/libvirt-gobject-domain.c b/libvirt-gobject/libvirt-gobject-domain.c index 0bafa7e..896aae1 100644 --- a/libvirt-gobject/libvirt-gobject-domain.c +++ b/libvirt-gobject/libvirt-gobject-domain.c @@ -572,6 +572,80 @@ GVirDomainInfo *gvir_domain_get_info(GVirDomain *dom, return ret; }
+static void +gvir_domain_get_info_helper(GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable G_GNUC_UNUSED) +{ + GVirDomain *dom = GVIR_DOMAIN(object); + GVirDomainInfo *info; + GError *err = NULL; + + info = gvir_domain_get_info(dom, &err); + if (err) + g_simple_async_result_take_error(res, err); + else + g_simple_async_result_set_op_res_gpointer(res, info, NULL);
Shouldn't the last parameter be gvir_domain_info_free?
+} + +/** + * gir_domain_get_info_async: + * @dom: the domain + * @cancellable: (allow-none)(transfer none): cancellation object + * @callback: (scope async): completion callback + * @user_data: (closure): opaque data for callback + * + * Asynchronous variant of #gvir_domain_get_info. + */ +void gvir_domain_get_info_async(GVirDomain *dom, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *res; + + g_return_if_fail(GVIR_IS_DOMAIN(dom)); + + res = g_simple_async_result_new(G_OBJECT(dom), + callback, + user_data, + gvir_domain_get_info_async); + g_simple_async_result_run_in_thread(res, + gvir_domain_get_info_helper, + G_PRIORITY_DEFAULT, + cancellable); + g_object_unref(res); +} + +/** + * gir_domain_get_info_finish: + * @dom: the domain + * @result: (transfer none): async method result + * @err: Place-holder for possible errors + * + * Finishes the operation started by #gvir_domain_get_info_async. + * + * Returns: (transfer full): the info + */ +GVirDomainInfo *gvir_domain_get_info_finish(GVirDomain *dom, + GAsyncResult *result, + GError **err) +{ + GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT(result); + + g_return_val_if_fail(GVIR_IS_DOMAIN(dom), FALSE);
FALSE -> NULL
+ g_return_val_if_fail + (g_simple_async_result_is_valid(result, + G_OBJECT(dom), + gvir_domain_get_info_async), + FALSE);
Same here
+ + if (g_simple_async_result_propagate_error(res, err)) + return NULL; + + return g_simple_async_result_get_op_res_gpointer(res); +} + /** * gvir_domain_screenshot: * @stream: stream to use as output diff --git a/libvirt-gobject/libvirt-gobject-domain.h b/libvirt-gobject/libvirt-gobject-domain.h index 8a4836e..677fbe6 100644 --- a/libvirt-gobject/libvirt-gobject-domain.h +++ b/libvirt-gobject/libvirt-gobject-domain.h @@ -141,6 +141,13 @@ gboolean gvir_domain_reboot(GVirDomain *dom,
GVirDomainInfo *gvir_domain_get_info(GVirDomain *dom, GError **err); +void gvir_domain_get_info_async(GVirDomain *dom, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +GVirDomainInfo *gvir_domain_get_info_finish(GVirDomain *dom, + GAsyncResult *result, + GError **err);
GVirConfigDomain *gvir_domain_get_config(GVirDomain *dom, guint flags, diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 64c91cc..00b32d4 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -70,6 +70,8 @@ LIBVIRT_GOBJECT_0.0.7 { gvir_domain_set_config; gvir_domain_get_devices; gvir_domain_get_info; + gvir_domain_get_info_async; + gvir_domain_get_info_finish; gvir_domain_get_persistent; gvir_domain_get_saved; gvir_domain_screenshot; -- 1.7.7.6
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On Thu, Apr 19, 2012 at 11:52 AM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Apr 19, 2012 at 03:12:01AM +0300, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> +static void +gvir_domain_get_info_helper(GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable G_GNUC_UNUSED) +{ + GVirDomain *dom = GVIR_DOMAIN(object); + GVirDomainInfo *info; + GError *err = NULL; + + info = gvir_domain_get_info(dom, &err); + if (err) + g_simple_async_result_take_error(res, err); + else + g_simple_async_result_set_op_res_gpointer(res, info, NULL);
Shouldn't the last parameter be gvir_domain_info_free?
No, we give this info to user in the finalize call:
+ + if (g_simple_async_result_propagate_error(res, err)) + return NULL; + + return g_simple_async_result_get_op_res_gpointer(res); +}
-- Regards, Zeeshan Ali (Khattak) FSF member#5124

On Thu, Apr 19, 2012 at 04:24:35PM +0300, Zeeshan Ali (Khattak) wrote:
On Thu, Apr 19, 2012 at 11:52 AM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Apr 19, 2012 at 03:12:01AM +0300, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> +static void +gvir_domain_get_info_helper(GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable G_GNUC_UNUSED) +{ + GVirDomain *dom = GVIR_DOMAIN(object); + GVirDomainInfo *info; + GError *err = NULL; + + info = gvir_domain_get_info(dom, &err); + if (err) + g_simple_async_result_take_error(res, err); + else + g_simple_async_result_set_op_res_gpointer(res, info, NULL);
Shouldn't the last parameter be gvir_domain_info_free?
No, we give this info to user in the finalize call:
Looking at GSimpleAsyncResult documentation and use, it seems customary to pass a GDestroyNotify function here, and to ref the returned object in the _finish function if needed. It seems it's perfectly valid to call gvir_domain_get_info_async() with a NULL callback, or not to call _finish if you are not interested in the result. If you don't pass a non-NULL GDestroyNotify to set_op_res_gpointer, I don't see how you can avoid a memory leak in these cases. Think also of an error (cancellation) occurring between the call to _helper and the call to finish. Christophe
participants (3)
-
Christophe Fergeau
-
Marc-André Lureau
-
Zeeshan Ali (Khattak)