I understand the spin lock. Small A, Small B, small C, and small d share the same room, but the room only has a Mouch and a toilet. When they want to "just", they will lock the door of the Mouch and occupy the toilet. For example, John is occupying the toilet, so they are very comfortable. It happened that John was in a hurry, but he couldn't help it because John had locked the door. As a result, Mr. B is in a hurry at the door, that is, "Spin ". Note that the word "Spin" is well used. Tom goes back to bed instead of locking the door, but "Spin" at the door ".... The final result is that A is unlocked and B is occupied. In addition, the action in the locking process is neat and neat, so that no one else can compete in front of the lock. This is a recurring process ...... Here the small a, B, c, d is the processor, and the moufang lock is the spin lock. When other processors want to access this public resource, they must first obtain the lock. If the lock is occupied, the spin (loop) waits. John's concentration indicates that IRQL is 2, and switch locks quickly indicate atomic operations. ---------------------------------------------------------- I don't know if I understand it correctly or not. Some examples may be inappropriate. If you have any misunderstanding, you may wish to give us some advice to avoid going astray and regret it. ---------------------------------------------------------- I wrote a test program and tested it: Kspin_lock spinlock; Ntstatus DriverEntry ( In pdriver_object driverobject, In punicode_string registrypath ) { Ntstatus status; Unicode_string devicename; Pdevice_object deviceobject; Handle threadhandle; Kirql oldirql; Kirql IRQL; Ulong processor; Ulong I; Deviceobject = NULL; Rtlinitunicodestring (& devicename, devicenamebuffer ); Status = iocreatedevice (driverobject, 0, & Devicename, File_device_unknown, 0, False, & Deviceobject ); If (! Nt_success (Status )) { Return status; } Driverobject-> driverunload = driverunload; Keinitializespinlock (& spinlock); // (2) Pscreatesystemthread (& threadhandle, thread_all_access, null, null, threadroutine, null ); I = 10000; Keacquirespinlock (& spinlock, & oldirql ); While (I --) { _ ASM NOP IRQL = kegetcurrentirql (); Processor = kegetcurrentprocessornumber (); Kdprint ("[% d] currentirql: \ t % d", processor, IRQL )); } Kereleasespinlock (& spinlock, oldirql ); Return status; } Void Threadroutine (in pvoid startcontext) { Kirql oldirql; Kirql IRQL; Ulong processor; Ulong I; I = 10000; Keacquirespinlock (& spinlock, & oldirql); // (1) While (I --) { _ ASM NOP IRQL = kegetcurrentirql (); Processor = kegetcurrentprocessornumber (); Kdprint ("** [% d] currentirql: \ t % d", processor, IRQL )); } Kereleasespinlock (& spinlock, oldirql); // (1) } --------------------------------------------------------------------------------- First, I am a dual-core system. If it is a single core, I want to enter the spin lock and IRQL has been raised to the DPC level, and the second thread will not be able to run. If he runs unexpectedly, a deadlock will occur. Test in several cases: 1. Test the code above. First catch the lock first, then catch the lock and then run. And IRQL during the lock is at the DPC level. 2. Remove the two rows marked with (1) The result is that two threads run simultaneously. one processor occupies [0] and the other processor occupies [1]. The locked IRQL level is DPC level. The unlocked IRQL value is 0. That is, the spin lock does not affect the normal operation of other processors. Unless Other Processors also want to obtain the lock. 3. Remove the line marked (2) (the spinlock is a global variable) The result is the same as that of 1, because the global variable is initialized to 0 by default. |