English 中文(简体)
spin_lock on non-preemtive linux kernels
原标题:

I read that on a system with 1 CPU and non preemtive linux kernel (2.6.x) a spin_lock call is equivalent to an empty call, and thus implemented that way.

I can t understand that: shouldn t it be equivalent to a sleep on a mutex? Even on non-preemtive kernels interrupt handlers may still be executed for example or I might call a function that would put the original thread to sleep. So it s not true that an empty spin_lock call is "safe" as it would be if it was implemented as a mutex.

Is there something I don t get?

最佳回答

Quoted from «Linux Device Drivers», by Jonathan Corbet, Alessandro Rubini and Greg Kroah-Hartman:

If a nonpreemptive uniprocessor system ever went into a spin on a lock, it would spin forever; no other thread would ever be able to obtain the CPU to release the lock (because it couldn t yield). Because of this, spinlock operations on uniprocessor systems without preemption enabled are optimized to do nothing, with the exception of the ones that change the IRQ masking status (in Linux, that would be spin_lock_irqsave()). Because of preemption, even if you never expect your code to run on an SMP system, you still need to implement proper locking.

If you re interested in a spinlock that can be taken by code running in interrupt context (hardware or software), you must use a form of spin_lock_* that disables interrupts. Not doing so will deadlock the system as soon as an interrupt arrives while you have entered your critical section.

问题回答

If you were to use spin_lock() on a non-preemptive kernel to shield data against an interrupt handler, you d deadlock (on a single-processor machine).

If the interrupt handler runs while other kernel code holds the lock, it will spin forever, as there is no way for the regular kernel code to resume and release the lock.

Spinlocks can only be used if the lock holder can always run to completion.

The solution for a lock that might be wanted by an interrupt handler is to use spin_lock_irqsave(), which disables interrupts while the spinlock is held. With 1 cpu, no interrupt handler can run, so there will not be a deadlock. On smp, an interrupt handler might start spinning on another cpu, but since the cpu holding the lock can t be interrupted, the lock will eventually be released.

To answer the two parts of your question:

Even on non-preemtive kernels interrupt handlers may still be executed for example ...

spin_lock() isn t supposed to protect against interrupt handlers - only user context kernel code. spin_lock_irqsave() is the interrupt-disabling version, and this isn t a no-op on a non-preemptive uniprocessor.

...or I might call a function that would put the original thread to sleep.

It is not allowed to sleep while holding a spin lock. This is the "Scheduling while atomic" bug. If you want to sleep, you have to use a mutex instead (again - these aren t a no-op on non-preemptive uniprocessor).

By definition, if you re using a non-preemptive kernel, you won t be preempted. If you do your own multitasking, that s not the kernel s problem; that s your problem. Interrupt handlers may still be executed, but they won t cause context switches.





相关问题
iPhone app running while screen locked

Here is something I am trying desperately to get to work: I have an app that polls the GPS module in specified intervals and then sends out coords out to a server using Unix calls such as write(); It ...

Threading and un-safe variables

I have code listed here: Threading and Sockets. The answer to that question was to modify isListening with volatile. As I remarked, that modifier allowed me to access the variable from another thread....

mySQL - Apply a row level lock using mysqli

Using PHP s mysqli how do you apply a row level lock? Row level locks stop anyone editing currently present rows that match your criteria right? but do they stop a user inserting a row which matches ...

How are mutex and lock structures implemented?

I understand the concept of locks, mutex and other synchronization structures, but how are they implemented? Are they provided by the OS, or are these structures dependent on special CPU instructions ...

Determine thread which holds the lock on file

I know there is no WINAPI which would do it, but if a thread is hung and holds an open handle of file. how do we determine the thread id and Terminate it within our processes. I m not talking about ...

xml Column update and Locking in Sql Server

I have a few windwos services. They get xml column from Sql server manipulate and update it. Service A- Gets XML Service B- Gets XML Service A- Updates XML (it will be lost) Service B- Updates XML I ...

热门标签