12.1.1 Cancel Routines in NT Drivers
Any NT driver in which IRPs can be held pending for an indefinite interval must have one or more Cancel routines.
For example, suppose a user-mode thread makes an I/O request, which is queued by a highest-level device driver’s Dispatch routine, and the requesting thread is terminated while the IRP is queued. IRPs queued on behalf of a terminated thread should be cancelled. Consequently, such a driver must set a driver-supplied Cancel routine in each IRP that it queues.
The number of Cancel routines any NT driver has depends on the driver’s design. In general, an NT driver should have a Cancel routine for each stage in its I/O processing at which an IRP might be held pending for an indefinite interval, which is also called held in a cancelable state.
NT driver writers should consider the following design guidelines:
·The highest-level driver in a chain of layered NT drivers must have at least one Cancel routine if it queues IRPs or otherwise holds IRPs in a cancelable state. It can have more than one Cancel routine at the driver writer’s discretion.
·Lower-level NT drivers in which IRPs can be held in a cancelable state for relatively long intervals also should have one or more Cancel routines.
·If a driver manages its own internal queues of IRPs, it should have a separate Cancel routine for each of its queues.
Certain highest-level NT drivers for interactive devices, such as keyboard, mouse, sound, parallel class and serial drivers, must have Cancel routines. Certain lower-level drivers, such as a parallel port driver that holds IRPs queued for some number of higher-level class drivers for relatively long intervals, also should have Cancel routines.
NT intermediate drivers that are layered over mass-storage device drivers and mass-storage device drivers are unlikely to have Cancel routines. In other words, it is the responsibility of an NT file system driver to handle the cancelation of file I/O requests, while the IRPs input to lower-level mass-storage drivers are usually processed to completion too quickly to be cancelable.