3.3 Using Spin Locks in a NIC Driver

A Windows NT spin lock provides a mechanism for protecting resources shared by several kernel-mode threads running in either a uniprocessor or a multiprocessor environment. A spin lock is a Windows NT kernel data structure located in memory (shared memory for a multiprocessor environment) and containing a test-and-set lock field. The spin lock mechanism handles synchronization between multiple processors and various threads of processes running on a single-processor machine. A process or thread acquires a spin lock to access protected resources by successfully testing and setting this field in a non-interruptible operation. The spin lock keeps any process or thread but the one holding the spin lock from using the resources. It locks them out and forces them to loop until the owning processor releases the lock.

Another component of the spin lock mechanism is the I/O request level (IRQL) of the executing thread. Attempted acquisition of a spin lock temporarily raises the IRQL of the executing thread to the IRQL associated with the spin lock. This prevents all lower-IRQL threads on the same processor from preempting the executing thread. Threads running at a higher IRQL can preempt the executing thread, but these threads cannot acquire the spin lock because it has a lower IRQL than they do. Therefore, no other threads on the same processor attempt to acquire the spin lock until it has been both acquired and released by the executing thread. Additionally, the test-and-set lock field in the spin lock prevents threads on other processors from acquiring the spin lock until the executing thread releases it. A well-written network driver will minimize the amount of time a spin lock is required.