[PATCH 1/5] iface-modem-cdma: port mm_iface_modem_cdma_initialize to use GTask

Aleksander Morgado aleksander at aleksander.es
Fri Jun 30 09:05:03 UTC 2017


On 29/06/17 13:46, Ben Chan wrote:
> ---
>  src/mm-iface-modem-cdma.c | 105 +++++++++++++++++++++-------------------------
>  1 file changed, 48 insertions(+), 57 deletions(-)
> 

Pushed to git master, thanks.

> diff --git a/src/mm-iface-modem-cdma.c b/src/mm-iface-modem-cdma.c
> index 771bffd7..6340b031 100644
> --- a/src/mm-iface-modem-cdma.c
> +++ b/src/mm-iface-modem-cdma.c
> @@ -1583,7 +1583,7 @@ mm_iface_modem_cdma_enable (MMIfaceModemCdma *self,
>  /*****************************************************************************/
>  
>  typedef struct _InitializationContext InitializationContext;
> -static void interface_initialization_step (InitializationContext *ctx);
> +static void interface_initialization_step (GTask *task);
>  
>  typedef enum {
>      INITIALIZATION_STEP_FIRST,
> @@ -1594,48 +1594,30 @@ typedef enum {
>  } InitializationStep;
>  
>  struct _InitializationContext {
> -    MMIfaceModemCdma *self;
>      MmGdbusModemCdma *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;
> -}
> -
>  #undef STR_REPLY_READY_FN
>  #define STR_REPLY_READY_FN(NAME,DISPLAY)                                \
>      static void                                                         \
>      load_##NAME##_ready (MMIfaceModemCdma *self,                        \
>                           GAsyncResult *res,                             \
> -                         InitializationContext *ctx)                    \
> +                         GTask *task)                                   \
>      {                                                                   \
> +        InitializationContext *ctx;                                     \
>          GError *error = NULL;                                           \
>          gchar *val;                                                     \
>                                                                          \
> +        ctx = g_task_get_task_data (task);                              \
> +                                                                        \
>          val = MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_##NAME##_finish (self, res, &error); \
>          mm_gdbus_modem_cdma_set_##NAME (ctx->skeleton, val);            \
>          g_free (val);                                                   \
> @@ -1647,7 +1629,7 @@ initialization_context_complete_and_free_if_cancelled (InitializationContext *ct
>                                                                          \
>          /* Go on to next step */                                        \
>          ctx->step++;                                                    \
> -        interface_initialization_step (ctx);                            \
> +        interface_initialization_step (task);                           \
>      }
>  
>  STR_REPLY_READY_FN (meid, "MEID")
> @@ -1656,11 +1638,14 @@ STR_REPLY_READY_FN (esn, "ESN")
>  static void
>  load_activation_state_ready (MMIfaceModemCdma *self,
>                               GAsyncResult *res,
> -                             InitializationContext *ctx)
> +                             GTask *task)
>  {
> +    InitializationContext *ctx;
>      GError *error = NULL;
>      MMModemCdmaActivationState state;
>  
> +    ctx = g_task_get_task_data (task);
> +
>      state = MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_activation_state_finish (self, res, &error);
>      mm_gdbus_modem_cdma_set_activation_state (ctx->skeleton, state);
>  
> @@ -1671,15 +1656,23 @@ load_activation_state_ready (MMIfaceModemCdma *self,
>  
>      /* Go on to next step */
>      ctx->step++;
> -    interface_initialization_step (ctx);
> +    interface_initialization_step (task);
>  }
>  
>  static void
> -interface_initialization_step (InitializationContext *ctx)
> +interface_initialization_step (GTask *task)
>  {
> +    MMIfaceModemCdma *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:
> @@ -1691,12 +1684,12 @@ interface_initialization_step (InitializationContext *ctx)
>           * lifetime of the modem. Therefore, if we already have it loaded,
>           * don't try to load it again. */
>          if (!mm_gdbus_modem_cdma_get_meid (ctx->skeleton) &&
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_meid &&
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_meid_finish) {
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_meid (
> -                ctx->self,
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_meid &&
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_meid_finish) {
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_meid (
> +                self,
>                  (GAsyncReadyCallback)load_meid_ready,
> -                ctx);
> +                task);
>              return;
>          }
>          /* Fall down to next step */
> @@ -1707,12 +1700,12 @@ interface_initialization_step (InitializationContext *ctx)
>           * lifetime of the modem. Therefore, if we already have it loaded,
>           * don't try to load it again. */
>          if (!mm_gdbus_modem_cdma_get_esn (ctx->skeleton) &&
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_esn &&
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_esn_finish) {
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_esn (
> -                ctx->self,
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_esn &&
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_esn_finish) {
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_esn (
> +                self,
>                  (GAsyncReadyCallback)load_esn_ready,
> -                ctx);
> +                task);
>              return;
>          }
>          /* Fall down to next step */
> @@ -1723,12 +1716,12 @@ interface_initialization_step (InitializationContext *ctx)
>           * whole lifetime of the modem. Therefore, if we already have it loaded,
>           * don't try to load it again. */
>          if (mm_gdbus_modem_cdma_get_activation_state (ctx->skeleton) == MM_MODEM_CDMA_ACTIVATION_STATE_UNKNOWN &&
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_activation_state &&
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_activation_state_finish) {
> -            MM_IFACE_MODEM_CDMA_GET_INTERFACE (ctx->self)->load_activation_state (
> -                ctx->self,
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_activation_state &&
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_activation_state_finish) {
> +            MM_IFACE_MODEM_CDMA_GET_INTERFACE (self)->load_activation_state (
> +                self,
>                  (GAsyncReadyCallback)load_activation_state_ready,
> -                ctx);
> +                task);
>              return;
>          }
>          /* Fall down to next step */
> @@ -1741,18 +1734,18 @@ interface_initialization_step (InitializationContext *ctx)
>          g_signal_connect (ctx->skeleton,
>                            "handle-activate",
>                            G_CALLBACK (handle_activate),
> -                          ctx->self);
> +                          self);
>          g_signal_connect (ctx->skeleton,
>                            "handle-activate-manual",
>                            G_CALLBACK (handle_activate_manual),
> -                          ctx->self);
> +                          self);
>  
>          /* Finally, export the new interface */
> -        mm_gdbus_object_skeleton_set_modem_cdma (MM_GDBUS_OBJECT_SKELETON (ctx->self),
> +        mm_gdbus_object_skeleton_set_modem_cdma (MM_GDBUS_OBJECT_SKELETON (self),
>                                                   MM_GDBUS_MODEM_CDMA (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;
>      }
>  
> @@ -1764,7 +1757,7 @@ mm_iface_modem_cdma_initialize_finish (MMIfaceModemCdma *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
> @@ -1775,6 +1768,7 @@ mm_iface_modem_cdma_initialize (MMIfaceModemCdma *self,
>  {
>      InitializationContext *ctx;
>      MmGdbusModemCdma *skeleton = NULL;
> +    GTask *task;
>  
>      /* Did we already create it? */
>      g_object_get (self,
> @@ -1806,16 +1800,13 @@ mm_iface_modem_cdma_initialize (MMIfaceModemCdma *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_cdma_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
> 


-- 
Aleksander
https://aleksander.es


More information about the ModemManager-devel mailing list