[Spice-devel] [PATCH 12/14] win-usb-driver-install: Use GTask instead of GSimpleAsyncResult
Christophe Fergeau
cfergeau at redhat.com
Mon Jan 18 06:05:12 PST 2016
On Mon, Jan 18, 2016 at 10:05:48AM +0100, Fabiano FidĂȘncio wrote:
> Instead of using GSimpleAsyncResult, use the new GTask API, which is
> much more straightforward.
> ---
> src/win-usb-driver-install.c | 86 +++++++++++++++++++++-----------------------
> 1 file changed, 41 insertions(+), 45 deletions(-)
>
> diff --git a/src/win-usb-driver-install.c b/src/win-usb-driver-install.c
> index 54e9b14..a7c4864 100644
> --- a/src/win-usb-driver-install.c
> +++ b/src/win-usb-driver-install.c
> @@ -44,8 +44,7 @@
>
> struct _SpiceWinUsbDriverPrivate {
> USBClerkReply reply;
> - GSimpleAsyncResult *result;
> - GCancellable *cancellable;
> + GTask *task;
> HANDLE handle;
> SpiceUsbDevice *device;
> };
> @@ -144,16 +143,16 @@ void win_usb_driver_handle_reply_cb(GObject *gobject,
>
> if (err) {
> g_warning("failed to read reply from usbclerk (%s)", err->message);
> - g_simple_async_result_take_error(priv->result, err);
> + g_task_return_error(priv->task, err);
> goto failed_reply;
> }
>
> if (bytes == 0) {
> g_warning("unexpected EOF from usbclerk");
> - g_simple_async_result_set_error(priv->result,
> - SPICE_WIN_USB_DRIVER_ERROR,
> - SPICE_WIN_USB_DRIVER_ERROR_FAILED,
> - "unexpected EOF from usbclerk");
> + g_task_return_new_error(priv->task,
> + SPICE_WIN_USB_DRIVER_ERROR,
> + SPICE_WIN_USB_DRIVER_ERROR_FAILED,
> + "unexpected EOF from usbclerk");
> goto failed_reply;
> }
>
> @@ -167,52 +166,53 @@ void win_usb_driver_handle_reply_cb(GObject *gobject,
> if (priv->reply.hdr.magic != USB_CLERK_MAGIC) {
> g_warning("usbclerk magic mismatch: mine=0x%04x server=0x%04x",
> USB_CLERK_MAGIC, priv->reply.hdr.magic);
> - g_simple_async_result_set_error(priv->result,
> - SPICE_WIN_USB_DRIVER_ERROR,
> - SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> - "usbclerk magic mismatch");
> + g_task_return_new_error(priv->task,
> + SPICE_WIN_USB_DRIVER_ERROR,
> + SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> + "usbclerk magic mismatch");
> goto failed_reply;
> }
>
> if (priv->reply.hdr.version != USB_CLERK_VERSION) {
> g_warning("usbclerk version mismatch: mine=0x%04x server=0x%04x",
> USB_CLERK_VERSION, priv->reply.hdr.version);
> - g_simple_async_result_set_error(priv->result,
> - SPICE_WIN_USB_DRIVER_ERROR,
> - SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> - "usbclerk version mismatch");
> + g_task_return_new_error(priv->task,
> + SPICE_WIN_USB_DRIVER_ERROR,
> + SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> + "usbclerk version mismatch");
> }
>
> if (priv->reply.hdr.type != USB_CLERK_REPLY) {
> g_warning("usbclerk message with unexpected type %d",
> priv->reply.hdr.type);
> - g_simple_async_result_set_error(priv->result,
> - SPICE_WIN_USB_DRIVER_ERROR,
> - SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> - "usbclerk message with unexpected type");
> + g_task_return_new_error(priv->task,
> + SPICE_WIN_USB_DRIVER_ERROR,
> + SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> + "usbclerk message with unexpected type");
> goto failed_reply;
> }
>
> if (priv->reply.hdr.size != bytes) {
> g_warning("usbclerk message size mismatch: read %"G_GSSIZE_FORMAT" bytes hdr.size=%d",
> bytes, priv->reply.hdr.size);
> - g_simple_async_result_set_error(priv->result,
> - SPICE_WIN_USB_DRIVER_ERROR,
> - SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> - "usbclerk message with unexpected size");
> + g_task_return_new_error(priv->task,
> + SPICE_WIN_USB_DRIVER_ERROR,
> + SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> + "usbclerk message with unexpected size");
> goto failed_reply;
> }
>
> if (priv->reply.status == 0) {
> - g_simple_async_result_set_error(priv->result,
> - SPICE_WIN_USB_DRIVER_ERROR,
> - SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> - "usbclerk error reply");
> + g_task_return_new_error(priv->task,
> + SPICE_WIN_USB_DRIVER_ERROR,
> + SPICE_WIN_USB_DRIVER_ERROR_MESSAGE,
> + "usbclerk error reply");
> goto failed_reply;
> }
>
> + g_task_return_boolean (priv->task, TRUE);
> +
> failed_reply:
> - g_simple_async_result_complete_in_idle(priv->result);
> g_clear_object(&priv->result);
g_clear_object(&priv->task);
> }
>
> @@ -268,7 +268,8 @@ void spice_win_usb_driver_read_reply_async(SpiceWinUsbDriver *self)
> istream = g_win32_input_stream_new(priv->handle, FALSE);
>
> g_input_stream_read_async(istream, &priv->reply, sizeof(priv->reply),
> - G_PRIORITY_DEFAULT, priv->cancellable,
> + G_PRIORITY_DEFAULT,
> + g_task_get_cancellable(priv->task),
> win_usb_driver_handle_reply_cb, self);
> }
>
> @@ -299,7 +300,7 @@ void spice_win_usb_driver_op(SpiceWinUsbDriver *self,
> {
> guint16 vid, pid;
> GError *err = NULL;
> - GSimpleAsyncResult *result;
> + GTask *task;
> SpiceWinUsbDriverPrivate *priv;
>
> g_return_if_fail(SPICE_IS_WIN_USB_DRIVER(self));
> @@ -307,12 +308,11 @@ void spice_win_usb_driver_op(SpiceWinUsbDriver *self,
>
> priv = self->priv;
>
> - result = g_simple_async_result_new(G_OBJECT(self), callback, user_data,
> - spice_win_usb_driver_op);
> + task = g_task_new(self, cancellable, callback, user_data);
>
> - if (priv->result) { /* allow one install/uninstall request at a time */
> + if (priv->task) { /* allow one install/uninstall request at a time */
> g_warning("Another request exists -- try later");
> - g_simple_async_result_set_error(result,
> + g_task_return_new_error(result,
g_task_return_new_error(task,
> SPICE_WIN_USB_DRIVER_ERROR, SPICE_WIN_USB_DRIVER_ERROR_FAILED,
> "Another request exists -- try later");
> goto failed_request;
> @@ -325,21 +325,21 @@ void spice_win_usb_driver_op(SpiceWinUsbDriver *self,
> if (!spice_win_usb_driver_send_request(self, op_type,
> vid, pid, &err)) {
> g_warning("failed to send a request to usbclerk %s", err->message);
> - g_simple_async_result_take_error(result, err);
> + g_task_return_error(result, err);
g_task_return_error(task, err);
> goto failed_request;
> }
>
> /* set up for async read */
> - priv->result = result;
> + priv->task = task;
> priv->device = device;
> - priv->cancellable = cancellable;
> +
> + g_task_return_boolean(priv->task, TRUE);
I don't think you want to g_task_return here, it will be done at the end
of the spice_win_usb_driver_read_reply_async() call (through
spice_win_usb_driver_read_reply_async).
Christophe
>
> spice_win_usb_driver_read_reply_async(self);
>
> return;
>
> failed_request:
> - g_simple_async_result_complete_in_idle(result);
> g_clear_object(&result);
> }
>
> @@ -351,16 +351,12 @@ static gboolean
> spice_win_usb_driver_op_finish(SpiceWinUsbDriver *self,
> GAsyncResult *res, GError **err)
> {
> - GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT(res);
> + GTask *task = G_TASK(res);
>
> g_return_val_if_fail(SPICE_IS_WIN_USB_DRIVER(self), 0);
> - g_return_val_if_fail(g_simple_async_result_is_valid(res, G_OBJECT(self),
> - spice_win_usb_driver_op),
> - FALSE);
> - if (g_simple_async_result_propagate_error(result, err))
> - return FALSE;
> + g_return_val_if_fail(g_task_is_valid(task, self), FALSE);
>
> - return TRUE;
> + return g_task_propagate_boolean(task, err);
> }
>
> /**
> --
> 2.5.0
>
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/spice-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20160118/b6dff122/attachment.sig>
More information about the Spice-devel
mailing list