After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 732116 - maincontext: Add a way to remove all currently attached sources by user_data in a thread safe way.
maincontext: Add a way to remove all currently attached sources by user_data ...
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: gthread
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2014-06-23 16:11 UTC by Thibault Saunier
Modified: 2018-05-24 16:42 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
aggregator: Avoid destroying source we do not own (2.98 KB, patch)
2014-06-30 10:29 UTC, Thibault Saunier
rejected Details | Review

Description Thibault Saunier 2014-06-23 16:11:22 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
Comment 1 Allison Karlitskaya (desrt) 2014-06-23 18:56:27 UTC
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...
Comment 2 Thibault Saunier 2014-06-23 19:30:47 UTC
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.
Comment 3 Allison Karlitskaya (desrt) 2014-06-24 01:34:27 UTC
Remove by user_data could be interesting.
Comment 4 Thibault Saunier 2014-06-30 10:29:06 UTC
Created attachment 279586 [details] [review]
aggregator: Avoid destroying source we do not own
Comment 5 Thibault Saunier 2014-06-30 10:31:29 UTC
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.
Comment 6 GNOME Infrastructure Team 2018-05-24 16:42:10 UTC
-- 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.