Multithreading and the device driver

I have a WDM driver which supports multiple R/W DMAs. The HW is a bus
master PCI device. Since I have to support Win98 and I am kinda lazy, I
only implement the packet-based DMA. The test program is simple; I
CreateThread(…) two threads with the same priority; one is doing READs and
the other is doing WRITEs. Both are through DeviceIoControl(…). READ
uses METHOD_OUT_DIRECT and WRITE uses METHOD_IN_DIRECT in the driver.

OK, from the user mode console program I see the transaction sequence is
RWRWWWW. Note the printout is before DeviceIoControl(…). From the KM
debugger I get RWWWWWR. That gives me the impression that the IO Manager
does not see the second READ until the last WRITE, though the second READ
actually is issued earlier.

Why is that? How come while WRITE is pending for the interrupt, READ would
not get fired by the IO manager? I have run the test several times;
sometimes READs dominates, sometimes it is WRITEs. Is there anything I miss
that the two threads would not be interleavingly running?

Thanks

TA H.


Join the world’s largest e-mail service with MSN Hotmail.
http://www.hotmail.com

The IO manager does not do any scheduling. The order of IO requests depends
only on thread scheduling. The IO is issued to the driver when your thread
runs. Is the user mode printout coming before or after the system call
completes ? Also note that console print calls will also be buffered. The
priority value you pass to IoCompleteRequest also has some effect on thread
scheduling.

If you want to do explicit scheduling of the IO requests use a lock or
critical section in user mode and ensure that your reads complete before you
issue writes. Otherwise the system does not guarantee any order whatsoever.

“Ta H.” wrote in message news:xxxxx@ntdev…
>
> I have a WDM driver which supports multiple R/W DMAs. The HW is a bus
> master PCI device. Since I have to support Win98 and I am kinda lazy, I
> only implement the packet-based DMA. The test program is simple; I
> CreateThread(…) two threads with the same priority; one is doing READs
and
> the other is doing WRITEs. Both are through DeviceIoControl(…). READ
> uses METHOD_OUT_DIRECT and WRITE uses METHOD_IN_DIRECT in the driver.
>
> OK, from the user mode console program I see the transaction sequence is
> RWRWWWW. Note the printout is before DeviceIoControl(…). From the KM
> debugger I get RWWWWWR. That gives me the impression that the IO Manager
> does not see the second READ until the last WRITE, though the second READ
> actually is issued earlier.
>
> Why is that? How come while WRITE is pending for the interrupt, READ
would
> not get fired by the IO manager? I have run the test several times;
> sometimes READs dominates, sometimes it is WRITEs. Is there anything I
miss
> that the two threads would not be interleavingly running?
>
> Thanks
>
> TA H.
>
>
> _________________________________________________________________
> Join the world’s largest e-mail service with MSN Hotmail.
> http://www.hotmail.com
>
>
>
>

In my user mode program, the printf(…) comes right before
DeviceIoControl(…) which operates READs or WRITEs. My questions is if
the READ thread is blocked and waiting for the interrupt, why does the
WRITE thread not get scheduled to run? and vice versa? Also is it the
scheduling round-robin? My ideal case is READs and WRITEs can happen
interleavingly. Now I cannot see it that way.

Thanks

Ta H.

did you open the file for synchronous i/o?

-p

-----Original Message-----
From: xxxxx@hotmail.com [mailto:xxxxx@hotmail.com]
Sent: Thursday, July 11, 2002 2:48 PM
To: NT Developers Interest List
Subject: [ntdev] Re: Multithreading and the device driver

In my user mode program, the printf(…) comes right before
DeviceIoControl(…) which operates READs or WRITEs. My questions is if
the READ thread is blocked and waiting for the interrupt, why does the
WRITE thread not get scheduled to run? and vice versa? Also is it the
scheduling round-robin? My ideal case is READs and WRITEs can happen
interleavingly. Now I cannot see it that way.

Thanks

Ta H.


You are currently subscribed to ntdev as: xxxxx@microsoft.com To
unsubscribe send a blank email to %%email.unsub%%

Do you mean if I CreateFile(…) the driver without FILE_FLAG_OVERLAPPED,
though I have multiple threads by the same handle, one thread has to wait
for another thread to be finished in order to get the chance to get run?
Even so, why one thread always seems to be dominant over the other in my
case?

For the case that more than one thread is pending, do I have to use the
FILE_FLAG_OVERLAPPED I/O?

From: “Peter Wieland”
>Reply-To: “NT Developers Interest List”
>To: “NT Developers Interest List”
>Subject: [ntdev] Re: Multithreading and the device driver
>Date: Thu, 11 Jul 2002 15:19:24 -0700
>
>did you open the file for synchronous i/o?
>
>-p
>
>-----Original Message-----
>From: xxxxx@hotmail.com [mailto:xxxxx@hotmail.com]
>Sent: Thursday, July 11, 2002 2:48 PM
>To: NT Developers Interest List
>Subject: [ntdev] Re: Multithreading and the device driver
>
>
>In my user mode program, the printf(…) comes right before
>DeviceIoControl(…) which operates READs or WRITEs. My questions is if
>the READ thread is blocked and waiting for the interrupt, why does the
>WRITE thread not get scheduled to run? and vice versa? Also is it the
>scheduling round-robin? My ideal case is READs and WRITEs can happen
>interleavingly. Now I cannot see it that way.
>
>Thanks
>
>Ta H.
>
>—
>You are currently subscribed to ntdev as: xxxxx@microsoft.com To
>unsubscribe send a blank email to %%email.unsub%%
>
>
>—
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to %%email.unsub%%

