CPU interrupt when another process spins to lock

There is one direct lock in my code that is shared between two threads. When one thread holds a lock and another thread tries to get a lock, the second thread will continue to spin on the processor. So, what happens if the interrupt happens on the processor, where the thread spins to lock?

I used spin_lock() to lock, not spin_lock_irqsave() , because I do not want to disable the interrupt on the local processor.

When I checked the spin_lock() function code in the kernel, I found that preemption was disabled by default, not IRQ. Therefore, I assume that interruption will take precedence over spinning thread. So, what would it mean that a back lock is not a sleeping lock?

+5
source share
1 answer

So, what happens if the interrupt happens on the processor, where the thread spins to lock?

Interruption will happen. There is no reason for not doing this. After the interrupt returns, the process returns to rotation.

So, what would it mean that a back lock is not a sleeping lock?

This means that threads waiting for a lock to be released will start a hard cycle by checking the status of the lock instead of sleeping so that other threads use the processor.

Sleeping is what happens when a thread gives the processor either as a result of a request for a resource that is not available, or as a result of anticipation.

Prevention is disabled for threads that hold the spin lock (note: not threads that spin waiting for a lock), because it would be a disaster for a thread that holds a spin lock to go to sleep. Imagine two CPU systems in which one thread holds the lock and the other rotates, and the first thread is unloaded for the third thread, which is also trying to get the lock. Suddenly you have two threads spinning and the system will stop efficiently until one of them changes. In the worst case, the system will shut down.

Spin locks can disable interrupts because interrupt routines are allowed to acquire spin locks. If an interrupt attempts to obtain a lock lock that is held by a thread running on the same processor, that processor will be locked.

Here is a good resource for more information on spin locks.

http://www.makelinux.net/ldd3/chp-5-sect-5

+5
source

Source: https://habr.com/ru/post/1265951/


All Articles