-
Notifications
You must be signed in to change notification settings - Fork 470
[Win32] Initialize Windows Runtime on queue threads #595
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
Conversation
695500f
to
52b65ed
Compare
is it possible to have app authors determine which initialization method to use? would this make the |
No, I don't see how to do that. These threads are spawned without user intervention, and I don't remember any APIs to control the worker threads it creates. Is there a global Windows API that lets you set the apartment model? |
@compnerd given the behavior of .NET it seems correct to initialize background threads to MTA: https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.setapartmentstate?view=net-7.0#remarks if needed perhaps we could evolve this in the future but not an immediate need |
Well, this is only for background threads, so this sounds like we have a path forward. I think that the only thing missing here is the death for an error scenario. @triplef any chance you can update the patch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking at the implementation of .NET, it looks like they call both: https://github.com/dotnet/runtime/blob/991b15056e276209b64a711face35715ba335c77/src/coreclr/vm/threads.cpp#L4898
also looks like ignoring an unexpected HRESULT might be acceptable?
It may be that its easier to debug the .NET runtime, things are a bit more complicated with debugging dispatch, I'd rather be more stringent. The number of times that the dispatch assertions have saved me hours of debugging is ridiculous. Blindly following .NET doesn't seem like the right thing to me. |
52b65ed
to
e8330b4
Compare
e8330b4
to
be275d4
Compare
I’ve updated the patch with a fatal error message in case COM initialization fails. Let me know if there’s any other changes needed. |
@swift-ci please test |
@swift-ci please test Windows platform |
1 similar comment
@swift-ci please test Windows platform |
return 0; | ||
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); | ||
if (FAILED(hr)) { | ||
_dispatch_client_assert_fail("Error %ld initializing Windows Runtime", hr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we safely assume that CoInitializeEx
is enough to initialize the Windows Runtime, when Roinitialize doesn't mention this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was an earlier discussion about these functions here, does that maybe answer your question? Unfortunately I’m not familiar with the differences between the two functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CoInitializeEx
initialize concurrent model as STA, where RoInitialize
by default uses MTA. Though passing RO_INIT_SINGLETHREADED
will use STA instead.
On Windows, all threads that want to use Windows Runtime or COM components must be initialized with a call to CoInitializeEx() or the slightly more modern RoInitialize() (same as
Windows::Foundation::Initialize
).This change initializes all libdispatch threads so it’s possible to use COM and Windows Runtime APIs.