golang sync.Mutex 源码阅读

1,229 阅读4分钟

闲来无事,阅读一下golang互斥锁实现

全称

mutual exclusion lock - 彼此互斥的锁

包简介

// Package sync provides basic synchronization primitives such as mutual
// exclusion locks. Other than the Once and WaitGroup types, most are intended
// for use by low-level library routines. Higher-level synchronization is
// better done via channels and communication.

  • mutex是golang比较基本的同步原语,原语=>原始语言
  • mutex相比channel是轻量级的lib

Mutex Struct

type Mutex struct {   
    state int32     //锁标识位, 0bit-锁标记 | 1bit-唤醒标记 | 2bit-饥饿标记 | 其他-waiter
    sema  uint32    //锁信号量
}

Const

const (   
    mutexLocked = 1 << iota // mutex is locked   
    mutexWoken   
    mutexStarving   
    mutexWaiterShift = iota 
    starvationThresholdNs = 1e6  
)

对应struct中的state标记位,starvationThresholdNs是自旋时间阈值,自旋时间超过这个值,会进入饥饿状态。

Mutex工作模式

// Mutex can be in 2 modes of operations: normal and starvation.
// In normal mode waiters are queued in FIFO order, but a woken up waiter
// does not own the mutex and competes with new arriving goroutines over
// the ownership. New arriving goroutines have an advantage -- they are
// already running on CPU and there can be lots of them, so a woken up
// waiter has good chances of losing. In such case it is queued at front
// of the wait queue. If a waiter fails to acquire the mutex for more than 1ms,
// it switches mutex to the starvation mode.
//
// In starvation mode ownership of the mutex is directly handed off from
// the unlocking goroutine to the waiter at the front of the queue.
// New arriving goroutines don't try to acquire the mutex even if it appears
// to be unlocked, and don't try to spin. Instead they queue themselves at
// the tail of the wait queue.
//
// If a waiter receives ownership of the mutex and sees that either
// (1) it is the last waiter in the queue, or (2) it waited for less than 1 ms,
// it switches mutex back to normal operation mode.
//
// Normal mode has considerably better performance as a goroutine can acquire
// a mutex several times in a row even if there are blocked waiters.
// Starvation mode is important to prevent pathological cases of tail latency.
  • 正常模式

mutex如果未被上锁,或者compete goroutine 通过自旋能够获取到锁,mutex会处于该模式下

  • 饥饿模式

自旋时间超过阈值,mutex会进入饥饿模式,之后的compete goroutine 不会自旋了,直接进入sema信号量的队列尾部

  • lifo or fifo

饥饿状态下,compete goroutine 会以先进先出的方式进入等待队列,参与过自旋的会以后进先出的方式进入等待队列

  • 自旋+lifo && starving + 不自旋 实质是性能和公平的兼顾综合考量,这块设计的很细腻!
  • woken up标识位

主要应对自旋和sema信号量释放并发的场景,当有自旋goroutine时,woken up =1,此时sema释放时,发现woken up =1时,就不会从阻塞队列释放goroutine了,让处于自旋状态的goroutines竞争


上锁代码

func (m *Mutex) Lock() {   
    // 使用cpu的原子操作,给mutex上锁,上锁成功直接返回   
    if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) {
      if race.Enabled {
         race.Acquire(unsafe.Pointer(m))
      }
      return
   }
   // Slow path (outlined so that the fast path can be inlined)
   m.lockSlow()
}

func (m *Mutex) lockSlow() {
    var waitStartTime int64
    starving := false
    awoke := false
    iter := 0
    old := m.state
    for {
        //locked状态 && 非饥饿状态 && 可以自旋 才能自旋
        //runtime_canSpin和诸多因素有关,cpu核数,当前processor不能有等待执行的goroutine等
        if old&(mutexLocked|mutexStarving) == mutexLocked && runtime_canSpin(iter) {
            //设置woken up标识位
            //设置条件 非woken up状态 && 有阻塞goroutine
            if !awoke && old&mutexWoken == 0 && old>>mutexWaiterShift != 0 &&
                atomic.CompareAndSwapInt32(&m.state, old, old|mutexWoken) {
                awoke = true
             }
             //自旋
             runtime_doSpin()
             iter++
             old = m.state
             continue
            }
            //自旋结束or没自旋
            new := old
            //自旋竞争失败要上锁
            if old&mutexStarving == 0 {
             new |= mutexLocked
            }
            //等待计数+1
            if old&(mutexLocked|mutexStarving) != 0 {
             new += 1 << mutexWaiterShift
            }
            //切换饥饿模式
            if starving && old&mutexLocked != 0 {
             new |= mutexStarving
            }
            // 竞争失败后,woken up 标识位要清除
            if awoke {
                if new&mutexWoken == 0 {
                    throw("sync: inconsistent mutex state")
                }
                new &^= mutexWoken
            }
            //更新mutex state状态
            if atomic.CompareAndSwapInt32(&m.state, old, new) {
                if old&(mutexLocked|mutexStarving) == 0 {
                    break // locked the mutex with CAS
                }
                //自旋过的goroutine 使用lifo方式进入信号量等待队列
                queueLifo := waitStartTime != 0
                if waitStartTime == 0 {
                    waitStartTime = runtime_nanotime()
                }
                //调用信号量func
                runtime_SemacquireMutex(&m.sema, queueLifo, 1)
                starving = starving || runtime_nanotime()-waitStartTime > starvationThresholdNs
                old = m.state
                //不在饥饿的情况,需要清除后面的标识位
                if old&mutexStarving != 0 {
                    if old&(mutexLocked|mutexWoken) != 0 || old>>mutexWaiterShift == 0 {
                       throw("sync: inconsistent mutex state")
                    }
                    delta := int32(mutexLocked - 1<<mutexWaiterShift)
                    if !starving || old>>mutexWaiterShift == 1 {
                   // Exit starvation mode.
                   // Critical to do it here and consider wait time.
                   // Starvation mode is so inefficient, that two goroutines
                   // can go lock-step infinitely once they switch mutex
                   // to starvation mode.
                       delta -= mutexStarving 
                   }
                    atomic.AddInt32(&m.state, delta)
                    break
                }
                awoke = true
                iter = 0
              } else {
                 old = m.state
              }
           }
           if race.Enabled {
              race.Acquire(unsafe.Pointer(m))
           }
}

解锁代码

func (m *Mutex) Unlock() {
   if race.Enabled {
      _ = m.state
      race.Release(unsafe.Pointer(m))
   }
   new := atomic.AddInt32(&m.state, -mutexLocked)
   //发现之前有其他标识位的,需要走额外逻辑
   if new != 0 {
      m.unlockSlow(new)
   }}

func (m *Mutex) unlockSlow(new int32) {
   //重复unlock 直接panic
   if (new+mutexLocked)&mutexLocked == 0 {
      throw("sync: unlock of unlocked mutex")
   }
   if new&mutexStarving == 0 {
      old := new
      for {
         if old>>mutexWaiterShift == 0 || old&(mutexLocked|mutexWoken|mutexStarving) != 0 {
            return
         }
         // 设置唤醒标识位
         new = (old - 1<<mutexWaiterShift) | mutexWoken
         if atomic.CompareAndSwapInt32(&m.state, old, new) {
            //释放信号量,lifo方式释放goroutine
            runtime_Semrelease(&m.sema, false, 1)
            return
         }
         old = m.state
      }
   } else {
      //有饥饿,fifo方式释放信号量和goroutine
      runtime_Semrelease(&m.sema, true, 1)
   }}