Fix waiting on Windows to be sure I/O events can still come in. #4325
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prior to this commit we were using a mechanism on Windows called
WaitForSingleObject
when we wanted to put a thread to sleep until we activated a signal to wake it up later.Now we use
WaitForSingleObjectEx
instead, so we can be sure that the thread is kept in an "alertable" state if there is an APC (Async Procedure Call) that needs to be run during the wait period.We use APCs for socket I/O completion callbacks, and they can only arrive on the thread that initiated them, so it's important to allow such callbacks to run even if the scheduler thread is suspended.
Note that the callbacks we use for sockets will not run any Pony code - they will only dispatch a message via the ASIO subsystem, so running those APCs is safe even if the actor has been work-stealed to another thread and is running there. In such a situation, the ASIO event will just go into the actor's queue and they'll process the message later, as normal. We just need to make sure the APC actually will get run, hence we need to ensure even suspended threads will stay "alertable".
Note that this fixes a similar problem to one of the problems that was fixed in PR #3816, wherein some calls to
Sleep
were migrated toSleepEx
for the same reason - to stay "alertable".