If critical sections not allowed to sleep, then no choice we have to go ahead with the spinlock for that critical section to prevent the race condition, but if the critical section is allows to sleep, then we have choice to choose between mutex or spinlock based on certain points.
- After acquire a spinlock, by any other that thread must not be sleep able, it is illegal to sleep or preempt or schedule or on a wait queue.
- Mutexes puta the calling thread into sleep mode, we have no choice other than spinlocks to use inside interrupt handlers (or any kind of softirq context)
Requirement Recommended Lock
Low overhead locking Spin lock is preferred.
Short lock hold time Spin lock is preferred.
Long lock hold time Mutex is preferred.
Need to lock from interrupt context Spin lock is required.
Need to sleep while holding lock Mutex is required