自旋锁

自旋锁是计算机科学用于多线程同步的一种锁,线程反复检查锁变量是否可用。由于线程在这一过程中保持执行,因此是一种忙等待。一旦获取了自旋锁,线程会一直保持该锁,直至显式释放自旋锁。

自旋锁避免了进程上下文的调度开销,因此对于线程只会阻塞很短时间的场合是有效的。因此操作系统的实现在很多地方往往用自旋锁。Windows操作系统提供的轻型读写锁(SRW Lock)内部就用了自旋锁。显然,单核CPU不适于使用自旋锁,这里的单核CPU指的是单核单线程的CPU,因为,在同一时间只有一个线程是处在运行状态,假设运行线程A发现无法获取锁,只能等待解锁,但因为A自身不挂起,所以那个持有锁的线程B没有办法进入运行状态,只能等到操作系统分给A的时间片用完,才能有机会被调度。这种情况下使用自旋锁的代价很高。

获取、释放自旋锁,实际上是读写自旋锁的存储内存或寄存器。因此这种读写操作必须是原子的。通常用test-and-set等原子操作来实现。

Windows内核API

  • KeInitializeSpinLock //初始化自旋锁
  • KeAcquireSpinLock //获取自旋锁
  • KeReleaseSpinLock //释放自旋锁

例子
汇编语言写的代码,运行于Intel 80386兼容处理器。

; Intel syntax

locked: ; The lock variable. 1 = locked, 0 = unlocked.
dd 0

spin_lock:
mov eax, 1 ; Set the EAX register to 1.

xchg eax, [locked] ; Atomically swap the EAX register with
; the lock variable.
; This will always store 1 to the lock, leaving
; the previous value in the EAX register.

test eax, eax ; Test EAX with itself. Among other things, this will
; set the processor's Zero Flag if EAX is 0.
; If EAX is 0, then the lock was unlocked and
; we just locked it.
; Otherwise, EAX is 1 and we didn't acquire the lock.

jnz spin_lock ; Jump back to the MOV instruction if the Zero Flag is
; not set; the lock was previously locked, and so
; we need to spin until it becomes unlocked.

ret ; The lock has been acquired, return to the calling
; function.

spin_unlock:
mov eax, 0 ; Set the EAX register to 0.

xchg eax, [locked] ; Atomically swap the EAX register with
; the lock variable.

ret ; The lock has been released.

参见
*忙碌等待
*死锁
*
*排号自旋锁

参考文献
外部链接
*[http://www.opengroup.org/onlinepubs/009695399/functions/pthread_spin_lock.html pthread_spin_lock documentation] from The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
*[https://github.com/concurrencykit/ck/blob/master/include/ck_spinlock.h Variety of spinlock Implementations] from Concurrency Kit
*Article "[https://web.archive.org/web/20041211235628/http://www.codeproject.com/threads/spinlocks.asp User-Level Spin Locks - Threads, Processes & IPC]" by Gert Boddaert
*Paper "[http://www.cs.washington.edu/homes/tom/pubs/spinlock.html The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors] " by Thomas E. Anderson
*Paper "[http://portal.acm.org/citation.cfm?id=103727.103729 Algorithms for Scalable Synchronization on Shared-Memory Multiprocessors]" by John M. Mellor-Crummey and Michael L. Scott. This paper received the [http://www.podc.org/dijkstra/2006.html 2006 Dijkstra Prize in Distributed Computing] .
*[http://msdn.microsoft.com/en-us/magazine/cc163726.aspx Spin-Wait Lock] by Jeffrey Richter
*[http://austria.sourceforge.net/dox/html/classSpinLock.html Austria C++ SpinLock Class Reference]
*[http://msdn2.microsoft.com/en-us/library/ms684122(VS.85).aspx Interlocked Variable Access(Windows)]
*[http://pages.cs.wisc.edu/~remzi/OSTEP/threads-locks.pdf Operating Systems: Three Easy Pieces (Chapter: Locks)]

评论 (0)

  • 还没有评论,来抢沙发吧。