Skip to content

Implement GodotSynchronizationContext#38

Merged
raulsntos merged 1 commit into
godotengine:masterfrom
paulloz:synchronization-context
Oct 24, 2025
Merged

Implement GodotSynchronizationContext#38
raulsntos merged 1 commit into
godotengine:masterfrom
paulloz:synchronization-context

Conversation

@paulloz

@paulloz paulloz commented Jul 23, 2025

Copy link
Copy Markdown
Member

As we discussed earlier, this is a basic synchronization context using the engine work at godotengine/godot#106030.

Let me know if you think this should be more elaborate. But for now I've been able to work with this on my personal project without issue.

@paulloz
paulloz force-pushed the synchronization-context branch from 6f65e37 to 03e0950 Compare August 4, 2025 08:17
@GeorgeS2019

This comment was marked as off-topic.

@raulsntos

This comment was marked as off-topic.

@GeorgeS2019

This comment was marked as off-topic.

@raulsntos

This comment was marked as off-topic.

@raulsntos raulsntos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks generally good. Most of my comments are just questions that came up as I was comparing it with the current implementation in GodotSharp and doesn't necessarily mean a change needs to be made, but if there's no specific reason to differ from the GodotSharp implementation I tend to prefer to keep the status quo.

Comment thread src/Godot.Bindings/Bridge/GodotBridge.cs Outdated
Comment thread src/Godot.Bindings/Core/GodotSynchronizationContext.cs Outdated
Comment thread src/Godot.Bindings/Core/GodotSynchronizationContext.cs Outdated
Comment thread src/Godot.Bindings/Core/GodotSynchronizationContext.cs Outdated
Comment thread src/Godot.Bindings/Core/GodotSynchronizationContext.cs Outdated
Comment on lines +97 to +100
if (SynchronizationContext.Current is not GodotSynchronizationContext context)
{
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check doesn't seem to be in GodotSharp. I guess it's a bit of an edge case anyway.

@paulloz
paulloz force-pushed the synchronization-context branch from 03e0950 to 0f7c400 Compare October 22, 2025 07:36
@paulloz
paulloz force-pushed the synchronization-context branch from 0f7c400 to c1af80a Compare October 23, 2025 11:16

@raulsntos raulsntos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@raulsntos
raulsntos merged commit e5bee4d into godotengine:master Oct 24, 2025
3 checks passed
@raulsntos

Copy link
Copy Markdown
Member

Thanks!

@scgm0

scgm0 commented Oct 24, 2025

Copy link
Copy Markdown
Contributor

An issue has been identified: in GodotSharp, GodotSynchronizationContext is public and can be accessed via Dispatcher.SynchronizationContext to manually invoke GodotSynchronizationContext.Post and GodotSynchronizationContext.Send for executing methods on the main thread. However, this PR only adds GodotSynchronizationContext and marks it as internal. Can we still manually call GodotSynchronizationContext.Post and GodotSynchronizationContext.Send?

@Delsin-Yu

Copy link
Copy Markdown
Contributor

@scgm0

Can we still manually call GodotSynchronizationContext.Post and GodotSynchronizationContext.Send?

I think you can still get this sync ctx via SynchronizationContext.Current and use Post & Send as usual, as we usually do in C#.

@scgm0

scgm0 commented Oct 24, 2025

Copy link
Copy Markdown
Contributor

@scgm0

Can we still manually call GodotSynchronizationContext.Post and GodotSynchronizationContext.Send?

I think you can still get this sync ctx via SynchronizationContext.Current and use Post & Send as usual, as we usually do in C#.

I know about SynchronizationContext.Current, but I recall it's not usable in GodotSharp; you have to use Dispatcher.SynchronizationContext instead. However, I might be mistaken. I'll test later to see if SynchronizationContext.Current works in GodotDotnet.

@scgm0

scgm0 commented Oct 24, 2025

Copy link
Copy Markdown
Contributor

I think you can still get this sync ctx via SynchronizationContext.Current and use Post & Send as usual, as we usually do in C#.

屏幕截图_20251025_010834 屏幕截图_20251025_010851

It does seem possible to use SynchronizationContext.Current, but it holds different values in different threads, meaning users need to first obtain and store SynchronizationContext.Current in the main thread before they can use it in a child thread.

This isn't a problem per se, but it's less convenient compared to GodotSharp. It would be better if an API similar to Dispatcher.SynchronizationContext could be provided.

@raulsntos

Copy link
Copy Markdown
Member

I don't think we want to expose this API, can you provide more details about your use case? I'd recommend opening a new issue describing what you are trying to do, why you need the GodotSynchronizationContext type to do it, and why alternative solutions fall short. Ideally with an MRP that shows the problem.

If all you need is to run some code in the main thread, I believe CallDeferred should work.

@paulloz
paulloz deleted the synchronization-context branch October 25, 2025 08:40
@scgm0

scgm0 commented Oct 25, 2025

Copy link
Copy Markdown
Contributor

If all you need is to run some code in the main thread, I believe CallDeferred should work.

CallDeferred works, but in my project, most of what needs to be pushed to the main thread are lambda functions. Since we already have GodotSynchronizationContext, there's no need to use Callable.From to create a wrapper for the lambda.

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.

6 participants