gir.core icon indicating copy to clipboard operation
gir.core copied to clipboard

Implement `MainLoopSynchronizationContext.Send`

Open chairclr opened this issue 2 years ago • 5 comments

  • [x] I agree that my contribution may be licensed either under MIT or any version of LGPL license.

Tests still need to be written, if they are necessary.

chairclr avatar Jul 30 '23 19:07 chairclr

Thanks for your PR. We explicitly did not implement the Send method as we did not know how to invoke / test it (see: https://github.com/gircore/gir.core/pull/856#issuecomment-1526969582).

Do you have any insight on how to trigger the method or for which use cases it is used (perhaps even some examples / documentation)?

If this is clarified we can go ahead and add some tests to your PR and then merge it.

The tests should probably be part of: https://github.com/gircore/gir.core/blob/main/src/Tests/Libs/GLib-2.0.Tests/SynchronizationContextTest.cs

badcel avatar Jul 30 '23 20:07 badcel

Thanks for the swift response.

Our use case involved creating our own ExecuteInMainContext function, which directly called SynchronizationContext.Post for a blocking implementation. (see: Launcher.cs)

We acquired a reference to the GLib MainLoopSynchronizationContext through SynchronizationContext.Current inside of our Gtk.Application.OnActivate event. (see: MainCommands.cs)

I'm not exactly sure how to integrate this into the GLib tests either.

chairclr avatar Jul 30 '23 21:07 chairclr

After reading several implementations I think the correct thing to do would be to call MainLoop.Invoke as it does the right thing as far as I understand. Your implementation is basically the last resort execution mentioned in the linked documentation which is fine but misses some optimizations mentioned there.

To test this I think it would be okay to manually invoke the Send method.

@cameronwhite: any thoughts?

badcel avatar Jul 31 '23 04:07 badcel

My understanding is that g_main_context_invoke() would be more optimal if this happens to be called while already on the main UI thread since it'll call the function immediately, but otherwise it's equivalent to idle_add()

cameronwhite avatar Jul 31 '23 15:07 cameronwhite

Exactly. If the method is called manually it is not known from which thread this happens. So having some short circuit to avoid IdleAdd might be preferable. Compare Avalonia and Unity3d and Wpf which do similar things from the looks.

badcel avatar Jul 31 '23 16:07 badcel