Queued Spin Locks

Queued spin locks are a variant of spin locks that work well for highly contended locks. Unqueued spinlocks are a better choice for lightly contended locks.

The driver supplies storage for the spin lock, and initializes it with KeInitializeSpinLock. The driver uses KeAcquireInStackQueuedSpinLock to acquire the queued spin lock, and KeReleaseInStackQueuedSpinLock to release it.

The driver allocates a KLOCK_QUEUE_HANDLE structure that it passes by pointer to KeAcquireInStackQueuedSpinLock. The driver passes the same structure by pointer to KeReleaseInStackQueuedSpinLock when it releases the spin lock.

Drivers should normally allocate the structure on the stack each time they acquire the lock. A driver should not allocate the structure as part of its device context and then share the same structure from multiple threads.

Drivers must not mix calls to the queued spin lock routines and the ordinary KeXxxSpinLock routines on the same spin lock.

If the driver is already at IRQL = DISPATCH_LEVEL, it can call KeAcquireInStackQueuedSpinLockAtDpcLevel and KeReleaseInStackQueuedSpinLockFromDpcLevel instead.