110 likes | 414 Views
Where are we?. Last topic: I/O Completion PortsRemind me what we did last class
E N D
1. Win32 Programming Lesson 27: I/O Completion Ports
OUCH
2. Where are we? Last topic: I/O Completion Ports
Remind me what we did last class…
3. Threads (again) Concurrent model where a thread is created to handle the request
Leverages thread pooling
But it is *very* gnarly
4. CreateIoCompletionPort HANDLE CreateIoCompletionPort( HANDLE hFile, HANDLE hExistingCompletionPort, ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads);
Pretty confusing, as it does two things…
5. Step 1 HANDLE CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, dwNumberOfConcurrentThreads);
Creates a new port with dwNumberOfConcurrentThreads (max)
What do you notice missing from typical Kernel Object calls?
6. Step 2 Associate a Device with this port
CreateIoCompletionPort( hDevice, hCompletionPort, dwCompletionKey, 0);
Next, create 2*CPU threads…
7. Per Thread Wait for requests, putting yourself to sleep in the process
BOOL GetQueuedCompletionStatus( HANDLE hCompletionPort, PDWORD pdwNumberOfBytesTransferred, PULONG_PTR pCompletionKey, OVERLAPPED ** ppOverlapped, DWORD dwMilliseconds);
Which port are we monitoring? How long to sleep for?
8. Example DWORD dwNumBytes;ULONG_PTR CompletionKey;OVERLAPPED * pOverlapped;BOOL bOk = GetQueuedCompletionStatus(hIOCP, &dwNumBytes, &CompletionKey, &pOverlapped, 1000);DWORD dwError = GetLastError;if (bOk) { // IO Request OK} else { if (pOverlapped !=NULL) { // We failed… } else { if (dwError == WAIT_TIMEOUT) { // We timed out } else { // Bad call to fn } }}
9. Note IO Completion Requests are FIFO (why?)
Thread awakenings are LIFO (why?)
Vista allows you to retrieve multiple requests at once (why is this helpful?)
GetQueuedCompletionStatusEx(…);
And now we get to the point
How this works when a thread goes to sleep…
10. Don’t Need a Real Completion BOOL PostQueuedCompletionStatus( HANDLE hCompletionPort, DWORD dwNumBytes, ULONG_PTR CompletionKey, OVERLAPPED* pOverlapped);
Very neat way to do this and has lots of other uses
Vista: CloseHandle (hCompletionPort) wakes all threads and returns FALSE with ERROR_INVALID_HANDLE
11. Congratulations YOU HAVE COMPLETED ALL YOUR NEW MATERIAL
Every single person who stuck with me this class has done well!