计算机科学

首页 > 计算机科学

自旋锁

2018-08-30 10:04:34     所属分类:编译原理

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

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

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

目录

  • 1 例子
  • 2 参见
  • 3 参考文献
  • 4 外部链接

例子

汇编语言写的代码,运行于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.

参见

  • Busy spin
  • Deadlock
  • Seqlock
  • Ticket lock

参考文献

  1. ^ Silberschatz, Abraham; Galvin, Peter B. Operating System Concepts Fourth. Addison-Wesley. 1994: 176–179. ISBN 0-201-59292-4. 

外部链接

  • pthread_spin_lock documentation from The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
  • Variety of spinlock Implementations from Concurrency Kit
  • Article "User-Level Spin Locks - Threads, Processes & IPC" by Gert Boddaert
  • Paper "The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors" by Thomas E. Anderson
  • Paper "Algorithms for Scalable Synchronization on Shared-Memory Multiprocessors" by John M. Mellor-Crummey and Michael L. Scott. This paper received the 2006 Dijkstra Prize in Distributed Computing.
  • Spin-Wait Lock by Jeffrey Richter
  • Austria C++ SpinLock Class Reference
  • Interlocked Variable Access(Windows)
  • Operating Systems: Three Easy Pieces (Chapter: Locks)

上一篇:动态重编译
下一篇:语法制导翻译
相关推荐