ReactOS 0.4.16-dev-287-g2d3f3a1
csq.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _IO_CSQ_IRP_CONTEXT
 
struct  _IO_CSQ
 

Macros

#define _CSQ_H_
 
#define IO_TYPE_CSQ_IRP_CONTEXT   1
 
#define IO_TYPE_CSQ   2
 
#define IO_TYPE_CSQ_EX   3
 

Typedefs

typedef struct _IO_CSQ IO_CSQ
 
typedef struct _IO_CSQPIO_CSQ
 
typedef struct _IO_CSQ_IRP_CONTEXT IO_CSQ_IRP_CONTEXT
 
typedef struct _IO_CSQ_IRP_CONTEXTPIO_CSQ_IRP_CONTEXT
 
typedef _In_ PIRP Irp
 
typedef IO_CSQ_INSERT_IRP * PIO_CSQ_INSERT_IRP
 
typedef IO_CSQ_REMOVE_IRP * PIO_CSQ_REMOVE_IRP
 
typedef _In_opt_ PIRP _In_opt_ PVOID PeekContext
 
typedef IO_CSQ_PEEK_NEXT_IRP * PIO_CSQ_PEEK_NEXT_IRP
 
typedef _Out_ PKIRQL Irql
 
typedef IO_CSQ_ACQUIRE_LOCK * PIO_CSQ_ACQUIRE_LOCK
 
typedef IO_CSQ_RELEASE_LOCK * PIO_CSQ_RELEASE_LOCK
 
typedef IO_CSQ_COMPLETE_CANCELED_IRP * PIO_CSQ_COMPLETE_CANCELED_IRP
 
typedef _In_ PIRP _In_ PVOID InsertContext
 
typedef IO_CSQ_INSERT_IRP_EX * PIO_CSQ_INSERT_IRP_EX
 

Functions

