StartIO例程------------IoStartPacket、IoStartNextPacket、IoCancelIrp实现代码
2013-05-07 19:15
501 查看
StartIO例程------------IoStartPacket、IoStartNextPacket、IoCancelIrp实现代码
根据wdk代码做的IoStartPacket、IoStartNextPacket、IoCancelIrp简化版:VOID
IoStartPacket(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PULONG Key OPTIONAL,
IN PDRIVER_CANCEL CancelFunction OPTIONAL
)
/*++
Routine Description:
This routine attempts to start the specified packet request (IRP) on the
specified device. If the device is already busy, then the packet is
simply queued to the device queue. If a non-NULL CancelFunction is
supplied, it will be put in the IRP. If the IRP has been canceled, the
CancelFunction will be called after the IRP has been inserted into the
queue or made the current packet.
Arguments:
DeviceObject - Pointer to device object itself.
Irp - I/O Request Packet which should be started on the device.
Key - Key to be used in inserting packet into device queue; optional
(if not specified, then packet is inserted at the tail).
CancelFunction - Pointer to an optional cancel routine.
Return Value:
None.
--*/
{
KIRQL oldIrql;
KIRQL cancelIrql = PASSIVE_LEVEL;
BOOLEAN i;
// Raise the IRQL of the processor to dispatch level for synchronization.
KeRaiseIrql( DISPATCH_LEVEL, &oldIrql );
// If the driver has indicated that packets are cancelable, then acquire
// the cancel spinlock and set the address of the cancel function to
// indicate that the packet is not only cancelable, but indicates what
// routine to invoke should it be cancelled.
if (CancelFunction) {
IoAcquireCancelSpinLock( &cancelIrql );
Irp->CancelRoutine = CancelFunction;
}
i = KeInsertDeviceQueue( &DeviceObject->DeviceQueue,
&Irp->Tail.Overlay.DeviceQueueEntry );
// If the packet was not inserted into the queue, then this request is
// now the current packet for this device. Indicate so by storing its
// address in the current IRP field, and begin processing the request.
if (!i) {
DeviceObject->CurrentIrp = Irp;
if (CancelFunction) {
IoReleaseCancelSpinLock( cancelIrql );
}
// Invoke the driver's start I/O routine to get the request going on the device.
// The StartIo routine should handle the cancellation.
DeviceObject->DriverObject->DriverStartIo( DeviceObject, Irp );
} else {
// The packet was successfully inserted into the device's work queue.
// Make one last check to determine whether or not the packet has
// already been marked cancelled. If it has, then invoke the
// driver's cancel routine now. Note that because the cancel
// spinlock is currently being held, an attempt to cancel the request
// from another processor at this point will simply wait until this
// routine is finished, and then get it cancelled.
if (CancelFunction) {
if (Irp->Cancel) {
Irp->CancelIrql = cancelIrql;
Irp->CancelRoutine = (PDRIVER_CANCEL) NULL;
CancelFunction( DeviceObject, Irp );
} else {
IoReleaseCancelSpinLock( cancelIrql );
}
}
}
// Restore the IRQL back to its value upon entry to this function before
// returning to the caller.
KeLowerIrql( oldIrql );
}
VOID
IopStartNextPacket(
IN PDEVICE_OBJECT DeviceObject,
IN LOGICAL Cancelable
)
/*++
Routine Description:
This routine is invoked to dequeue the next packet (IRP) from the
specified device work queue and invoke the device driver's start I/O
routine for it. If the Cancelable parameter is TRUE, then the update of
current IRP is synchronized using the cancel spinlock.
Arguments:
DeviceObject - Pointer to device object itself.
Cancelable - Indicates that IRPs in the device queue may be cancelable.
Return Value:
None.
--*/
{
KIRQL cancelIrql = PASSIVE_LEVEL;
PIRP irp;
PKDEVICE_QUEUE_ENTRY packet;
// Begin by checking to see whether or not this driver's requests are
// to be considered cancelable. If so, then acquire the cancel spinlock.
if (Cancelable) {
IoAcquireCancelSpinLock( &cancelIrql );
}
// Clear the current IRP field before starting another request.
DeviceObject->CurrentIrp = (PIRP) NULL;
// Remove the next packet from the head of the queue. If a packet was
// found, then process it.
packet = KeRemoveDeviceQueue( &DeviceObject->DeviceQueue );
if (packet) {
irp = CONTAINING_RECORD( packet, IRP, Tail.Overlay.DeviceQueueEntry );
// A packet was located so make it the current packet for this
// device.
DeviceObject->CurrentIrp = irp;
if (Cancelable) {
IoReleaseCancelSpinLock( cancelIrql );
}
// Invoke the driver's start I/O routine for this packet.
DeviceObject->DriverObject->DriverStartIo( DeviceObject, irp );
} else {
// No packet was found, so simply release the cancel spinlock if
// it was acquired.
if (Cancelable) {
IoReleaseCancelSpinLock( cancelIrql );
}
}
}
BOOLEAN
IoCancelIrp(
IN PIRP Irp
)
{
PDRIVER_CANCEL cancelRoutine;
KIRQL irql;
BOOLEAN returnValue;
ASSERT( Irp->Type == IO_TYPE_IRP );
// Acquire the cancel spin lock.
IoAcquireCancelSpinLock( &irql );
// Set the cancel flag in the IRP.
Irp->Cancel = TRUE;
// Obtain the address of the cancel routine, and if one was specified,
// invoke it.
cancelRoutine = (PDRIVER_CANCEL) (ULONG_PTR) InterlockedExchangePointer( (PVOID *) &Irp->CancelRoutine, NULL );
if (cancelRoutine) {
Irp->CancelIrql = irql;
cancelRoutine( Irp->Tail.Overlay.CurrentStackLocation->DeviceObject,
Irp );
// The cancel spinlock should have been released by the cancel routine.
return(TRUE);
} else {
// There was no cancel routine, so release the cancel spinlock and
// return indicating the Irp was not currently cancelable.
IoReleaseCancelSpinLock( irql );
return(FALSE);
}
}
#define IoSetCancelRoutine( Irp, NewCancelRoutine ) ( /
(PDRIVER_CANCEL) (ULONG_PTR) InterlockedExchangePointer( (PVOID *) &(Irp)->CancelRoutine, (PVOID) (ULONG_PTR)(NewCancelRoutine) ) )
相关文章推荐
- StartIO例程------------IoStartPacket、IoStartNextPacket、IoCancelIrp实现代码
- IoCancelIrp,IoStartPacket,IoStartNextPacket的关系
- iOS本地存储的代码实现
- IOS UI控件代码实现——Segmentarray控件
- 手把手教你实现微信小视频iOS代码实现
- iOS 实现多代理的方法及实例代码
- iOS中代码支持多国语言切换的实现(Xcode5+iOS7)
- ios 基础学习之零基础代码控制实现1
- iOS中数据存储的几种代码实现
- ios代码实现时间设置NSDate
- iOS 判断一断代码的执行时间(从网上看的,自己实现一下)
- ios消息推送客户端和服务端实现带代码(java)
- iOS实现翻页效果动画实例代码
- iOS Mobile Development: Using Xcode Targets to Reuse the Code 使用xcode targets来实现代码复用
- iOS项目实现SVN代码管理方法③(Part 三)
- iOS实现只有底部边框线的输入框示例代码
- iOS 简约日历控件EBCalendarView的实现代码
- 用PHP ob_start()控制浏览器cache、生成html实现代码
- iOS滑动解锁、滑动获取验证码效果的实现代码
- iOS客户端本地推送实现代码