DPC and APC

来源:互联网 发布:c语言将华氏温度 编辑:程序博客网 时间:2024/05/23 19:25

An asynchronous procedure call (APC) is a function that executes asynchronously. APCs are similar to deferred procedure calls (DPCs), but unlike DPCs,APCs execute within the context of a particular thread . Drivers (other than file systems and file-system filter drivers) do not use APCs directly, but other parts of the operating system do, so you need to be aware of how APCs work.

 

The Windows operating system uses three kinds of APCs:

  • User APCs run strictly in user mode and only when the current thread is in an alertable wait state. The operating system uses user APCs to implement mechanisms such as overlapped I/O and the QueueUserApc Win32 routine.
  • Normal kernel APCs run in kernel mode at IRQL = PASSIVE_LEVEL. A normal kernel APC preempts all user-mode code, including user APCs. Normal kernel APCs are generally used by file systems and file-system filter drivers.
  • Special kernel APCs run in kernel mode at IRQL = APC_LEVEL. A special kernel APC preempts user-mode code and kernel-mode code that executes at IRQL = PASSIVE_LEVEL, including both user APCs and normal kernel APCs. The operating system uses special kernel APCs to handle operations such as I/O request completion. 

Deferred procedure calls (DPCs). Any driver that has an ISR typically also has at least oneDpcForIsr or CustomDpc routine to complete processing of interrupt-driven I/O operations. A typical lowest-level driver'sDpcForIsr or CustomDpc routine does the following:

  • Finishes handling an I/O operation that the ISR began processing.
  • Dequeues the next IRP so that the driver can begin processing it.
  • Completes the current IRP, if possible.

Sometimes the current IRP can't be completed because several data transfers are required, or a recoverable error was detected. In these cases, theDpcForIsr or CustomDpc routine typically reprograms the device for either another transfer or a retry of the last operation.

 

DPC在Driver的中断处理中经常使用。ISR通过调用IoRequestDpc激活DPC routine,处理中断引起而不能在ISR中处理的I/O操作。这些操作大多需要较长时间,且不能在较高的IRQL上运行。

 

APC在底层Driver中较少使用,多用于User APP和File System Driver中。和DPC的最大区别在于APC运行在某个特定线程的上下文中,而DPC运行在任意线程上下文(Arbitrary Thread Context)中。

原创粉丝点击