0
votes

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

1
Spin locks helps when there are multiple CPUs. How would the multiple CPUs fit in this "specific queue and no locks" solution ?srd
They would just atomically increase the counter with fences and swap one pointer. hmmmKroma
Not sure what you're asking. 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. Ex: assume an interrupt handler (and 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.Gil Hamilton
Got it. You made it clear to me.Kroma
@GilHamilton, why don't you make that an answer (which can be accepted)?Alex D

1 Answers

0
votes

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.