Description
The next big step with improving win32 support is getting the event loop running.
There is already a promising draft of a basic implementation at #9957. The most basic first step is #10605 which adds sleep support and swapping between concurrent fibers.
The full event loop implementation can expand on that by waiting on completion status messages from IOCP instead of just putting the thread to sleep.
But we need completion-port based IO for developing and testing event loop integration. The most obvious candidate for this is sockets. Sockets work well with overlapped IO and porting it is relatively simple (see #10610, #10650 for example). There is already a very extensive PR for porting Socket
at #9544. This only uses non-overlapped (i.e. blocking) operations, however. So it's a step in the right direction, but does not reach far enough.
Without a working IOCP-based event loop, the overlapped IO is useless and vice versa.
In order to reduce the chicken-and-egg-problem, I think the best first step is to port Socket
with the overlapped IO, but explicitly waiting on completion for every operation. This means the socket operations are essentially blocking, but internally they use the non-blocking API. Later we can transparently swap the wait routine for a delegation to the event loop.
The handling of overlapped IO is a bit more complex because it is different to the evented approach we have established in the unix implementation.
I think it is necessary to completely extract the system-specific implementation of Socket
methods, similar to other system API implementations, like File
, Dir
or File
. The product would be a Crystal::System::Socket
module with system-specific implementations. The unix version includes include IO::Evented
and defines integrations with the BSD sockets API. The win32 version defines integrations with the Winsock API, using overlapped operations. The overlapped boilerplate should probably be extracted to a module IO::Overlapped
, which is similar to IO::Evented
in purpose and only available on win32.
TODO List
- Extract system-specifics from
Socket
toCrystal::System::Socket
for unix (Extract system-specifics from Socket #10706) - Implement
Crystal::System::Socket
andIO::Overlapped
for win32, using overlapped operations and mocked event loop integration - Implement event loop for win32