typedef VOID (NTAPI IO_CSQ_INSERT_IRP)(_In_ struct _IO_CSQ *Csq
 
typedef PIRP (NTAPI IO_CSQ_PEEK_NEXT_IRP)(_In_ struct _IO_CSQ *Csq
 
typedef NTSTATUS (NTAPI IO_CSQ_INSERT_IRP_EX)(_In_ struct _IO_CSQ *Csq
 
NTKERNELAPI NTSTATUS NTAPI IoCsqInitialize (_Out_ PIO_CSQ Csq, _In_ PIO_CSQ_INSERT_IRP CsqInsertIrp, _In_ PIO_CSQ_REMOVE_IRP CsqRemoveIrp, _In_ PIO_CSQ_PEEK_NEXT_IRP CsqPeekNextIrp, _In_ PIO_CSQ_ACQUIRE_LOCK CsqAcquireLock, _In_ PIO_CSQ_RELEASE_LOCK CsqReleaseLock, _In_ PIO_CSQ_COMPLETE_CANCELED_IRP CsqCompleteCanceledIrp)
 Set up a CSQ struct to initialize the queue.
 
NTKERNELAPI NTSTATUS NTAPI IoCsqInitializeEx (_Out_ PIO_CSQ Csq, _In_ PIO_CSQ_INSERT_IRP_EX CsqInsertIrpEx, _In_ PIO_CSQ_REMOVE_IRP CsqRemoveIrp, _In_ PIO_CSQ_PEEK_NEXT_IRP CsqPeekNextIrp, _In_ PIO_CSQ_ACQUIRE_LOCK CsqAcquireLock, _In_ PIO_CSQ_RELEASE_LOCK CsqReleaseLock, _In_ PIO_CSQ_COMPLETE_CANCELED_IRP CsqCompleteCanceledIrp)
 Set up a CSQ struct to initialize the queue (extended version)
 
NTKERNELAPI VOID NTAPI IoCsqInsertIrp (_Inout_ PIO_CSQ Csq, _Inout_ PIRP Irp, _Out_opt_ PIO_CSQ_IRP_CONTEXT Context)
 Insert an IRP into the CSQ.
 
NTKERNELAPI NTSTATUS NTAPI IoCsqInsertIrpEx (_Inout_ PIO_CSQ Csq, _Inout_ PIRP Irp, _Out_opt_ PIO_CSQ_IRP_CONTEXT Context, _In_opt_ PVOID InsertContext)
 Insert an IRP into the CSQ, with additional tracking context.
 
NTKERNELAPI PIRP NTAPI IoCsqRemoveIrp (_Inout_ PIO_CSQ Csq, _Inout_ PIO_CSQ_IRP_CONTEXT Context)
 Remove anb IRP from the queue.
 
NTKERNELAPI PIRP NTAPI IoCsqRemoveNextIrp (_Inout_ PIO_CSQ Csq, _In_opt_ PVOID PeekContext)
 IoCsqRemoveNextIrp - Removes the next IRP from the queue.
 

Macro Definition Documentation

◆ _CSQ_H_

#define _CSQ_H_

Definition at line 57 of file csq.h.

◆ IO_TYPE_CSQ

#define IO_TYPE_CSQ   2

Definition at line 79 of file csq.h.

◆ IO_TYPE_CSQ_EX

#define IO_TYPE_CSQ_EX   3

Definition at line 238 of file csq.h.

◆ IO_TYPE_CSQ_IRP_CONTEXT

#define IO_TYPE_CSQ_IRP_CONTEXT   1

Definition at line 78 of file csq.h.

Typedef Documentation

◆ InsertContext

Definition at line 258 of file csq.h.

◆ IO_CSQ

Definition at line 69 of file csq.h.

◆ IO_CSQ_IRP_CONTEXT

◆ Irp

Initial value:
{
return ((PFN_WDFDEVICEWDMDISPATCHPREPROCESSEDIRP) WdfFunctions[WdfDeviceWdmDispatchPreprocessedIrpTableIndex])(WdfDriverGlobals, Device, Irp)
_In_ PIRP Irp
Definition: csq.h:116
#define WdfFunctions
Definition: wdf.h:66
_Must_inspect_result_ _In_ WDFDEVICE Device
Definition: wdfchildlist.h:474
PWDF_DRIVER_GLOBALS WdfDriverGlobals
@ WdfDeviceWdmDispatchPreprocessedIrpTableIndex
Definition: wdffuncenum.h:59

Definition at line 116 of file csq.h.

◆ Irql

typedef _In_ KIRQL Irql

Definition at line 179 of file csq.h.

◆ PeekContext

Definition at line 160 of file csq.h.

◆ PIO_CSQ

typedef struct _IO_CSQ * PIO_CSQ

Definition at line 69 of file csq.h.

◆ PIO_CSQ_ACQUIRE_LOCK

typedef IO_CSQ_ACQUIRE_LOCK* PIO_CSQ_ACQUIRE_LOCK

Definition at line 180 of file csq.h.

◆ PIO_CSQ_COMPLETE_CANCELED_IRP

typedef IO_CSQ_COMPLETE_CANCELED_IRP* PIO_CSQ_COMPLETE_CANCELED_IRP

Definition at line 217 of file csq.h.

◆ PIO_CSQ_INSERT_IRP

typedef IO_CSQ_INSERT_IRP* PIO_CSQ_INSERT_IRP

Definition at line 117 of file csq.h.

◆ PIO_CSQ_INSERT_IRP_EX

typedef IO_CSQ_INSERT_IRP_EX* PIO_CSQ_INSERT_IRP_EX

Definition at line 259 of file csq.h.

◆ PIO_CSQ_IRP_CONTEXT

◆ PIO_CSQ_PEEK_NEXT_IRP

typedef IO_CSQ_PEEK_NEXT_IRP* PIO_CSQ_PEEK_NEXT_IRP

Definition at line 161 of file csq.h.

◆ PIO_CSQ_RELEASE_LOCK

typedef IO_CSQ_RELEASE_LOCK* PIO_CSQ_RELEASE_LOCK

Definition at line 196 of file csq.h.

◆ PIO_CSQ_REMOVE_IRP

typedef IO_CSQ_REMOVE_IRP* PIO_CSQ_REMOVE_IRP

Definition at line 135 of file csq.h.

Function Documentation

◆ IoCsqInitialize()

NTKERNELAPI NTSTATUS NTAPI IoCsqInitialize ( _Out_ PIO_CSQ  Csq,
_In_ PIO_CSQ_INSERT_IRP  CsqInsertIrp,
_In_ PIO_CSQ_REMOVE_IRP  CsqRemoveIrp,
_In_ PIO_CSQ_PEEK_NEXT_IRP  CsqPeekNextIrp,
_In_ PIO_CSQ_ACQUIRE_LOCK  CsqAcquireLock,
_In_ PIO_CSQ_RELEASE_LOCK  CsqReleaseLock,
_In_ PIO_CSQ_COMPLETE_CANCELED_IRP  CsqCompleteCanceledIrp 
)

Set up a CSQ struct to initialize the queue.

Parameters
Csq- Caller-allocated non-paged space for our IO_CSQ to be initialized
CsqInsertIrp- Insert routine
CsqRemoveIrp- Remove routine
CsqPeekNextIrp- Routine to paeek at the next IRP in queue
CsqAcquireLock- Acquire the queue's lock
CsqReleaseLock- Release the queue's lock
CsqCompleteCanceledIrp- Routine to complete IRPs when they are canceled
Returns
  • STATUS_SUCCESS in all cases
Note
  • Csq must be non-paged, as the queue is manipulated with a held spinlock

Definition at line 103 of file csq.c.

111{
120
121 return STATUS_SUCCESS;
122}
#define IO_TYPE_CSQ
Definition: csq.h:79
IO_CSQ Csq
Definition: csqrtns.c:46
#define NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65
PIO_CSQ_RELEASE_LOCK CsqReleaseLock
Definition: csq.h:228
PIO_CSQ_ACQUIRE_LOCK CsqAcquireLock
Definition: csq.h:227
PVOID ReservePointer
Definition: csq.h:230
PIO_CSQ_COMPLETE_CANCELED_IRP CsqCompleteCanceledIrp
Definition: csq.h:229
PIO_CSQ_REMOVE_IRP CsqRemoveIrp
Definition: csq.h:225
ULONG Type
Definition: csq.h:223
PIO_CSQ_PEEK_NEXT_IRP CsqPeekNextIrp
Definition: csq.h:226
PIO_CSQ_INSERT_IRP CsqInsertIrp
Definition: csq.h:224
_In_ PIO_CSQ_INSERT_IRP _In_ PIO_CSQ_REMOVE_IRP _In_ PIO_CSQ_PEEK_NEXT_IRP _In_ PIO_CSQ_ACQUIRE_LOCK _In_ PIO_CSQ_RELEASE_LOCK _In_ PIO_CSQ_COMPLETE_CANCELED_IRP CsqCompleteCanceledIrp
Definition: iofuncs.h:1893
_In_ PIO_CSQ_INSERT_IRP CsqInsertIrp
Definition: iofuncs.h:1888
_In_ PIO_CSQ_INSERT_IRP _In_ PIO_CSQ_REMOVE_IRP _In_ PIO_CSQ_PEEK_NEXT_IRP CsqPeekNextIrp
Definition: iofuncs.h:1890
_In_ PIO_CSQ_INSERT_IRP _In_ PIO_CSQ_REMOVE_IRP _In_ PIO_CSQ_PEEK_NEXT_IRP _In_ PIO_CSQ_ACQUIRE_LOCK CsqAcquireLock
Definition: iofuncs.h:1891
_In_ PIO_CSQ_INSERT_IRP _In_ PIO_CSQ_REMOVE_IRP CsqRemoveIrp
Definition: iofuncs.h:1889
_In_ PIO_CSQ_INSERT_IRP _In_ PIO_CSQ_REMOVE_IRP _In_ PIO_CSQ_PEEK_NEXT_IRP _In_ PIO_CSQ_ACQUIRE_LOCK _In_ PIO_CSQ_RELEASE_LOCK CsqReleaseLock
Definition: iofuncs.h:1892

Referenced by DriverEntry(), MsfsCreateMailslot(), and USBPORT_StartDevice().

◆ IoCsqInitializeEx()

NTKERNELAPI NTSTATUS NTAPI IoCsqInitializeEx ( _Out_ PIO_CSQ  Csq,
_In_ PIO_CSQ_INSERT_IRP_EX  CsqInsertIrpEx,
_In_ PIO_CSQ_REMOVE_IRP  CsqRemoveIrp,
_In_ PIO_CSQ_PEEK_NEXT_IRP  CsqPeekNextIrp,
_In_ PIO_CSQ_ACQUIRE_LOCK  CsqAcquireLock,
_In_ PIO_CSQ_RELEASE_LOCK  CsqReleaseLock,
_In_ PIO_CSQ_COMPLETE_CANCELED_IRP  CsqCompleteCanceledIrp 
)

Set up a CSQ struct to initialize the queue (extended version)

Parameters
Csq- Caller-allocated non-paged space for our IO_CSQ to be initialized
CsqInsertIrpEx- Extended insert routine
CsqRemoveIrp- Remove routine
CsqPeekNextIrp- Routine to paeek at the next IRP in queue
CsqAcquireLock- Acquire the queue's lock
CsqReleaseLock- Release the queue's lock
CsqCompleteCanceledIrp- Routine to complete IRPs when they are canceled
Returns
  • STATUS_SUCCESS in all cases
Note
  • Csq must be non-paged, as the queue is manipulated with a held spinlock

Definition at line 143 of file csq.c.

151{
160
161 return STATUS_SUCCESS;
162}
IO_CSQ_INSERT_IRP * PIO_CSQ_INSERT_IRP
Definition: csq.h:117
#define IO_TYPE_CSQ_EX
Definition: csq.h:238
NTSTATUS NTAPI CsqInsertIrpEx(PIO_CSQ Csq, PIRP Irp, PVOID InsertContext)
Definition: csqtest.c:82

Referenced by FsRtlPrivateLock(), and InitializeMessageWaiterQueue().

◆ IoCsqInsertIrp()

NTKERNELAPI VOID NTAPI IoCsqInsertIrp ( _Inout_ PIO_CSQ  Csq,
_Inout_ PIRP  Irp,
_Out_opt_ PIO_CSQ_IRP_CONTEXT  Context 
)

Insert an IRP into the CSQ.

Parameters
Csq- Pointer to the initialized CSQ
Irp- Pointer to the IRP to queue
Context- Context record to track the IRP while queued
Returns
  • Just passes through to IoCsqInsertIrpEx, with no InsertContext

Definition at line 177 of file csq.c.

181{
183}
NTSTATUS NTAPI IoCsqInsertIrpEx(_Inout_ PIO_CSQ Csq, _Inout_ PIRP Irp, _Out_opt_ PIO_CSQ_IRP_CONTEXT Context, _In_opt_ PVOID InsertContext)
Insert an IRP into the CSQ, with additional tracking context.
Definition: csq.c:205

Referenced by DeviceIoctl(), DispatchReadWrite(), MsfsRead(), ReadWrite(), USBPORT_DoIdleNotificationCallback(), USBPORT_HandleSubmitURB(), and USBPORT_IdleNotification().

◆ IoCsqInsertIrpEx()

NTKERNELAPI NTSTATUS NTAPI IoCsqInsertIrpEx ( _Inout_ PIO_CSQ  Csq,
_Inout_ PIRP  Irp,
_Out_opt_ PIO_CSQ_IRP_CONTEXT  Context,
_In_opt_ PVOID  InsertContext 
)

Insert an IRP into the CSQ, with additional tracking context.

Parameters
Csq- Pointer to the initialized CSQ
Irp- Pointer to the IRP to queue
Context- Context record to track the IRP while queued
InsertContext- additional data that is passed through to CsqInsertIrpEx
Note
  • Passes the additional context through to the driver-supplied callback, which can be used with more sophistocated queues
  • Marks the IRP pending in all cases
  • Guaranteed to not queue a canceled IRP
  • This is complicated logic, and is patterend after the Microsoft library. I'm sure I have gotten the details wrong on a fine point or two, but basically this works with the MS-supplied samples.

Definition at line 205 of file csq.c.

210{
211 NTSTATUS Retval = STATUS_SUCCESS;
212 KIRQL Irql;
213
215
216 do
217 {
218 /* mark all irps pending -- says so in the cancel sample */
220
221 /* set up the context if we have one */
222 if(Context)
223 {
225 Context->Irp = Irp;
226 Context->Csq = Csq;
227 Irp->Tail.Overlay.DriverContext[3] = Context;
228 }
229 else
230 Irp->Tail.Overlay.DriverContext[3] = Csq;
231
232 /*
233 * NOTE! This is very sensitive to order. If you set the cancel routine
234 * *before* you queue the IRP, our cancel routine will get called back for
235 * an IRP that isn't in its queue.
236 *
237 * There are three possibilities:
238 * 1) We get an IRP, we queue it, and it is valid the whole way
239 * 2) We get an IRP, and the IO manager cancels it before we're done here
240 * 3) We get an IRP, queue it, and the IO manager cancels it.
241 *
242 * #2 is is a booger.
243 *
244 * When the IO manger receives a request to cancel an IRP, it sets the cancel
245 * bit in the IRP's control byte to TRUE. Then, it looks to see if a cancel
246 * routine is set. If it isn't, the IO manager just returns to the caller.
247 * If there *is* a routine, it gets called.
248 *
249 * If we test for cancel first and then set the cancel routine, there is a spot
250 * between test and set that the IO manager can cancel us without our knowledge,
251 * so we miss a cancel request. That is bad.
252 *
253 * If we set a routine first and then test for cancel, we race with our completion
254 * routine: We set the routine, the IO Manager sets cancel, we test cancel and find
255 * it is TRUE. Meanwhile the IO manager has called our cancel routine already, so
256 * we can't complete the IRP because it'll rip it out from under the cancel routine.
257 *
258 * The IO manager does us a favor though: it nulls out the cancel routine in the IRP
259 * before calling it. Therefore, if we test to see if the cancel routine is NULL
260 * (after we have just set it), that means our own cancel routine is already working
261 * on the IRP, and we can just return quietly. Otherwise, we have to de-queue the
262 * IRP and cancel it ourselves.
263 *
264 * We have to go through all of this mess because this API guarantees that we will
265 * never return having left a canceled IRP in the queue.
266 */
267
268 /* Step 1: Queue the IRP */
269 if(Csq->Type == IO_TYPE_CSQ)
271 else
272 {
274 Retval = pCsqInsertIrpEx(Csq, Irp, InsertContext);
275 if(Retval != STATUS_SUCCESS)
276 break;
277 }
278
279 /* Step 2: Set our cancel routine */
280 (void)IoSetCancelRoutine(Irp, IopCsqCancelRoutine);
281
282 /* Step 3: Deal with an IRP that is already canceled */
283 if(!Irp->Cancel)
284 break;
285
286 /*
287 * Since we're canceled, see if our cancel routine is already running
288 * If this is NULL, the IO Manager has already called our cancel routine
289 */
291 break;
292
293
294 Irp->Tail.Overlay.DriverContext[3] = 0;
295
296 /* OK, looks like we have to de-queue and complete this ourselves */
299
300 if(Context)
301 Context->Irp = NULL;
302 }
303 while(0);
304
306
307 return Retval;
308}
LONG NTSTATUS
Definition: precomp.h:26
_In_ PIRP _In_ PVOID InsertContext
Definition: csq.h:258
_Out_ PKIRQL Irql
Definition: csq.h:179
IO_CSQ_INSERT_IRP_EX * PIO_CSQ_INSERT_IRP_EX
Definition: csq.h:259
#define IO_TYPE_CSQ_IRP_CONTEXT
Definition: csq.h:78
UCHAR KIRQL
Definition: env_spec_w32.h:591
IoMarkIrpPending(Irp)
IoSetCancelRoutine(Irp, CancelRoutine)

Referenced by FsRtlFastUnlockSingle(), and FsRtlPrivateLock().

◆ IoCsqRemoveIrp()

NTKERNELAPI PIRP NTAPI IoCsqRemoveIrp ( _Inout_ PIO_CSQ  Csq,
_Inout_ PIO_CSQ_IRP_CONTEXT  Context 
)

Remove anb IRP from the queue.

Parameters
Csq- Queue to remove the IRP from
Context- Context record containing the IRP to be dequeued
Returns
  • Pointer to an IRP if we found it
Note
  • Don't forget that we can be canceled any time up to the point where we unset our cancel routine

Definition at line 326 of file csq.c.

329{
330 KIRQL Irql;
331 PIRP Irp = NULL;
332
334
335 do
336 {
337 /* It's possible that this IRP could have been canceled */
338 Irp = Context->Irp;
339
340 if(!Irp)
341 break;
342
343 ASSERT(Context->Csq == Csq);
344
345 /* Unset the cancel routine and see if it has already been canceled */
347 {
348 /*
349 * already gone, return NULL --> NOTE that we cannot touch this IRP *or* the context,
350 * since the context is being simultaneously twiddled by the cancel routine
351 */
352 Irp = NULL;
353 break;
354 }
355
356 ASSERT(Context == Irp->Tail.Overlay.DriverContext[3]);
357
358 /* This IRP is valid and is ours. Dequeue it, fix it up, and return */
360
361 Context = (PIO_CSQ_IRP_CONTEXT)InterlockedExchangePointer(&Irp->Tail.Overlay.DriverContext[3], NULL);
362
364 {
365 Context->Irp = NULL;
366
367 ASSERT(Context->Csq == Csq);
368 }
369
370 Irp->Tail.Overlay.DriverContext[3] = 0;
371 }
372 while(0);
373
375
376 return Irp;
377}
struct _IO_CSQ_IRP_CONTEXT * PIO_CSQ_IRP_CONTEXT
#define InterlockedExchangePointer(Target, Value)
Definition: dshow.h:45
#define ASSERT(a)
Definition: mode.c:44

Referenced by MsfsTimeout().

◆ IoCsqRemoveNextIrp()

NTKERNELAPI PIRP NTAPI IoCsqRemoveNextIrp ( _Inout_ PIO_CSQ  Csq,
_In_opt_ PVOID  PeekContext 
)

IoCsqRemoveNextIrp - Removes the next IRP from the queue.

Parameters
Csq- Queue to remove the IRP from
PeekContext- Identifier of the IRP to be removed
Returns
Pointer to the IRP that was removed, or NULL if one could not be found
Note
  • This function is sensitive to yet another race condition. The basic idea is that we have to return the first IRP that we get that matches the PeekContext >that is not already canceled<. Therefore, we have to do a trick similar to the one done in Insert above.

Definition at line 398 of file csq.c.

401{
402 KIRQL Irql;
403 PIRP Irp = NULL;
405
407
408 while((Irp = Csq->CsqPeekNextIrp(Csq, Irp, PeekContext)))
409 {
410 /*
411 * If the cancel routine is gone, we're already canceled,
412 * and are spinning on the queue lock in our own cancel
413 * routine. Move on to the next candidate. It'll get
414 * removed by the cance routine.
415 */
417 continue;
418
420
421 /* Unset the context stuff and return */
422 Context = (PIO_CSQ_IRP_CONTEXT)InterlockedExchangePointer(&Irp->Tail.Overlay.DriverContext[3], NULL);
423
425 {
426 Context->Irp = NULL;
427
428 ASSERT(Context->Csq == Csq);
429 }
430
431 Irp->Tail.Overlay.DriverContext[3] = 0;
432
433 break;
434 }
435
437
438 return Irp;
439}
_In_opt_ PIRP _In_opt_ PVOID PeekContext
Definition: csq.h:160

Referenced by DispatchCreateCloseCleanup(), DispatchIoctl(), FsRtlFastUnlockSingle(), FsRtlUninitializeFileLock(), MsfsWrite(), QueueThread(), USBPORT_BadRequestFlush(), USBPORT_CompletePendingIdleIrp(), and USBPORT_DoIdleNotificationCallback().

◆ NTSTATUS()

typedef NTSTATUS ( NTAPI  IO_CSQ_INSERT_IRP_EX)

◆ PIRP()

typedef PIRP ( NTAPI  IO_CSQ_PEEK_NEXT_IRP)

◆ VOID()

typedef VOID ( NTAPI  IO_CSQ_INSERT_IRP)