这项技术就是所谓的自旋锁。
这样可以减少自旋锁的数目。
JD K 1.6中引入了自适应自旋锁。
首先通过一个简单的声明创建一个新的自旋锁。
First you create a new spinlock through a simple declaration.
这种自旋锁禁用了本地CPU上的软中断。
This version of the spinlock disables soft interrupts on the local CPU.
在中断服务函数中获得一个自旋锁完全合理;
Taking out a spinlock in an interrupt handler is a legitimate thing to do;
当锁被占有的时间很短时,自旋锁的效果非常好。
Spin locks work well in cases where lock durations are very short.
定义了自旋锁之后,就可以使用大量的锁定变量了。
Now that you have a spinlock defined, there are a number of locking variants that you can use.
然而,还有一些使用自旋锁时必须要注意的问题。
However, there are a few rules about how you must work with spinlocks.
确保这个拷贝是在一个具有自旋锁的临界区中发生的。
Make sure this copying happens inside the critical section by holding a spinlock.
内核的抢占机制被它自己操作自旋锁的代码所控制。
The kernel preemption case is handled by the spinlock code itself.
我们的目标程序使用自旋锁机制来实现线程间对共享内存的互斥访问。
The classic spin lock technology is introduced to implement the mutually exclusive access to Shared memory.
显然,如果您使用的是原版的读/写锁,那么按照标准自旋锁的用法使用这个自旋锁,而不区分读线程和写线程。
Obviously, if your use of the lock is reader/writer in nature, this spinlock should be used over the standard spinlock, which doesn't differentiate between readers and writers.
spin_unlock _ irqrestore函数释放自旋锁,并且(通过flags参数)恢复中断。
The spin_unlock_irqrestore function releases the spinlock and restores the interrupts (via the flags argument).
另一方面,如果锁被占有很长时间,那么自旋的线程只会消耗CPU而不做任何有用的工作,因此带来浪费。
On the other hand, if the lock is held for a longer time, spinning is wasteful as the spinning thread consumes CPU without doing anything useful.
如果在同一个锁对象上,自旋刚刚成功过,并且持有锁的线程正在运行中,那么自旋很有可能再次成功。
If spinning succeeded on that same lock object in the recent history and the thread holding the lock is running, then spinning is likely to succeed again.
自适应意味着自旋的时间不再固定了,而是取决于一个基于前一次在同一个锁上的自旋时间以及锁的拥有者的状态。
With adaptive spinning the duration of the spin is not fixed anymore, but determined by a policy based on previous spin attempts on the same lock and the state of the lock owner.
自旋机制描述了闩锁仍然在使用中,但是仅仅在互斥中获取一个闩锁。
The spinning mechanisms described under latches is still used, but only to get one of the latches in the mutex.
自旋机制描述了闩锁仍然在使用中,但是仅仅在互斥中获取一个闩锁。
The spinning mechanisms described under latches is still used, but only to get one of the latches in the mutex.
应用推荐