[PATCH 1/3] iface-modem-messaging: port mm_iface_modem_messaging_initialize to use GTask
Aleksander Morgado
aleksander at aleksander.es
Wed Jun 28 08:23:26 UTC 2017
On Tue, Jun 27, 2017 at 5:54 PM, Ben Chan <benchan at chromium.org> wrote:
> ---
> src/mm-iface-modem-messaging.c | 129 +++++++++++++++++++----------------------
> 1 file changed, 60 insertions(+), 69 deletions(-)
>
Pushed to git master, thanks!
> diff --git a/src/mm-iface-modem-messaging.c b/src/mm-iface-modem-messaging.c
> index 01a30f01..4a6604c6 100644
> --- a/src/mm-iface-modem-messaging.c
> +++ b/src/mm-iface-modem-messaging.c
> @@ -1050,7 +1050,7 @@ mm_iface_modem_messaging_enable (MMIfaceModemMessaging *self,
> /*****************************************************************************/
>
> typedef struct _InitializationContext InitializationContext;
> -static void interface_initialization_step (InitializationContext *ctx);
> +static void interface_initialization_step (GTask *task);
>
> typedef enum {
> INITIALIZATION_STEP_FIRST,
> @@ -1062,38 +1062,17 @@ typedef enum {
> } InitializationStep;
>
> struct _InitializationContext {
> - MMIfaceModemMessaging *self;
> MmGdbusModemMessaging *skeleton;
> - GCancellable *cancellable;
> - GSimpleAsyncResult *result;
> InitializationStep step;
> };
>
> static void
> -initialization_context_complete_and_free (InitializationContext *ctx)
> +initialization_context_free (InitializationContext *ctx)
> {
> - g_simple_async_result_complete_in_idle (ctx->result);
> - g_object_unref (ctx->self);
> - g_object_unref (ctx->result);
> - g_object_unref (ctx->cancellable);
> g_object_unref (ctx->skeleton);
> g_free (ctx);
> }
>
> -static gboolean
> -initialization_context_complete_and_free_if_cancelled (InitializationContext *ctx)
> -{
> - if (!g_cancellable_is_cancelled (ctx->cancellable))
> - return FALSE;
> -
> - g_simple_async_result_set_error (ctx->result,
> - MM_CORE_ERROR,
> - MM_CORE_ERROR_CANCELLED,
> - "Interface initialization cancelled");
> - initialization_context_complete_and_free (ctx);
> - return TRUE;
> -}
> -
> static void
> skip_unknown_storages (GArray *mem)
> {
> @@ -1112,11 +1091,13 @@ skip_unknown_storages (GArray *mem)
> static void
> load_supported_storages_ready (MMIfaceModemMessaging *self,
> GAsyncResult *res,
> - InitializationContext *ctx)
> + GTask *task)
> {
> + InitializationContext *ctx;
> StorageContext *storage_ctx;
> GError *error = NULL;
>
> + ctx = g_task_get_task_data (task);
> storage_ctx = get_storage_context (self);
> if (!MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->load_supported_storages_finish (
> self,
> @@ -1183,14 +1164,15 @@ load_supported_storages_ready (MMIfaceModemMessaging *self,
>
> /* Go on to next step */
> ctx->step++;
> - interface_initialization_step (ctx);
> + interface_initialization_step (task);
> }
>
> static void
> check_support_ready (MMIfaceModemMessaging *self,
> GAsyncResult *res,
> - InitializationContext *ctx)
> + GTask *task)
> {
> + InitializationContext *ctx;
> GError *error = NULL;
>
> if (!MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->check_support_finish (self,
> @@ -1209,15 +1191,17 @@ check_support_ready (MMIfaceModemMessaging *self,
> }
>
> /* Go on to next step */
> + ctx = g_task_get_task_data (task);
> ctx->step++;
> - interface_initialization_step (ctx);
> + interface_initialization_step (task);
> }
>
> static void
> init_current_storages_ready (MMIfaceModemMessaging *self,
> GAsyncResult *res,
> - InitializationContext *ctx)
> + GTask *task)
> {
> + InitializationContext *ctx;
> StorageContext *storage_ctx;
> GError *error = NULL;
>
> @@ -1233,16 +1217,25 @@ init_current_storages_ready (MMIfaceModemMessaging *self,
> }
>
> /* Go on to next step */
> + ctx = g_task_get_task_data (task);
> ctx->step++;
> - interface_initialization_step (ctx);
> + interface_initialization_step (task);
> }
>
> static void
> -interface_initialization_step (InitializationContext *ctx)
> +interface_initialization_step (GTask *task)
> {
> + MMIfaceModemMessaging *self;
> + InitializationContext *ctx;
> +
> /* Don't run new steps if we're cancelled */
> - if (initialization_context_complete_and_free_if_cancelled (ctx))
> + if (g_task_return_error_if_cancelled (task)) {
> + g_object_unref (task);
> return;
> + }
> +
> + self = g_task_get_source_object (task);
> + ctx = g_task_get_task_data (task);
>
> switch (ctx->step) {
> case INITIALIZATION_STEP_FIRST:
> @@ -1258,23 +1251,23 @@ interface_initialization_step (InitializationContext *ctx)
> ctx->step++;
>
> case INITIALIZATION_STEP_CHECK_SUPPORT:
> - if (!GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (ctx->self),
> + if (!GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (self),
> support_checked_quark))) {
> /* Set the checked flag so that we don't run it again */
> - g_object_set_qdata (G_OBJECT (ctx->self),
> + g_object_set_qdata (G_OBJECT (self),
> support_checked_quark,
> GUINT_TO_POINTER (TRUE));
> /* Initially, assume we don't support it */
> - g_object_set_qdata (G_OBJECT (ctx->self),
> + g_object_set_qdata (G_OBJECT (self),
> supported_quark,
> GUINT_TO_POINTER (FALSE));
>
> - if (MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->check_support &&
> - MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->check_support_finish) {
> - MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->check_support (
> - ctx->self,
> + if (MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->check_support &&
> + MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->check_support_finish) {
> + MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->check_support (
> + self,
> (GAsyncReadyCallback)check_support_ready,
> - ctx);
> + task);
> return;
> }
>
> @@ -1285,37 +1278,37 @@ interface_initialization_step (InitializationContext *ctx)
> ctx->step++;
>
> case INITIALIZATION_STEP_FAIL_IF_UNSUPPORTED:
> - if (!GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (ctx->self),
> + if (!GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (self),
> supported_quark))) {
> - g_simple_async_result_set_error (ctx->result,
> - MM_CORE_ERROR,
> - MM_CORE_ERROR_UNSUPPORTED,
> - "Messaging not supported");
> - initialization_context_complete_and_free (ctx);
> + g_task_return_new_error (task,
> + MM_CORE_ERROR,
> + MM_CORE_ERROR_UNSUPPORTED,
> + "Messaging not supported");
> + g_object_unref (task);
> return;
> }
> /* Fall down to next step */
> ctx->step++;
>
> case INITIALIZATION_STEP_LOAD_SUPPORTED_STORAGES:
> - if (MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->load_supported_storages &&
> - MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->load_supported_storages_finish) {
> - MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->load_supported_storages (
> - ctx->self,
> + if (MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->load_supported_storages &&
> + MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->load_supported_storages_finish) {
> + MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->load_supported_storages (
> + self,
> (GAsyncReadyCallback)load_supported_storages_ready,
> - ctx);
> + task);
> return;
> }
> /* Fall down to next step */
> ctx->step++;
>
> case INITIALIZATION_STEP_INIT_CURRENT_STORAGES:
> - if (MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->init_current_storages &&
> - MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->init_current_storages_finish) {
> - MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (ctx->self)->init_current_storages (
> - ctx->self,
> + if (MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->init_current_storages &&
> + MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->init_current_storages_finish) {
> + MM_IFACE_MODEM_MESSAGING_GET_INTERFACE (self)->init_current_storages (
> + self,
> (GAsyncReadyCallback)init_current_storages_ready,
> - ctx);
> + task);
> return;
> }
> /* Fall down to next step */
> @@ -1328,22 +1321,22 @@ interface_initialization_step (InitializationContext *ctx)
> g_signal_connect (ctx->skeleton,
> "handle-create",
> G_CALLBACK (handle_create),
> - ctx->self);
> + self);
> g_signal_connect (ctx->skeleton,
> "handle-delete",
> G_CALLBACK (handle_delete),
> - ctx->self);
> + self);
> g_signal_connect (ctx->skeleton,
> "handle-list",
> G_CALLBACK (handle_list),
> - ctx->self);
> + self);
>
> /* Finally, export the new interface */
> - mm_gdbus_object_skeleton_set_modem_messaging (MM_GDBUS_OBJECT_SKELETON (ctx->self),
> + mm_gdbus_object_skeleton_set_modem_messaging (MM_GDBUS_OBJECT_SKELETON (self),
> MM_GDBUS_MODEM_MESSAGING (ctx->skeleton));
>
> - g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
> - initialization_context_complete_and_free (ctx);
> + g_task_return_boolean (task, TRUE);
> + g_object_unref (task);
> return;
> }
>
> @@ -1355,7 +1348,7 @@ mm_iface_modem_messaging_initialize_finish (MMIfaceModemMessaging *self,
> GAsyncResult *res,
> GError **error)
> {
> - return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
> + return g_task_propagate_boolean (G_TASK (res), error);
> }
>
> void
> @@ -1366,6 +1359,7 @@ mm_iface_modem_messaging_initialize (MMIfaceModemMessaging *self,
> {
> InitializationContext *ctx;
> MmGdbusModemMessaging *skeleton = NULL;
> + GTask *task;
>
> /* Did we already create it? */
> g_object_get (self,
> @@ -1388,16 +1382,13 @@ mm_iface_modem_messaging_initialize (MMIfaceModemMessaging *self,
> /* Perform async initialization here */
>
> ctx = g_new0 (InitializationContext, 1);
> - ctx->self = g_object_ref (self);
> - ctx->cancellable = g_object_ref (cancellable);
> - ctx->result = g_simple_async_result_new (G_OBJECT (self),
> - callback,
> - user_data,
> - mm_iface_modem_messaging_initialize);
> ctx->step = INITIALIZATION_STEP_FIRST;
> ctx->skeleton = skeleton;
>
> - interface_initialization_step (ctx);
> + task = g_task_new (self, cancellable, callback, user_data);
> + g_task_set_task_data (task, ctx, (GDestroyNotify)initialization_context_free);
> +
> + interface_initialization_step (task);
> }
>
> void
> --
> 2.13.1
>
--
Aleksander
https://aleksander.es
More information about the ModemManager-devel
mailing list