cgtk3glibgdbus

Possible to reuse my GCancellable instance?


Before triggering a proxy-call via gdbus, I want to cancel any possible pending calls on this dbus method. My first attempt was like that:

// in the "member" list of my widget
GCancellable   *my_cancellable;

// in the init method of my widget:
plugin->my_cancellable = g_cancellable_new ();

// in the method which does the call
g_cancellable_cancel (plugin->my_cancellable);
my_gdbus_call_something (plugin->proxy, plugin->my_cancellable, reply_handler,plugin);

That did not work out, since using the same cancellable instance as well will cancel any future call. Looks like I cannot use g_cancellable_reset, since the doc stats the following:

If cancellable is currently in use by any cancellable operation then the behavior of this function is undefined.

Is it possible to check the in-use state of my GCancellable? Would it help me at all ?

What already works fine for me is to create a new cancellable for each call:

// in the "member" list of my widget
GCancellable   *my_cancellable;

// in the init method of my widget:
plugin->my_cancellable = NULL;

// in the method which does the call
if(plugin->my_cancellable != NULL)
  {
    g_cancellable_cancel (plugin->my_cancellable);
    g_object_unref (plugin->my_cancellable);
  }
plugin->my_cancellable = g_cancellable_new ();
my_gdbus_call_something (plugin->proxy, plugin->my_cancellable, reply_handler,plugin);

Is it save to unref my_cancellable, considered there is a pending call ? This must be a standard use-case .. I wonder if there is no better solution.


Solution

  • My first coding attempt was almost fine .. I just did not realize that a cancellable is not "in use" any more, when it is cancelled, so after calling g_cancellable_cancel it is safe to call g_cancellable_reset

    Thanks to José Fonte for pointing that out!

    Here the fixed code:

    // in the "member" list of my widget
    GCancellable   *my_cancellable;
    
    // in the init method of my widget:
    plugin->my_cancellable = g_cancellable_new ();
    
    // in the method which does the call
    g_cancellable_cancel (plugin->my_cancellable);
    g_cancellable_reset (plugin->my_cancellable);
    my_gdbus_call_something (plugin->proxy, plugin->my_cancellable, reply_handler,plugin);