Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
На входе в цикл событий проверяется счетчик асинхронных запросов. Если зарегистрированных запросов нет, то программа завершается. Если есть, то вызывается функция GetQueuedCompletionStatus(), которая либо возвращает очередное событие, либо, если событий нет, приостанавливает работу потока до их появления.
«Wait functions allow a thread to block its own execution. The wait functions do not return until the specified criteria have been met. The type of wait function determines the set of criteria used. When a wait function is called, it checks whether the wait criteria have been met. If the criteria have not been met, the calling thread enters the wait state until the conditions of the wait criteria have been met or the specified time-out interval elapses.»
«Блокирующие функции позволяют потоку приостановить свое выполнение.… При вызове блокирующая функция проверяет переданное ей условие. Если условие не выполнено, то поток получает ожидающий статус и функция не возвращает своего значения до тех пор пока условие не выполнится »
В node.js создается нить, которая вызывает CreateFileW(), потом PostQueuedCompletionStatus() и завершает работу.
The libuv filesystem operations are different from socket operations. Socket operations use the non-blocking operations provided by the operating system. Filesystem operations use blocking functions internally, but invoke these functions in a thread pool and notify watchers registered with the event loop when application interaction is required.
QueueUserWorkItem(&uv_fs_thread_proc,
req,
WT_EXECUTEDEFAULT)
file = CreateFileW(req->pathw,
access,
share,
NULL,
disposition,
attributes,
NULL);
ReadFile(handle, req->buf, req->length, &bytes, overlapped_ptr)
pOverlapped [in, out, optional]
A pointer to an OVERLAPPED structure is required if the hFile parameter was opened with FILE_FLAG_OVERLAPPED, otherwise it can be NULL.
If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete.
if (offset != -1) {
memset(&overlapped, 0, sizeof overlapped);
offset_.QuadPart = offset;
overlapped.Offset = offset_.LowPart;
overlapped.OffsetHigh = offset_.HighPart;
overlapped_ptr = &overlapped;
} else {
overlapped_ptr = NULL;
}
Количество потоков, которые будут обслуживать собственно работу с IO задаются при создании completion port, параметр NumberOfConcurrentThreads
loop->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
NODE.JS + Windows: заглянем внутрь