3

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.

José Fonte
  • 4,016
  • 2
  • 16
  • 28
Alex
  • 1,602
  • 20
  • 33
  • Do you know why you added the tag for `objective-c`? It is another language and so it appears unrelated to me. – Jongware Jan 20 '18 at 11:43
  • I think you generally don't want to reuse cancellables for multiple operations. Is there a reason you need to keep a reference to it in your struct? – ptomato Jan 20 '18 at 21:55
  • 2
    What about cancel it first, then reset it and reuse it afterwards? – José Fonte Jan 21 '18 at 00:13
  • @ptomato ok for me to just dont reuse. I have the cancellables in my struct because I want to cancel all pending operations on finalize, when my widget gets destroyed. – Alex Jan 21 '18 at 06:08
  • @Jose Fonte I'll have a try. So directly after calling cancel, reset should be safe? – Alex Jan 21 '18 at 06:11
  • 1
    Like you said, resetting a cancellable in use is undefined but if you cancel it first, then it's not in use and you should be able to reset it for reuse. – José Fonte Jan 21 '18 at 12:10
  • 1
    Thanks José Fonte! Seems to work fine now .. I just did not know that a cancellable is not "in use" any more, when it is cancelled. – Alex Jan 21 '18 at 16:41
  • 1
    Feel free to complete your question with your own answer to "close it" and serve as reference to future readers. GL – José Fonte Feb 02 '18 at 21:12
  • I first waited if you would do, than I forgot about this question :) Thanks for the offer! Answer added. – Alex Feb 04 '18 at 18:27

1 Answers1

0

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);
Alex
  • 1,602
  • 20
  • 33