【编程技术-IO完成端口原理】此文章归类为:编程技术。
IO完成端口原理
内核创建一个KQUEUE,
fileObject->CompletionContext->Port ==> PKQUEUE;
fileObject->CompletionContext->Key = 用户自定义的值,可以是buf指针;
typedef struct _KQUEUE {
DISPATCHER_HEADER Header;
LIST_ENTRY EntryListHead; // 已完成IO的IRP List: &irp->Tail.Overlay.ListEntry;
ULONG CurrentCount; // 当前运行状态线程数
ULONG MaximumCount; // 最大并行线程数(默认等于CPU核心数)
LIST_ENTRY ThreadListHead; // 所有调用过GetQueuedCompletionStatus()的线程List
} KQUEUE, *PKQUEUE;
IO完成时:
IopCompleteRequest()
irp->Tail.CompletionKey = fileObject->CompletionContext->Key;
KeInsertQueue(PRKQUEUE, PLIST_ENTRY)
PRKQUEUE = fileObject->CompletionContext->Port; PLIST_ENTRY = &irp->Tail.Overlay.ListEntry;
KiInsertQueue()
if (Queue->CurrentCount < Queue->MaximumCount)
{CurrentCount++; KiReadyThread(Queue->Header.WaitListHead[WaitBlock->Thread]);}
else
InsertTailList(&Queue->EntryListHead, Entry);
线程来GetQueuedCompletionStatus()时:
NtRemoveIoCompletion()
Entry = KeRemoveQueue()
once: InsertTailList(&Queue->ThreadListHead, Entry);
if (Queue->CurrentCount < Queue->MaximumCount)
Entry = RemoveEntryList(Queue->EntryListHead.Flink);
else
{CurrentCount--; InsertTailList(&Queue->Header.WaitListHead, &Thread->WaitBlock[0].WaitListEntry); KiSwapThread();}
更多【编程技术-IO完成端口原理】相关视频教程:www.yxfzedu.com