diff --git a/src/afd.c b/src/afd.c index 669aba9..e03062d 100644 --- a/src/afd.c +++ b/src/afd.c @@ -126,64 +126,38 @@ error: int afd_poll(SOCKET driver_socket, AFD_POLL_INFO* poll_info, OVERLAPPED* overlapped) { - IO_STATUS_BLOCK iosb; - IO_STATUS_BLOCK* iosb_ptr; - HANDLE event = NULL; + IO_STATUS_BLOCK* iosb; + HANDLE event; void* apc_context; NTSTATUS status; - if (overlapped != NULL) { - /* Overlapped operation. */ - iosb_ptr = (IO_STATUS_BLOCK*) &overlapped->Internal; - event = overlapped->hEvent; + /* Blocking operation is not supported. */ + assert(overlapped != NULL); - /* Do not report iocp completion if hEvent is tagged. */ - if ((uintptr_t) event & 1) { - event = (HANDLE)((uintptr_t) event & ~(uintptr_t) 1); - apc_context = NULL; - } else { - apc_context = overlapped; - } + iosb = (IO_STATUS_BLOCK*) &overlapped->Internal; + event = overlapped->hEvent; - } else { - /* Blocking operation. */ - iosb_ptr = &iosb; - event = CreateEventW(NULL, FALSE, FALSE, NULL); - if (event == NULL) - return_map_error(-1); + /* Do what other windows APIs would do: if hEvent has it's lowest bit set, + * don't post a completion to the completion port. */ + if ((uintptr_t) event & 1) { + event = (HANDLE)((uintptr_t) event & ~(uintptr_t) 1); apc_context = NULL; + } else { + apc_context = overlapped; } - iosb_ptr->Status = STATUS_PENDING; + iosb->Status = STATUS_PENDING; status = NtDeviceIoControlFile((HANDLE) driver_socket, event, NULL, apc_context, - iosb_ptr, + iosb, IOCTL_AFD_POLL, poll_info, sizeof *poll_info, poll_info, sizeof *poll_info); - if (overlapped == NULL) { - /* If this is a blocking operation, wait for the event to become signaled, - * and then grab the real status from the io status block. */ - if (status == STATUS_PENDING) { - DWORD r = WaitForSingleObject(event, INFINITE); - - if (r == WAIT_FAILED) { - DWORD error = GetLastError(); - CloseHandle(event); - return_set_error(-1, error); - } - - status = iosb_ptr->Status; - } - - CloseHandle(event); - } - if (status == STATUS_SUCCESS) return 0; else if (status == STATUS_PENDING)