[PATCH 2/8] libqmi-glib, device: port initable_init_async to use GTask
Ben Chan
benchan at chromium.org
Wed Apr 12 20:15:04 UTC 2017
---
src/libqmi-glib/qmi-device.c | 101 ++++++++++++++++++-------------------------
1 file changed, 41 insertions(+), 60 deletions(-)
diff --git a/src/libqmi-glib/qmi-device.c b/src/libqmi-glib/qmi-device.c
index a3dee23..cf2940a 100644
--- a/src/libqmi-glib/qmi-device.c
+++ b/src/libqmi-glib/qmi-device.c
@@ -2800,29 +2800,12 @@ qmi_device_new (GFile *file,
/*****************************************************************************/
/* Async init */
-typedef struct {
- QmiDevice *self;
- GSimpleAsyncResult *result;
- GCancellable *cancellable;
-} InitContext;
-
-static void
-init_context_complete_and_free (InitContext *ctx)
-{
- g_simple_async_result_complete_in_idle (ctx->result);
- if (ctx->cancellable)
- g_object_unref (ctx->cancellable);
- g_object_unref (ctx->result);
- g_object_unref (ctx->self);
- g_slice_free (InitContext, ctx);
-}
-
static gboolean
initable_init_finish (GAsyncInitable *initable,
GAsyncResult *result,
GError **error)
{
- return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error);
+ return g_task_propagate_boolean (G_TASK (result), error);
}
static void
@@ -2835,39 +2818,42 @@ sync_indication_cb (QmiClientCtl *client_ctl,
}
static void
-client_ctl_setup (InitContext *ctx)
+client_ctl_setup (GTask *task)
{
+ QmiDevice *self;
GError *error = NULL;
+ self = g_task_get_source_object (task);
+
/* Create the implicit CTL client */
- ctx->self->priv->client_ctl = g_object_new (QMI_TYPE_CLIENT_CTL,
- QMI_CLIENT_DEVICE, ctx->self,
- QMI_CLIENT_SERVICE, QMI_SERVICE_CTL,
- QMI_CLIENT_CID, QMI_CID_NONE,
- NULL);
+ self->priv->client_ctl = g_object_new (QMI_TYPE_CLIENT_CTL,
+ QMI_CLIENT_DEVICE, self,
+ QMI_CLIENT_SERVICE, QMI_SERVICE_CTL,
+ QMI_CLIENT_CID, QMI_CID_NONE,
+ NULL);
/* Register the CTL client to get indications */
- register_client (ctx->self,
- QMI_CLIENT (ctx->self->priv->client_ctl),
+ register_client (self,
+ QMI_CLIENT (self->priv->client_ctl),
&error);
g_assert_no_error (error);
/* Connect to 'Sync' indications */
- ctx->self->priv->sync_indication_id =
- g_signal_connect (ctx->self->priv->client_ctl,
+ self->priv->sync_indication_id =
+ g_signal_connect (self->priv->client_ctl,
"sync",
G_CALLBACK (sync_indication_cb),
- ctx->self);
+ self);
/* Done we are */
- g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
- init_context_complete_and_free (ctx);
+ g_task_return_boolean (task, TRUE);
+ g_object_unref (task);
}
static void
query_info_async_ready (GFile *file,
GAsyncResult *res,
- InitContext *ctx)
+ GTask *task)
{
GError *error = NULL;
GFileInfo *info;
@@ -2876,24 +2862,24 @@ query_info_async_ready (GFile *file,
if (!info) {
g_prefix_error (&error,
"Couldn't query file info: ");
- g_simple_async_result_take_error (ctx->result, error);
- init_context_complete_and_free (ctx);
+ g_task_return_error (task, error);
+ g_object_unref (task);
return;
}
/* Our QMI device must be of SPECIAL type */
if (g_file_info_get_file_type (info) != G_FILE_TYPE_SPECIAL) {
- g_simple_async_result_set_error (ctx->result,
- QMI_CORE_ERROR,
- QMI_CORE_ERROR_FAILED,
- "Wrong file type");
- init_context_complete_and_free (ctx);
+ g_task_return_new_error (task,
+ QMI_CORE_ERROR,
+ QMI_CORE_ERROR_FAILED,
+ "Wrong file type");
+ g_object_unref (task);
return;
}
g_object_unref (info);
/* Go on with client CTL setup */
- client_ctl_setup (ctx);
+ client_ctl_setup (task);
}
static void
@@ -2903,43 +2889,38 @@ initable_init_async (GAsyncInitable *initable,
GAsyncReadyCallback callback,
gpointer user_data)
{
- InitContext *ctx;
+ QmiDevice *self;
+ GTask *task;
- ctx = g_slice_new0 (InitContext);
- ctx->self = g_object_ref (initable);
- if (cancellable)
- ctx->cancellable = g_object_ref (cancellable);
- ctx->result = g_simple_async_result_new (G_OBJECT (initable),
- callback,
- user_data,
- initable_init_async);
+ self = QMI_DEVICE (initable);
+ task = g_task_new (self, cancellable, callback, user_data);
/* We need a proper file to initialize */
- if (!ctx->self->priv->file) {
- g_simple_async_result_set_error (ctx->result,
- QMI_CORE_ERROR,
- QMI_CORE_ERROR_INVALID_ARGS,
- "Cannot initialize QMI device: No file given");
- init_context_complete_and_free (ctx);
+ if (!self->priv->file) {
+ g_task_return_new_error (task,
+ QMI_CORE_ERROR,
+ QMI_CORE_ERROR_INVALID_ARGS,
+ "Cannot initialize QMI device: No file given");
+ g_object_unref (task);
return;
}
/* If no file check requested, don't do it */
- if (ctx->self->priv->no_file_check) {
- client_ctl_setup (ctx);
+ if (self->priv->no_file_check) {
+ client_ctl_setup (task);
return;
}
/* Check the file type. Note that this is just a quick check to avoid
* creating QmiDevices pointing to a location already known not to be a QMI
* device. */
- g_file_query_info_async (ctx->self->priv->file,
+ g_file_query_info_async (self->priv->file,
G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_DEFAULT,
- ctx->cancellable,
+ cancellable,
(GAsyncReadyCallback)query_info_async_ready,
- ctx);
+ task);
}
/*****************************************************************************/
--
2.12.2.715.g7642488e1d-goog
More information about the libqmi-devel
mailing list