Driver Locking & W2K DDK Sample drivers

The sample ‘toaster’ driver that comes with the win2k ddk has some strange
locking going on.

Both the bus and toaster drivers keep has a counter ‘OutstandingIO’ that is
initalized to 1, and two events, StopEvent and RemoveEvent.

StopEvent is set whenever OutstandingIO <= 1 (thus it is initalized in the
“signaled” state). It is waited on for an IRP_MN_QUERY_STOP_DEVICE.

RemoveEvent is set whenever OutstandingIO == 0. It is waited on for
IRP_MN_QUERY_REMOVE_DEVICE.

While the code makes it sufficiently clear that this is what happens, I don’t
see the reason *why* there are two locks – if the device can be stopped for
resource balancing, why can’t it be removed? It would be very much as if the
device was stopped and never restarted (it seems that this may happen due to a
lack of resources?).

The sample serial driver that is provided does not seem to have this behavior –
but then, the fact that it has actual code (unlike the virtual ‘toaster’ driver)
may be hiding it.

Could someone please shed some light on this locking stuff?

  • Stevie-O

“Intuitive” mostly means “what the writer or speaker of intuitive likes”.
– Bruce Ediger

Stevie-O wrote:

Could someone please shed some light on this locking stuff?

Basically, a driver (A) has to make sure that it doesn’t allow the
driver underneath it (B) to be unloaded while that driver is processing
an IRP sent by A. The IO_REMOVE_LOCK in the kernel is one way to do
this. Toaster happens to be using another way.

You can read more than you probably wanted to know about this stuff at
pp. 328-37 of my WDM book.


Walter Oney, Consulting and Training
Basic and Advanced Driver Programming Seminars
Check out our schedule at http://www.oneysoft.com