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.

@badcel
Copy link
Member

badcel commented Feb 26, 2025

@ShortDevelopment I saw in your project that you implemented the Send-Method on a synchronization context. Do you have some more insight why this method is important except calling it manually?

I wonder if this should be implemented in the way I mentioned in #915 (comment). Any feedback is appreciated.

@ShortDevelopment
Copy link

@badcel I honestly did not expect someone to find this new repo that quickly 😅.
It's just a very early prototype to learn Gtk and this projection.

I'm using the "lowlevel" Tmds.DBus.Protocol library to interop with BLueZ DBus and there's a single "manual" call to Send:
https://github.com/tmds/Tmds.DBus/blob/1271f1a85fa2d31d4dcb3cf0221d8ce2b60cb834/src/Tmds.DBus.Protocol/DBusConnection.cs#L505-L520

This was just a quick hack to get it to work.
In a robust implementation there should be at least a shortcut (like you mentioned) to prevent deadlocks and optimize performance.

Also I just noticed I'm calling the delegate twice...

@badcel
Copy link
Member

badcel commented Feb 27, 2025

Thanks for the quick reply.

It helps a lot to know that not some application developer but library developer would do the call manually like in the scenario you mentioned.

In this case I think there should be something done in regard to this PR.


P.S.

I'm always looking for GirCore code from real world applications which could be a good starting template for newcomers to GTK. Especially your builder pattern is pretty clever and creates a nice and clean api to use.

You can find other projects using your own library pretty easy via GitHub insights.

@badcel badcel added this to the 0.7.0 milestone Feb 27, 2025
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.

4 participants