Export (0) Print
Expand All
Expand Minimize

ExEnterCriticalRegionAndAcquireResourceExclusive routine

The ExEnterCriticalRegionAndAcquireResourceExclusive routine first enters a critical region, and then acquires exclusive access to the specified resource.

Syntax


PVOID ExEnterCriticalRegionAndAcquireResourceExclusive(
  _Inout_  PERESOURCE Resource
);

Parameters

Resource [in, out]

A pointer to the resource to acquire for exclusive access. This parameter points to a caller-allocated ERESOURCE structure. The caller previously allocated this structure from nonpaged pool and initialized it by calling the ExInitializeResourceLite or ExReinitializeResourceLite routine.

Return value

A pointer to an opaque system structure. Drivers should ignore this return value.

Remarks

If this routine is called when the specified resource is not immediately available for exclusive access, the calling thread is blocked until the resource is available.

While a driver thread runs in the critical region, the driver's user APCs and normal kernel APCs are not executed. Special kernel APCs are still executed. For more information about these APC types, see Types of APCs.

By temporarily disabling normal kernel APCs, ExEnterCriticalRegionAndAcquireResourceExclusive prevents a passive-level calling thread from being preempted by an APC while the thread holds a shared resource that the APC might try to access.

The calling thread should stay in the critical region and hold the specified resource for no longer than is necessary. To release the specified resource and leave the critical region, call the ExReleaseResourceAndLeaveCriticalRegion routine.

For more information about using an ERESOURCE structure to synchronize exclusive access to a resource, see Introduction to ERESOURCE Routines. For more information about entering and leaving a critical region, see Critical Regions and Guarded Regions.

Examples

The following code example shows how your driver can use the ExEnterCriticalRegionAndAcquireResourceExclusive and ExReleaseResourceAndLeaveCriticalRegion routines to acquire exclusive access to a resource, access the resource, and then release exclusive access to the resource. The Resource variable in this example is a pointer to an ERESOURCE structure.


ExEnterCriticalRegionAndAcquireResourceExclusive(Resource);

//
// Access the resource here.
//
...

ExReleaseResourceAndLeaveCriticalRegion(Resource);


The preceding code example is equivalent in behavior to the following code example.


KeEnterCriticalRegion();
ExAcquireResourceExclusiveLite(Resource, TRUE);

//
// Access the resource here.
//
...

ExReleaseResourceLite(Resource);
KeLeaveCriticalRegion();


In other words, the call to ExEnterCriticalRegionAndAcquireResourceExclusive in the first example is equivalent to the calls to the KeEnterCriticalRegion and ExAcquireResourceExclusiveLite routines in the second example. Similarly, the call to ExReleaseResourceAndLeaveCriticalRegion is equivalent to the calls to the ExReleaseResourceLite and KeLeaveCriticalRegion routines. However, using ExEnterCriticalRegionAndAcquireResourceExclusive and ExReleaseResourceAndLeaveCriticalRegion reduces the number of calls by two, which simplifies the code and improves performance.

Requirements

Version

Available starting with Windows Vista and Windows Server 2003 with SP1.

Header

Wdm.h (include Wdm.h, Ntddk.h, Ntifs.h, or Fltkernel.h)

Library

Ntoskrnl.lib

IRQL

<= APC_LEVEL

See also

ERESOURCE
ExAcquireResourceExclusiveLite
ExInitializeResourceLite
ExReinitializeResourceLite
ExReleaseResourceLite
ExReleaseResourceAndLeaveCriticalRegion
KeEnterCriticalRegion
KeLeaveCriticalRegion

 

 

Send comments about this topic to Microsoft

Show:
© 2014 Microsoft