异步读写

测试了下异步读写,记录一下。

1. 若要进行异步读写,创建(或打开)文件时需要指定异步标志位(FILE_FLAG_OVERLAPPED)。

2. 读写函数的返回值依赖底层实现,若底层直接完成操作,则函数返回成功,User Mode代码可立即操作缓冲区。若底层挂起操作,则函数返回false,GetLastError()返回997,若  为其它的错误代码,说明底层处理失败。

3. 处理应用层发送下来的Irp,IoCompleteRequest只是调用完成例程,而不会去拷贝缓冲区、释放Irp等操作,这些操作会等待分发例程执行完毕后再去操作,也就是说,对于应用层IRP,设备栈中的下层驱动处理完IRP后,上层依然可以继续处理IRP,因为此时IRP依然存在。

4. 对于内核层创建的同步IRP,IoCompleteRequest只是调用完成例程,也会去拷贝缓冲区、释放Irp等操作。因此,IoCallDriver返回后,不应再处理IRP,因为此时IRP极可能已被销毁(IoCallDriver返回STATUS_PENDING) 或者已经被销毁(IoCallDriver返回其它代码)。

5. 对于内核层创建的异步IRP,上层驱动应该设置一个完成例程,在完成例程中设置事件、释放IRP,最后返回STATUS_MORE_PROCESSING_REQUIRED,返回这个值得目的是阻止IO Manager继续对这个驱动分配的IRP处理完成操作。代码模型如下:

SOMETYPE SomeFunction(PDEVICE_EXTENSION pdx,
  PDEVICE_OBJECT DeviceObject)
  {
  NTSTATUS status = IoAcquireRemoveLock(&pdx->RemoveLock,
    (PVOID) 42);
  if (!NT_SUCCESS(status))
    return ;
  PIRP Irp;
  Irp = IoBuildAsynchronousFsdRequest(IRP_MJ_XXX, DeviceObject,
    ...);
    -or-
  Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
  PIO_STACK_LOCATION stack = IoGetNextIrpStackLocation(Irp);
  Stack->MajorFunction = IRP_MJ_XXX;
  DeviceObject], Irp,
    (PIO_COMPLETION_ROUTINE) CompletionRoutine,
    &event, TRUE, TRUE, TRUE);
  status = IoCallDriver(DeviceObject, Irp);
  if (status == STATUS_PENDING)
    KeWaitForSingleObject(&event, Executive, KernelMode,
    FALSE, NULL);
  IoReleaseRemoveLock(&pdx->RemoveLock, (PVOID) 42);
  }

NTSTATUS CompletionRoutine(PDEVICE_OBJECT junk, PIRP Irp,
  PKEVENT pev)
  {
  if (Irp->PendingReturned)
    KeSetEvent(pev, EVENT_INCREMENT, FALSE);
  
  IoFreeIrp(Irp);
  return STATUS_MORE_PROCESSING_REQUIRED;
  }


//拷贝段wdk文档,凑个字数

A driver can call IoBuildDeviceIoControlRequest to set up IRPs for device control requests that it synchronously sends to lower-level drivers.

After calling IoBuildDeviceIoControlRequest to create a request, the driver must call IoCallDriver to send the request to the next-lower driver. If IoCallDriver returns STATUS_PENDING, the driver must wait for the completion of the IRP by calling KeWaitForSingleObject on the given Event. Most drivers do not need to set an IoCompletion routine for the IRP.

IRPs that are created by IoBuildDeviceIoControlRequest must be completed by a driver's call to IoCompleteRequest. A driver that calls IoBuildDeviceIoControlRequest must not call IoFreeIrp, because the I/O manager frees these synchronous IRPs after IoCompleteRequest has been called.

IoBuildDeviceIoControlRequest queues the IRPs that it creates to an IRP queue that is specific to the current thread. If the thread exits, the I/O manager cancels the IRP.




你可能感兴趣的:(异步读写)