Mutexs, Spinlocks and all that jazz
Posted: Wed Aug 23, 2006 4:15 pm
Hi guys, it's time once again for my weekly post ;D I just need to clarify how to go about protecting sections of non-reentrant code and code that simply must not be interrupted on different types of systems.
So first, we have single CPU systems. For code that can't be interrupted I imagine the obvious solution is to disable interrupts while it's executing. For non-reentrant code I guess it would make most sense to have a semaphore/mutex of some type with the thread giving up its timeslice (or equivalent) while it waits for access to it.
Then we have multi-CPU systems. The solutions should be the same as the above shouldn't they? Code that can't be interrupted can disable the interrupts that are normally routed to that particular CPU (or reroute them to the other CPU temporarily). Mutexs/semaphores and yielding should still have the desired result as well should they not? Even if the code is entered on different CPUs they should be using the same lock and thus they should all still wait and allow other threads to execute.
While I think about it, is there any specific reason to choose spinlocks instead of or in addition to mutexs/semaphores? I appreciate that they cause less reliance on the scheduler, but it seems to be less efficient to sit there spinning than to allow another thread to execute.
Well, there's my thoughts. Any obvious problems here or is it all good?
So first, we have single CPU systems. For code that can't be interrupted I imagine the obvious solution is to disable interrupts while it's executing. For non-reentrant code I guess it would make most sense to have a semaphore/mutex of some type with the thread giving up its timeslice (or equivalent) while it waits for access to it.
Then we have multi-CPU systems. The solutions should be the same as the above shouldn't they? Code that can't be interrupted can disable the interrupts that are normally routed to that particular CPU (or reroute them to the other CPU temporarily). Mutexs/semaphores and yielding should still have the desired result as well should they not? Even if the code is entered on different CPUs they should be using the same lock and thus they should all still wait and allow other threads to execute.
While I think about it, is there any specific reason to choose spinlocks instead of or in addition to mutexs/semaphores? I appreciate that they cause less reliance on the scheduler, but it seems to be less efficient to sit there spinning than to allow another thread to execute.
Well, there's my thoughts. Any obvious problems here or is it all good?