[libvirt] [libvirt-glib 1/2] Add async variant of gvir_domain_resume()

From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> --- libvirt-gobject/libvirt-gobject-domain.c | 56 ++++++++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-domain.h | 7 ++++ libvirt-gobject/libvirt-gobject.sym | 2 ++ 3 files changed, 65 insertions(+) diff --git a/libvirt-gobject/libvirt-gobject-domain.c b/libvirt-gobject/libvirt-gobject-domain.c index 861f713..d6d804d 100644 --- a/libvirt-gobject/libvirt-gobject-domain.c +++ b/libvirt-gobject/libvirt-gobject-domain.c @@ -446,6 +446,62 @@ gboolean gvir_domain_resume(GVirDomain *dom, return TRUE; } +static void +gvir_domain_resume_helper(GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable G_GNUC_UNUSED) +{ + GVirDomain *dom = GVIR_DOMAIN(object); + GError *err = NULL; + + if (!gvir_domain_resume(dom, &err)) + g_simple_async_result_take_error(res, err); +} + +/** + * gvir_domain_resume_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_resume. + */ +void gvir_domain_resume_async(GVirDomain *dom, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *res; + + g_return_if_fail(GVIR_IS_DOMAIN(dom)); + g_return_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable)); + + res = g_simple_async_result_new(G_OBJECT(dom), + callback, + user_data, + gvir_domain_resume_async); + g_simple_async_result_run_in_thread(res, + gvir_domain_resume_helper, + G_PRIORITY_DEFAULT, + cancellable); + g_object_unref(res); +} + +gboolean gvir_domain_resume_finish(GVirDomain *dom, + GAsyncResult *result, + GError **err) +{ + 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_resume_async), FALSE); + g_return_val_if_fail(err == NULL || *err == NULL, FALSE); + + if (g_simple_async_result_propagate_error(G_SIMPLE_ASYNC_RESULT(result), err)) + return FALSE; + + return TRUE; +} + /** * gvir_domain_stop: * @dom: the domain diff --git a/libvirt-gobject/libvirt-gobject-domain.h b/libvirt-gobject/libvirt-gobject-domain.h index c61a2f5..6f74a97 100644 --- a/libvirt-gobject/libvirt-gobject-domain.h +++ b/libvirt-gobject/libvirt-gobject-domain.h @@ -162,6 +162,13 @@ gboolean gvir_domain_start_finish(GVirDomain *dom, gboolean gvir_domain_resume(GVirDomain *dom, GError **err); +void gvir_domain_resume_async(GVirDomain *dom, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean gvir_domain_resume_finish(GVirDomain *dom, + GAsyncResult *result, + GError **err); gboolean gvir_domain_stop(GVirDomain *dom, guint flags, GError **err); diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index fe3de97..926381d 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -177,6 +177,8 @@ LIBVIRT_GOBJECT_0.0.9 { LIBVIRT_GOBJECT_0.1.1 { global: + gvir_domain_resume_async; + gvir_domain_resume_finish; gvir_domain_shutdown_flags_get_type; gvir_domain_xml_flags_get_type; } LIBVIRT_GOBJECT_0.0.9; -- 1.7.11.2

From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org> We create struct and a free function each time we need to pass a simple unsigned integer to an async function. Code can be simplified if we don't do that but rather use glib's API to convert to/from uint to pointer. This patch only changes this for one of these functions. I'll change other functions when/if there is a consensus on this.. --- libvirt-gobject/libvirt-gobject-domain.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/libvirt-gobject/libvirt-gobject-domain.c b/libvirt-gobject/libvirt-gobject-domain.c index d6d804d..0289915 100644 --- a/libvirt-gobject/libvirt-gobject-domain.c +++ b/libvirt-gobject/libvirt-gobject-domain.c @@ -345,27 +345,18 @@ gboolean gvir_domain_start(GVirDomain *dom, return TRUE; } -typedef struct { - guint flags; -} DomainStartData; - -static void domain_start_data_free(DomainStartData *data) -{ - g_slice_free(DomainStartData, data); -} - static void gvir_domain_start_helper(GSimpleAsyncResult *res, GObject *object, GCancellable *cancellable G_GNUC_UNUSED) { GVirDomain *dom = GVIR_DOMAIN(object); - DomainStartData *data; GError *err = NULL; + guint flags; - data = g_simple_async_result_get_op_res_gpointer(res); + flags = GPOINTER_TO_UINT(g_simple_async_result_get_op_res_gpointer(res)); - if (!gvir_domain_start(dom, data->flags, &err)) + if (!gvir_domain_start(dom, flags, &err)) g_simple_async_result_take_error(res, err); } @@ -386,19 +377,17 @@ void gvir_domain_start_async(GVirDomain *dom, gpointer user_data) { GSimpleAsyncResult *res; - DomainStartData *data; g_return_if_fail(GVIR_IS_DOMAIN(dom)); g_return_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable)); - data = g_slice_new0(DomainStartData); - data->flags = flags; - res = g_simple_async_result_new(G_OBJECT(dom), callback, user_data, gvir_domain_start_async); - g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)domain_start_data_free); + g_simple_async_result_set_op_res_gpointer (res, + GUINT_TO_POINTER(flags), + NULL); g_simple_async_result_run_in_thread(res, gvir_domain_start_helper, G_PRIORITY_DEFAULT, -- 1.7.11.2

On Mon, Jul 30, 2012 at 6:30 PM, Zeeshan Ali (Khattak) <zeeshanak@gnome.org> wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
We create struct and a free function each time we need to pass a simple unsigned integer to an async function. Code can be simplified if we don't do that but rather use glib's API to convert to/from uint to pointer.
This patch only changes this for one of these functions. I'll change other functions when/if there is a consensus on this..
Personally, I prefer consistency with the rest of the async functions, and I often found myself replacing GUINT_TO_POINTER "trick" with a real pointer to a structure that can be extended. -- Marc-André Lureau

On Mon, Jul 30, 2012 at 7:09 PM, Marc-André Lureau <marcandre.lureau@gmail.com> wrote:
On Mon, Jul 30, 2012 at 6:30 PM, Zeeshan Ali (Khattak) <zeeshanak@gnome.org> wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
We create struct and a free function each time we need to pass a simple unsigned integer to an async function. Code can be simplified if we don't do that but rather use glib's API to convert to/from uint to pointer.
This patch only changes this for one of these functions. I'll change other functions when/if there is a consensus on this..
Personally, I prefer consistency with the rest of the async functions,
Me too and thats why I said I'll change it everywhere if we agree this is better.
and I often found myself replacing GUINT_TO_POINTER "trick" with a real pointer to a structure that can be extended.
Although its a trick, its pretty safe to keep 32-bit integers as pointers (the other way around isn't true AFAIK). All this is internal so we can always switch back to structs if/when needed and its trivial. Keeping in mind that there isn't a very good chance of us needing more fields of most async calls. Another way to simply would to have just one struct/free function for functions that take only flags argument but that won't be as simple. I'd rather keep the code as simple as possible as long as its guaranteed to work. -- Regards, Zeeshan Ali (Khattak) FSF member#5124

On Tue, Jul 31, 2012 at 12:09 PM, Zeeshan Ali (Khattak) <zeeshanak@gnome.org> wrote:
Me too and thats why I said I'll change it everywhere if we agree this is better.
You won't be able to switch all the async function to use GUINT_TO_POINTER. -- Marc-André Lureau

On Tue, Jul 31, 2012 at 12:59 PM, Marc-André Lureau <marcandre.lureau@gmail.com> wrote:
On Tue, Jul 31, 2012 at 12:09 PM, Zeeshan Ali (Khattak) <zeeshanak@gnome.org> wrote:
Me too and thats why I said I'll change it everywhere if we agree this is better.
You won't be able to switch all the async function to use GUINT_TO_POINTER.
Ah, you really meant *all all*. :) -- Regards, Zeeshan Ali (Khattak) FSF member#5124

On Mon, Jul 30, 2012 at 06:30:28PM +0200, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
We create struct and a free function each time we need to pass a simple unsigned integer to an async function. Code can be simplified if we don't do that but rather use glib's API to convert to/from uint to pointer.
If I was writing such code, I'd probably go with GUINT_TO_POINTER, but since it's written with a struct, and it's more consistent with other async code, I'd keep it this way as it makes things more predictable and readable. But if you really want to make this change, this is fine with me. Christophe

On Mon, Jul 30, 2012 at 06:30:27PM +0200, Zeeshan Ali (Khattak) wrote:
From: "Zeeshan Ali (Khattak)" <zeeshanak@gnome.org>
--- libvirt-gobject/libvirt-gobject-domain.c | 56 ++++++++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-domain.h | 7 ++++ libvirt-gobject/libvirt-gobject.sym | 2 ++ 3 files changed, 65 insertions(+)
diff --git a/libvirt-gobject/libvirt-gobject-domain.c b/libvirt-gobject/libvirt-gobject-domain.c index 861f713..d6d804d 100644 --- a/libvirt-gobject/libvirt-gobject-domain.c +++ b/libvirt-gobject/libvirt-gobject-domain.c @@ -446,6 +446,62 @@ gboolean gvir_domain_resume(GVirDomain *dom, return TRUE; }
+static void +gvir_domain_resume_helper(GSimpleAsyncResult *res, + GObject *object, + GCancellable *cancellable G_GNUC_UNUSED) +{ + GVirDomain *dom = GVIR_DOMAIN(object); + GError *err = NULL; + + if (!gvir_domain_resume(dom, &err)) + g_simple_async_result_take_error(res, err); +} + +/** + * gvir_domain_resume_async: + * @dom: the domain
Maybe "the domain to resume" ACK. Christophe
participants (3)
-
Christophe Fergeau
-
Marc-André Lureau
-
Zeeshan Ali (Khattak)