NdisAllocateSpinLock

VOID
    NdisAllocateSpinLock(

        IN PNDIS_SPIN_LOCK  SpinLock
        );

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

Parameters

SpinLock
Points to an opaque variable that represents a spin lock.

Comments

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, a driver should not pass the same spin lock to every Ndis..SpinLock and NdisInterlockedXxx it calls subsequently. Thus, any 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.

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 nonISR functions share with its MiniportISR or MiniportDisableInterrupt function. To access resources shared with a MiniportISR or MiniportDisableInterrupt function, a miniport 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.

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

See Also

DriverEntry of NDIS Protocol Drivers, MiniportDisableInterrupt, MiniportHalt, MiniportInitialize, MiniportISR, MiniportTimer, NdisAcquireSpinLock, NdisDprAcquireSpinLock, NdisDprReleaseSpinLock, NdisFreeSpinLock, NdisInterlockedAddUlong, NdisInterlockedInsertHeadList, NdisInterlockedInsertTailList, NdisInterlockedRemoveHeadList, NdisMSynchronizeWithInterrupt, NdisReleaseSpinLock