[Spice-devel] [PATCH v2 09/13] usb-acl-helper: Use GTask instead of GSimpleAsyncResult

Jonathon Jongsma jjongsma at redhat.com
Tue Feb 16 22:31:02 UTC 2016


On Fri, 2016-02-12 at 10:46 +0100, Fabiano FidĂȘncio wrote:
> Instead of using GSimpleAsyncResult, use the new GTask API, which is
> much more straightforward.
> ---
>  src/usb-acl-helper.c | 73 +++++++++++++++++++++++----------------------------
> -
>  1 file changed, 32 insertions(+), 41 deletions(-)
> 
> diff --git a/src/usb-acl-helper.c b/src/usb-acl-helper.c
> index 6a49627..d1fdfbe 100644
> --- a/src/usb-acl-helper.c
> +++ b/src/usb-acl-helper.c
> @@ -35,10 +35,9 @@
>      (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SPICE_TYPE_USB_ACL_HELPER,
> SpiceUsbAclHelperPrivate))
>  
>  struct _SpiceUsbAclHelperPrivate {
> -    GSimpleAsyncResult *result;
> +    GTask *task;
>      GIOChannel *in_ch;
>      GIOChannel *out_ch;
> -    GCancellable *cancellable;
>      gulong cancellable_id;
>  };
>  
> @@ -53,11 +52,11 @@ static void spice_usb_acl_helper_cleanup(SpiceUsbAclHelper
> *self)
>  {
>      SpiceUsbAclHelperPrivate *priv = self->priv;
>  
> -    g_cancellable_disconnect(priv->cancellable, priv->cancellable_id);
> -    priv->cancellable = NULL;
> +    g_cancellable_disconnect(g_task_get_cancellable(priv->task),
> +                             priv->cancellable_id);
>      priv->cancellable_id = 0;
>  
> -    g_clear_object(&priv->result);
> +    g_clear_object(&priv->task);
>  
>      if (priv->in_ch) {
>          g_io_channel_unref(priv->in_ch);
> @@ -90,9 +89,9 @@ static void
> spice_usb_acl_helper_class_init(SpiceUsbAclHelperClass *klass)
>  /* ------------------------------------------------------------------ */
>  /* callbacks                                                          */
>  
> -static void async_result_set_cancelled(GSimpleAsyncResult *result)
> +static void async_result_set_cancelled(GTask *task)
>  {
> -    g_simple_async_result_set_error(result,
> +    g_task_return_new_error(task,
>                  G_IO_ERROR, G_IO_ERROR_CANCELLED,
>                  "Setting USB device node ACL cancelled");
>  }
> @@ -105,12 +104,13 @@ static gboolean cb_out_watch(GIOChannel    *channel,
>      SpiceUsbAclHelperPrivate *priv = self->priv;
>      gboolean success = FALSE;
>      GError *err = NULL;
> +    GCancellable *cancellable;
>      GIOStatus status;
>      gchar *string;
>      gsize size;
>  
>      /* Check that we've not been cancelled */
> -    if (priv->result == NULL)
> +    if (priv->task == NULL)
>          goto done;
>  
>      g_return_val_if_fail(channel == priv->out_ch, FALSE);
> @@ -121,10 +121,11 @@ static gboolean cb_out_watch(GIOChannel    *channel,
>              string[strlen(string) - 1] = 0;
>              if (!strcmp(string, "SUCCESS")) {
>                  success = TRUE;
> +                g_task_return_boolean(priv->task, TRUE);
>              } else if (!strcmp(string, "CANCELED")) {
> -                async_result_set_cancelled(priv->result);
> +                async_result_set_cancelled(priv->task);
>              } else {
> -                g_simple_async_result_set_error(priv->result,
> +                g_task_return_new_error(priv->task,
>                              SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
>                              "Error setting USB device node ACL: '%s'",
>                              string);
> @@ -132,10 +133,10 @@ static gboolean cb_out_watch(GIOChannel    *channel,
>              g_free(string);
>              break;
>          case G_IO_STATUS_ERROR:
> -            g_simple_async_result_take_error(priv->result, err);
> +            g_task_return_error(priv->task, err);
>              break;
>          case G_IO_STATUS_EOF:
> -            g_simple_async_result_set_error(priv->result,
> +            g_task_return_new_error(priv->task,
>                          SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
>                          "Unexpected EOF reading from acl helper stdout");
>              break;
> @@ -143,16 +144,15 @@ static gboolean cb_out_watch(GIOChannel    *channel,
>              return TRUE; /* Wait for more input */
>      }
>  
> -    g_cancellable_disconnect(priv->cancellable, priv->cancellable_id);
> -    priv->cancellable = NULL;
> +    cancellable = g_task_get_cancellable(priv->task);
> +    g_cancellable_disconnect(cancellable, priv->cancellable_id);
>      priv->cancellable_id = 0;
>  
> -    g_simple_async_result_complete_in_idle(priv->result);
> -    g_clear_object(&priv->result);
> -
>      if (!success)
>          spice_usb_acl_helper_cleanup(self);
>  
> +    g_clear_object(&priv->task);
> +
>  done:
>      g_object_unref(self);
>      return FALSE;
> @@ -193,7 +193,7 @@ void spice_usb_acl_helper_open_acl(SpiceUsbAclHelper
> *self,
>      g_return_if_fail(SPICE_IS_USB_ACL_HELPER(self));
>  
>      SpiceUsbAclHelperPrivate *priv = self->priv;
> -    GSimpleAsyncResult *result;
> +    GTask *task;
>      GError *err = NULL;
>      GIOStatus status;
>      GPid helper_pid;
> @@ -202,25 +202,24 @@ void spice_usb_acl_helper_open_acl(SpiceUsbAclHelper
> *self,
>      gint in, out;
>      gchar buf[128];
>  
> -    result = g_simple_async_result_new(G_OBJECT(self), callback, user_data,
> -                                       spice_usb_acl_helper_open_acl);
> +    task = g_task_new(self, cancellable, callback, user_data);
>  
>      if (priv->out_ch) {
> -        g_simple_async_result_set_error(result,
> +        g_task_return_new_error(task,
>                              SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
>                              "Error acl-helper already has an acl open");
>          goto done;
>      }
>  
>      if (g_cancellable_set_error_if_cancelled(cancellable, &err)) {
> -        g_simple_async_result_take_error(result, err);
> +        g_task_return_error(task, err);
>          goto done;
>      }
>  
>      if (!g_spawn_async_with_pipes(NULL, argv, NULL,
>                             G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
>                             NULL, NULL, &helper_pid, &in, &out, NULL, &err)) {
> -        g_simple_async_result_take_error(result, err);
> +        g_task_return_error(task, err);
>          goto done;
>      }
>      g_child_watch_add(helper_pid, helper_child_watch_cb, NULL);
> @@ -232,7 +231,7 @@ void spice_usb_acl_helper_open_acl(SpiceUsbAclHelper
> *self,
>      g_io_channel_set_close_on_unref(priv->out_ch, TRUE);
>      status = g_io_channel_set_flags(priv->out_ch, G_IO_FLAG_NONBLOCK, &err);
>      if (status != G_IO_STATUS_NORMAL) {
> -        g_simple_async_result_take_error(result, err);
> +        g_task_return_error(task, err);
>          goto done;
>      }
>  
> @@ -240,18 +239,17 @@ void spice_usb_acl_helper_open_acl(SpiceUsbAclHelper
> *self,
>      status = g_io_channel_write_chars(priv->in_ch, buf, -1,
>                                        &bytes_written, &err);
>      if (status != G_IO_STATUS_NORMAL) {
> -        g_simple_async_result_take_error(result, err);
> +        g_task_return_error(task, err);
>          goto done;
>      }
>      status = g_io_channel_flush(priv->in_ch, &err);
>      if (status != G_IO_STATUS_NORMAL) {
> -        g_simple_async_result_take_error(result, err);
> +        g_task_return_error(task, err);
>          goto done;
>      }
>  
> -    priv->result = result;
> +    priv->task = task;
>      if (cancellable) {
> -        priv->cancellable = cancellable;
>          priv->cancellable_id = g_cancellable_connect(cancellable,
>                                                      
>  G_CALLBACK(cancelled_cb),
>                                                       self, NULL);
> @@ -262,24 +260,19 @@ void spice_usb_acl_helper_open_acl(SpiceUsbAclHelper
> *self,
>  
>  done:
>      spice_usb_acl_helper_cleanup(self);
> -    g_simple_async_result_complete_in_idle(result);
> -    g_object_unref(result);
> +    g_object_unref(task);
>  }
>  
>  G_GNUC_INTERNAL
>  gboolean spice_usb_acl_helper_open_acl_finish(
>      SpiceUsbAclHelper *self, GAsyncResult *res, GError **err)
>  {
> -    GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT(res);
> +    GTask *task = G_TASK(res);
>  
> -    g_return_val_if_fail(g_simple_async_result_is_valid(res, G_OBJECT(self),
> -                                              
>  spice_usb_acl_helper_open_acl),
> +    g_return_val_if_fail(g_task_is_valid(task, self),
>                           FALSE);
>  
> -    if (g_simple_async_result_propagate_error(result, err))
> -        return FALSE;
> -
> -    return TRUE;
> +    return g_task_propagate_boolean(task, err);
>  }
>  
>  G_GNUC_INTERNAL
> @@ -290,10 +283,8 @@ void spice_usb_acl_helper_close_acl(SpiceUsbAclHelper
> *self)
>      SpiceUsbAclHelperPrivate *priv = self->priv;
>  
>      /* If the acl open has not completed yet report it as cancelled */
> -    if (priv->result) {
> -        async_result_set_cancelled(priv->result);
> -        g_simple_async_result_complete_in_idle(priv->result);
> -    }
> +    if (priv->task)
> +        async_result_set_cancelled(priv->task);
>  
>      spice_usb_acl_helper_cleanup(self);
>  }


Acked-by: Jonathon Jongsma <jjongsma at redhat.com>


More information about the Spice-devel mailing list