0

I am new to linux field.

I have some confusion on use of spinlocks(in kernel mode) in various scenarios . Please clarify.

Spinlocks are used as busy waiting mechanisms.

1 ) Spinlocks( Uniprocessor + Non-preemptive kernel)--> Does not exist(since if you sleep while in spinlock it would cause a deadlock)

2) Spinlocks( Uniprocessor + Preemptive kernel)--> Should not be used as it waste resources.( what happens when the thread sleeps while holding the locks?? Can other processes execute as normal? )

3) Spinlocks( Multiprocessor + Preemptive kernel)--> Should be used Cautiously.( what happens when the thread sleeps while holding the locks?? Can other processes execute as normal? )

Please let me know if my understanding for above scenarios is right and please provide any extra info which would help me clarify the use of spinlocks.

Anup Buchke
  • 5,210
  • 5
  • 22
  • 38
  • Here's one I made earlier: http://stackoverflow.com/questions/6555358/linux-kernel-preemption-during-spin-lock-and-mutex-lock/6556013#6556013 – Dipstick Mar 04 '13 at 23:43
  • Hi Anup, there are many considerations on usage of spinlocks. Best place to use spinlock is in "interrupt context" where you can't sleep. In small code path where you are updating a global structure or a global variable, you can use spinlocks. Spinlocks with "irq_save" api to be used it's a best practice. – Gautham Kantharaju Mar 07 '13 at 03:05

1 Answers1

1

IIRC, in the UP case, spinlocks are for the most part ignored (other than possibly disabling preemption, and for some of the spinlock function variants, also IRQs).

In the Multiprocessor case, Preemption is again disabled, IRQs may also be.

In general, Tasks holding spinlocks should not be sleeping. (Doubly so if they just disabled interrupts)

(Disclaimer: Contents of post may be mildy/wildly inaccurate, as it's late and I can't verify details ATM)

Hasturkun
  • 35,395
  • 6
  • 71
  • 104