Partager via


NdisAllocateSpinLock (NDIS 5.1) function

Note   NDIS 5. x has been deprecated and is superseded by NDIS 6. x. For new NDIS driver development, see Network Drivers Starting with Windows Vista. For information about porting NDIS 5. x drivers to NDIS 6. x, see Porting NDIS 5.x Drivers to NDIS 6.0.

NdisAllocateSpinLock initializes a variable of type NDIS_SPIN_LOCK, used to synchronize access to resources shared among non-ISR driver functions.

Syntax

VOID NdisAllocateSpinLock(
  _In_ PNDIS_SPIN_LOCK SpinLock
);

Parameters

  • SpinLock [in]
    Pointer to an opaque variable that represents a spin lock.

Return value

None

Remarks

Before a driver calls NdisAcquireSpinLock, NdisDprAcquireSpinLock, or any of the NdisInterlockedXxx functions, it must call NdisAllocateSpinLock to initialize the spin lock passed as a required parameter to these NdisXxx functions. The caller must provide nonpaged storage for the variable at SpinLock.

After calling NdisAllocateSpinLock, the driver can call NdisAcquireSpinLock to obtain exclusive use of the resource(s) the spin lock protects. When resource access is complete, the driver calls NdisReleaseSpinLock so that other driver functions can access the resource(s) protected by that spin lock.

As a general rule, to improve performance a driver should use different locks to protect different critical sections. Thus, a driver might initialize more than one spin lock with NdisAllocateSpinLock.

Each spin lock that a driver allocates protects a discrete set of shared resources from simultanous access by driver functions that run at IRQL <= DISPATCH_LEVEL. For example, a driver that maintains an internal queue of packets might initialize one spin lock to protect its queue and another to protect a set of state variables that several driver functions, not including the MiniportISR or MiniportDisableInterrupt function, access while the driver is processing packets.

NdisAcquireSpinLock raises the IRQL to DISPATCH_LEVEL and stores the old IRQL in the spin lock. Releasing the spin lock sets the IRQL to the value stored in the spin lock. Because NDIS sometimes enters drivers at PASSIVE_LEVEL, problems can arise with the following code:

NdisAcquireSpinLock(A);
NdisAcquireSpinLock(B);
NdisReleaseSpinLock(A);
NdisReleaseSpinLock(B);

A driver should not access spin locks in this sequence for the following reasons:

  • Between NdisReleaseSpinLock(A) and NdisReleaseSpinLock(B) the code is running at PASSIVE_LEVEL instead of DISPATCH_LEVEL and is subject to inappropriate interruption.

  • After NdisReleaseSpinLock(B) the code is running at DISPATCH_LEVEL which could cause the caller to fault at much later time with an IRQL_NOT_LESS_OR_EQUAL stop error.

A driver should never use two spin locks to protect the same (sub)set of resources because nested spin lock acquisitions so frequently cause deadlocks. Even if a driver could be designed to prevent deadlocks, nested spin lock acquisitions have an adverse effect on driver performance and I/O throughput.

A miniport driver cannot use a spin lock to protect resources that its non-ISR functions share with its MiniportISR or MiniportDisableInterrupt function. To access resources shared with a MiniportISR or MiniportDisableInterrupt function, a miniport driver must call NdisMSynchronizeWithInterrupt to have its MiniportSynchronizeISR function access those resources at DIRQL.

When a driver no longer requires resource protection, for example, when a NIC is being removed and the driver is releasing the resources it allocated for that NIC, the driver calls NdisFreeSpinLock.

Freeing a spin lock and releasing a spin lock are potentially confusing. NdisFreeSpinLock clears the memory at SpinLock so it no longer represents a spin lock. Releasing an acquired spin lock with NdisReleaseSpinLock simply allows another thread of execution to acquire that spin lock.

For more information about acquiring and releasing NDIS spin locks, see Synchronization and Notification in Network Drivers.

Callers of NdisAllocateSpinLock can run at any IRQL. Usually a caller is running at IRQL = PASSIVE_LEVEL during initialization.

Requirements

Target platform

Universal

Version

See NdisAllocateSpinLock.

Header

Ndis.h (include Ndis.h)

Library

Ndis.lib

IRQL

Any level (see Remarks section)

See also

DriverEntry of NDIS Protocol Drivers

MiniportDisableInterrupt

MiniportHalt

MiniportInitialize

MiniportISR

MiniportTimer

NdisAcquireSpinLock

NdisDprAcquireSpinLock

NdisDprReleaseSpinLock

NdisFreeSpinLock

NdisInterlockedAddUlong

NdisInterlockedInsertHeadList

NdisInterlockedInsertTailList

NdisInterlockedRemoveHeadList

NdisMSynchronizeWithInterrupt

NdisReleaseSpinLock

 

 

Send comments about this topic to Microsoft