_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

If you don’t open a file for overlapped IO then each request is serialized
against the other (for the same handle). I presume both the read/write
threads used the same file handle (see the platform SDK for more
information). In this case the second operation will start only after the
first operation completes. Again the order in which the operations take
place depends on the thread scheduler. Is your write operation completely
CPU bound (like copying to a cache) ? That could mean that your read
completed and your second thread got the CPU and did a few writes before the
first thread got it back. There are all kinds of possibilities.

“Ta H.” wrote in message news:xxxxx@ntdev…
>
> Do you mean if I CreateFile(…) the driver without FILE_FLAG_OVERLAPPED,
> though I have multiple threads by the same handle, one thread has to wait
> for another thread to be finished in order to get the chance to get run?
> Even so, why one thread always seems to be dominant over the other in my
> case?
>
> For the case that more than one thread is pending, do I have to use the
> FILE_FLAG_OVERLAPPED I/O?
>
> >From: “Peter Wieland”
> >Reply-To: “NT Developers Interest List”
> >To: “NT Developers Interest List”
> >Subject: [ntdev] Re: Multithreading and the device driver
> >Date: Thu, 11 Jul 2002 15:19:24 -0700
> >
> >did you open the file for synchronous i/o?
> >
> >-p
> >
> >-----Original Message-----
> >From: xxxxx@hotmail.com [mailto:xxxxx@hotmail.com]
> >Sent: Thursday, July 11, 2002 2:48 PM
> >To: NT Developers Interest List
> >Subject: [ntdev] Re: Multithreading and the device driver
> >
> >
> >In my user mode program, the printf(…) comes right before
> >DeviceIoControl(…) which operates READs or WRITEs. My questions is if
> >the READ thread is blocked and waiting for the interrupt, why does the
> >WRITE thread not get scheduled to run? and vice versa? Also is it the
> >scheduling round-robin? My ideal case is READs and WRITEs can happen
> >interleavingly. Now I cannot see it that way.
> >
> >Thanks
> >
> >Ta H.
> >
> >—
> >You are currently subscribed to ntdev as: xxxxx@microsoft.com To
> >unsubscribe send a blank email to %%email.unsub%%
> >
> >
> >—
> >You are currently subscribed to ntdev as: xxxxx@hotmail.com
> >To unsubscribe send a blank email to %%email.unsub%%
>
>
>
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
>
>

Both READs and WRITEs use the packet-based DMA, and the differences are the
DMA direction and there are two dummy objects. I am not sure they are
CPU-bound, but I do need to set up the staged DMAs in the DPC. Another
thing is if the operions were CPU-bound, why would the thread scheduler
prefer one over another?

From: “Nar Ganapathy [MS]”
>Reply-To: “NT Developers Interest List”
>To: “NT Developers Interest List”
>Subject: [ntdev] Re: Multithreading and the device driver
>Date: Thu, 11 Jul 2002 18:36:01 -0700
>
>If you don’t open a file for overlapped IO then each request is serialized
>against the other (for the same handle). I presume both the read/write
>threads used the same file handle (see the platform SDK for more
>information). In this case the second operation will start only after the
>first operation completes. Again the order in which the operations take
>place depends on the thread scheduler. Is your write operation completely
>CPU bound (like copying to a cache) ? That could mean that your read
>completed and your second thread got the CPU and did a few writes before
>the
>first thread got it back. There are all kinds of possibilities.
>
>
>“Ta H.” wrote in message news:xxxxx@ntdev…
> >
> > Do you mean if I CreateFile(…) the driver without
>FILE_FLAG_OVERLAPPED,
> > though I have multiple threads by the same handle, one thread has to
>wait
> > for another thread to be finished in order to get the chance to get run?
> > Even so, why one thread always seems to be dominant over the other in my
> > case?
> >
> > For the case that more than one thread is pending, do I have to use the
> > FILE_FLAG_OVERLAPPED I/O?
> >
> > >From: “Peter Wieland”
> > >Reply-To: “NT Developers Interest List”
> > >To: “NT Developers Interest List”
> > >Subject: [ntdev] Re: Multithreading and the device driver
> > >Date: Thu, 11 Jul 2002 15:19:24 -0700
> > >
> > >did you open the file for synchronous i/o?
> > >
> > >-p
> > >
> > >-----Original Message-----
> > >From: xxxxx@hotmail.com [mailto:xxxxx@hotmail.com]
> > >Sent: Thursday, July 11, 2002 2:48 PM
> > >To: NT Developers Interest List
> > >Subject: [ntdev] Re: Multithreading and the device driver
> > >
> > >
> > >In my user mode program, the printf(…) comes right before
> > >DeviceIoControl(…) which operates READs or WRITEs. My questions is
>if
> > >the READ thread is blocked and waiting for the interrupt, why does the
> > >WRITE thread not get scheduled to run? and vice versa? Also is it the
> > >scheduling round-robin? My ideal case is READs and WRITEs can happen
> > >interleavingly. Now I cannot see it that way.
> > >
> > >Thanks
> > >
> > >Ta H.
> > >
> > >—
> > >You are currently subscribed to ntdev as: xxxxx@microsoft.com To
> > >unsubscribe send a blank email to %%email.unsub%%
> > >
> > >
> > >—
> > >You are currently subscribed to ntdev as: xxxxx@hotmail.com
> > >To unsubscribe send a blank email to %%email.unsub%%
> >
> >
> >
> >
> >
> > Send and receive Hotmail on your mobile device: http://mobile.msn.com
> >
> >
> >
> >
>
>
>
>—
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to %%email.unsub%%


MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx