IOCP Reference
IOCP is how Windows provides the infrastructure we need to create a scalable event queue. It differs from the two others since IOCP uses a completion based model.

Quick reference of important IOCP syscalls

CreateCompletionPort

This syscall gives us a handle to a completion port. To us, this will represent the queue which we register an interest in getting notified on when an event has happened.
CreateIoCompletionPort function (IoAPI.h) - Win32 apps
docsmsft

WSARecv

This syscall registers an interest in Readevents on a socket. Windows calls this an Receiveevent.
WSARecv function (winsock2.h) - Win32 apps
docsmsft

GetQueuedCompletionStatusEx

This syscall comes in a different flavor as well called GetQueuedCompletionStatuswhich only returns one event at a time. The Exversion can return multiple events that has been completed, and we'll use that since we there is no point for us to only handle one and one event at a time.
GetQueuedCompletionStatus function (ioapiset.h) - Win32 apps
docsmsft

WSAGetLastError

We actually need this too since a perfectly normal situation is returned as an error on Windows. We have to check if the error is WSA_IO_PENDINGwhich we most likely will get when calling WSARecv. This specific error simply means that the action we're waiting for is not ready immediately, and can be waited on by calling GetQueuedCompletionStatusEx.
WSAGetLastError function (winsock.h) - Win32 apps
docsmsft

CloseHandle

When requesting resources from the OS directly we're also responsible for letting the OS know we're done with them. This function lets us do just that.
CloseHandle function (handleapi.h) - Win32 apps
docsmsft

PostQueuedCompletionStatus

It turns out that closing a thread which has yielded control to the OS is not so easy. We actually need to wake up the thread and let it know that we want it to stop waiting, release it's resources and close down gracefully. To do this, we issue a timeout event with the timeout set to 0 which means it times out immediately. We have to register interest to get woken up from our blocking Poll::poll()when the timeout event has completed, and we need this syscall will let us register such an interest to our completion port.
PostQueuedCompletionStatus function (IoAPI.h) - Win32 apps
docsmsft
So, this is the basic infrastructure we need on Windows. As we'll see when we look through the code, each of this call requires us to pass in some structures and flags to actually make them do what we want but it's good to keep this high-level overview in mind when we dig into the details.
Last modified 1yr ago