Skip to content

Instantly share code, notes, and snippets.

@PlushBeaver
Created August 21, 2020 22:28
Show Gist options
  • Save PlushBeaver/3844b55e2fd7a76a7368ab3289ed84d8 to your computer and use it in GitHub Desktop.
Save PlushBeaver/3844b55e2fd7a76a7368ab3289ed84d8 to your computer and use it in GitHub Desktop.
IOCP alertable wait (buggy)
#define _WIN32_WINNT _WIN32_WINNT_WIN10
#include <windows.h>
#include <stdio.h>
OVERLAPPED overlapped = {};
VOID
timer_proc(LPVOID arg, DWORD low, DWORD high)
{
HANDLE cp = (HANDLE)arg;
ULONG_PTR key = 42;
printf("%s: posting completion: key = %lu, &overlapped = %p\n",
__func__, (ULONG)key, &overlapped);
PostQueuedCompletionStatus(cp, 0, 42, &overlapped);
}
int
main(int argc, char** argv)
{
HANDLE cp;
HANDLE timer;
OVERLAPPED_ENTRY oes[10];
ULONG noes = sizeof(oes) / sizeof(oes[0]);
ULONG noes_out = 0;
ULONG i;
BOOL result;
cp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
if (cp == NULL) {
printf("CreateIoCompletionPort(cp) -> %lu\n", GetLastError());
return EXIT_FAILURE;
}
timer = CreateWaitableTimer(NULL, FALSE, NULL);
if (timer == NULL) {
printf("CreateWaitableTimer() -> %lu\n", GetLastError());
return EXIT_FAILURE;
}
LARGE_INTEGER timeout;
timeout.QuadPart = -10ll * 1000ll * 1000ll; /* 1 sec (relative) */
if (!SetWaitableTimer(timer, &timeout, 0, timer_proc, cp, FALSE)) {
printf("SetWaitableTimer() -> %lu\n", GetLastError());
return EXIT_FAILURE;
}
printf("%s: waiting on IOCP\n", __func__);
result = GetQueuedCompletionStatusEx(
cp, oes, noes, &noes_out, INFINITE, TRUE);
if (!result) {
printf("GetQueuedCompletionStatus() -> %lu\n", GetLastError());
/* Not exiting, let's see the result anyway. */
}
printf("%s: %lu I/O request completed\n", __func__, noes_out);
for (i = 0; i < noes_out; i++) {
printf("I/O result %lu: key = %lu, &overlapped = %p\n",
i, (ULONG)oes[i].lpCompletionKey, oes[i].lpOverlapped);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment