Search code examples
linuxlinux-kernelkernelqueuespinlock

c - kernel - spinlocks vs queues


I think, no matter the whole lot of documentation available, I don't understand why one have to wait for a spin lock in a kernel context.

Why isn't there a specific queue with process requiring a lock with an atomic counter/index and , with preempt disabled, treat them as they come in this list and when the counter is down to 0 on thislist, go back to the main schedule list ?


Two situations :

  • system underloaded, maybe the spinlock is faster (depends on the lock concurrency at this moment);

  • system heavily loaded, maybe this strategy is faster (no more wait).

I may miss something very smart here, and I would like to understand it, please.

Thank you


Solution

  • Spinlocks are primarily for use in (or to interoperate with) contexts that cannot block / reschedule. They should only be used where the likelihood of actually waiting for them is relatively low and the lock will not be held long. For example, assume an interrupt handler (and/or other contexts as well) has created a data structure and needs to link it into a doubly-linked list. That will only take nanoseconds to complete and the likelihood of colliding with another process is low, yet it must have an atomic effect: no other cpu/thread should see the list in an intermediate (partially linked) state.