GNOME Bugzilla – Bug 732116
maincontext: Add a way to remove all currently attached sources by user_data in a thread safe way.
Last modified: 2018-05-24 16:42:10 UTC
It looks like currently there is no easy way to "clean up" a GMainContext from all the sources that have been attached to it. In the new GstAggregator[0] base class, we needed that feature and to do so we ended up creating a dedicated mutex protecting the maincontext in order to make sure that we do not end up adding a source right in time we are actually removing all the others, (which would leads to "g_source_attach: assertion '!SOURCE_DESTROYED (source)' failed"). We might have missed something but looking at the API it looked like the best solution right now. Adding such a feature would make our code simpler, and avoid having 2 Mutexes that do mostly the same thing (as GMainContext also as its own mutex) which is not great performance-wise. [0] http://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/gst-libs/gst/base/gstaggregator.c
We've often talked about this before, but usually from the angle of "run the main context until all of the sources are gone". The trouble with this is that a source is not really a meaningful thing. You may think of it as "something that may cause a dispatch on the main loop at some point in the future" but this is not the whole story. The best counterexample to this is pending asynchronous operations. These (via GTask or similar) take a reference on a main context, but do not add any new sources to it. At some point in the future, with the operation is complete, a source will be added and the reference dropped. This means that "clear all sources from this main context" may result in a main context that wants to dispatch almost as soon as the call is complete. See bug 690126 for some more discussion. Can you tell me more about your specific use case? Maybe there is another way that we could give a lockless and race-free mechanism for detaching a specified group of sources without trying to solve the "detach everything" case that I mention above...
I am not sure I understand exactly what problem you are describing here, but this is the use case we have to handle: We wrote a Baseclass for Gstelements that have several sinkpads, and one single source pad (basically a muxer or a mixer) and we run a thread on the srcpad in which the aggregation of the buffers will happen. To make sure that the aggregating function is called the right number of time, and at the right time/order we use a GMainContext (which is run by the srcpad thread). Now we can get flush start events on the srcpad, that means that current operations should be forgotten, and we should start everything over. In reaction to that we need to ensure that all the GSources that have been attached to our MainContext should be removed from that context. The code that does it is currently: http://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/gst-libs/gst/base/gstaggregator.c#n377 -- so yes it is actually not *all* sources that we remove from the maincontext but all the sources that have the aggregator itself as user data.
Remove by user_data could be interesting.
Created attachment 279586 [details] [review] aggregator: Avoid destroying source we do not own
Comment on attachment 279586 [details] [review] aggregator: Avoid destroying source we do not own Sorry about that, it was not supposed to be attached here.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/895.