http://blog.csdn.net/scutanjun/article/details/6240200
另外 ulk3中讲
1.6.5.4. Spin locks
In multiprocessor systems, semaphores are not always the best solution to the synchronization problems. Some kernel data structures should be protected from being concurrently accessed by kernel control paths that run on different CPUs. In this case, if the time required to update the data structure is short, a semaphore could be very inefficient. To check a semaphore, the kernel must insert a process in the semaphore list and then suspend it. Because both operations are relatively expensive, in the time it takes to complete them, the other kernel control path could have already released the semaphore.
In these cases, multiprocessor operating systems use spin locks . A spin lock is very similar to a semaphore, but it has no process list; when a process finds the lock closed by another process, it "spins" around repeatedly, executing a tight instruction loop until the lock becomes open.
Of course, spin locks are useless in a uniprocessor environment. When a kernel control path tries to access a locked data structure, it starts an endless loop. Therefore, the kernel control path that is updating the protected data structure would not have a chance to continue the execution and release the spin lock. The final result would be that the system hangs.
当然,自旋锁在单处理器环境下无效。当一个内核控制路径试图访问一个已被加锁了的临界数据(即试图获取同一个锁)时,它开始原地打转。因此原来那个正在更新临界数据的内核控制路径将没有机会继续执行去释放锁。最终结果是挂机。
所以对于
单处理器支持内核抢占的系统,在某个内核执行路径获得锁后要关闭内核抢占,在释放锁后再打开内核抢占。以防止死锁。
单处理器不支持内核抢占系统,没必要使用自旋锁。自旋锁可以退化为空操作。
smp系统类似于单处理器支持内核抢占的系统,在自旋锁获得期间也要关闭内核抢占。