Skip to content

gio: Add GCancellableChild, a GCancellable with a GCancellable parent

Marco Trevisan requested to merge 3v1n0/glib:cancellable-child into main

Many times we need to manage multiple async operations inside an object and each operation may need to be controlled via a different GCancellable's, however this implies keeping track of all of them.

It's instead at times convenient to just create a main object cancellable to control a chain of GCancellable's that depend on it.

As per this introduce a GCancellable subtype that requires a parent GCancellable object that is monitored for cancellation.

As per the way GCancellable's "cancelled" signal is defined we need to disconnect on it outside the actual callback and we use an idle for that. We avoid keeping references in the involved objects so in case the idle functions may just do nothing.


I preferred not to use a GCancellableSource to avoid extra overhead and to have more control on the data, and also to ensure that a child gets cancelled in the same thread of the parent, and at the very same moment.


To avoid having to introduce even more multi-thread complications (I tried hard to use other mutexes, as we already have them in the parent cancellable, unless we decide to share that one for both implementations), I decided that it's better to just throw an idle and forget it in case the cancellable we're connected to and its child are finalized earlier.

Indeed we could track that, without using Weak refs, and just act early in case we want, but it implies much complication that I don't think it's worth.


Indeed all this could be done without adding a new type, but I thought was better not to touch GCancellable itself adding a new_with_parent constructor for example, to avoid increasing its size when not required.

Edited by Marco Trevisan

Merge request reports