5.7.4.3. Spinlocks
- fast, busy waiting mutual exclusion mechanism
- Mechanism:
- Atomic variable is initalized to 1: available
-
spin_lock()
atomically decrements value and checks if equals 0- if yes: lock obtained
- if no: no lock, spin in tight loop until value is 1
-
spin_unlock()
sets value back to 1
- must only be used in code that cannot sleep!
- no userspace memory access
- no memory allocation (except
GFP_ATOMIC
)
- three flavors
-
spin_lock()
,spin_unlock()
- on UP, no preemption: optimized out
- on UP with preemption: disable preemption
- protects against concurrency with "regular" kernel code
-
spin_lock_irqsave
,spin_unlock_irqrestore
,spin_lock_irq
,spin_unlock_irq
- disables interrupts on local CPU
- protects against concurrency with interrupt handlers
-
spin_lock_bh
,spin_unlock_bh
- disables soft interrupt on local CPU
- protects against concurrency with bottom halves, tasklets, softirqs, ...
-
- Advantages:
- fast and simple
- Disadvantages:
- code must be atomic: can never sleep!
- unfair! solution: ticket spinlocks (merged 2.6.25)
5.7.4.2. Mutexes | 1. Denx Training Topics | 5.7.4.4. Other locking techniques | |||
Prev | Home | Next | |||