Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MainLoopSynchronizationContext.Send #915

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chairclr
Copy link

  • 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.

@badcel
Copy link
Member

badcel commented Jul 30, 2023

Thanks for your PR. We explicitly did not implement the Send method as we did not know how to invoke / test it (see: #856 (comment)).

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

@chairclr
Copy link
Author

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.

@badcel
Copy link
Member

badcel commented Jul 31, 2023

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?

@cameronwhite
Copy link
Contributor

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()

@badcel
Copy link
Member

badcel commented Jul 31, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants