How Can I DeviceIoControl in less than 10 milliseconds?
- From: christ2008 <christhompson@xxxxxxxxxxxxxx>
- Date: Wed, 5 Mar 2008 11:38:55 -0800 (PST)
The following is code snippet inside a thread that first waits on an
incoming data from a socket (not shown here), then attempts to
immediately write it to my device.
The entire sequence (wait for buffer full, then DeviceIoControl, then
GetOverlappedResult) should take no more than 10 milliseconds.
//////////////////// BEGIN SNIPPET /////////////////////////
ResetEvent(context->ahEvents[OVERLAPPEDWRITE]);
int na;
DWORD numBytesRead = 0;
BOOL rc = DeviceIoControl(hDevice,
DIOC_WRITE,
buffer, buflen, // in/write
&na, sizeof(na), // out/read
&numBytesRead,
&(context->overlapped));
if (!rc)
{
if (GetLastError() == ERROR_IO_PENDING)
{
// Wait for write to complete
DWORD dw = WaitForMultipleObjects(2,
context->ahEvents,
FALSE,
INFINITE);
{
if (dw == WAIT_OBJECT_0+1) // overlapped i/o event
{
rc = GetOverlappedResult(hDevice,
&(context->overlapped),
&numBytesRead,
FALSE);
if (!rc)
HandleLE(GetLastError(), "GetOverlappedResult()");
}
else // thread stop event or error
{
HandleLE(GetLastError(), "WaitForMultipleObjects()");
if (CancelIo(hDevice) == 0) // cancel pending write
HandleLE(GetLastError(), "CancelIo()");
}
}
}
else
HandleLE(GetLastError(), "DeviceIoControl not/O pending");
}
\\\\\\\\\\\\\\\\\\\ END SNIPPET \\\\\\\\\\\\\\\\\\\\\\\\\
My problem is that the above snippet (not even including the preceding
socket handling) *sometimes* takes MUCH longer than 10 milliseconds...
sometimes even more than 200 milliseconds!
Further diagnostics and measurements revealed that this time is not
spent in DeviceIoControl() and not in GetOverlappedResult() but rather
in WaitForMultipleObjects().
My (current) conclusion is that this is due to preemption.
Intuitively it seems that, in order for "my socket handling sequence"
to take less than 10ms, WaitForMultipleObjects (and thus
GetOverlappedResult) should move elsewhere (possibly to another
thread?)
If so, how do I do that? It seems insane to track dynamically variable
number of overlapped events...
Also, the way I have been using overlapped I/O as depicted above gives
me no benefit over blocking I/O. So, what am I doing wrong? How can
overlapped I/O be used the way it was intended to be used?
Thanks,
Chris
.
- Follow-Ups:
- Re: How Can I DeviceIoControl in less than 10 milliseconds?
- From: Ben Voigt [C++ MVP]
- Re: How Can I DeviceIoControl in less than 10 milliseconds?
- From: Alex Mizrahi
- Re: How Can I DeviceIoControl in less than 10 milliseconds?
- From: Remy Lebeau
- Re: How Can I DeviceIoControl in less than 10 milliseconds?
- Prev by Date: RE: Interrupt Priority
- Next by Date: Re: How Can I DeviceIoControl in less than 10 milliseconds?
- Previous by thread: RE: Interrupt Priority
- Next by thread: Re: How Can I DeviceIoControl in less than 10 milliseconds?
- Index(es):
Relevant Pages
|