golang sync.Mutex source code reading

Nothing else, read the mutex achieve golang

Full name

mutual exclusion lock - mutually exclusive lock

Package Introduction

// 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 is golang more basic synchronization primitives, primitive => original language
  • mutex is lightweight compared to 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 tag bit corresponding to the state, starvationThresholdNs spin time threshold, the spin time exceeds this value, will enter starvation.

Mutex operating mode

// 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.复制代码
  • Normal mode

If the mutex is not locked, or compete goroutine can be obtained by the spin lock, will be in this mode mutex

  • Starvation mode

Spin time exceeds the threshold value, the mutex enters starvation mode, compete goroutine not after the spin, and directly into the tail of the queue semaphore sema

  • lifo or fifo

Starvation, compete goroutine will FIFO way into the waiting queue, the spin will be involved in first-out way into the waiting queue

  • Spin + lifo && starving + do not spin essence is both fair and comprehensive consideration of performance, very fine piece of design!
  • woken up flag

The main scene to deal with spin and sema semaphore release concurrent, when there is a spin goroutine, woken up = 1, this time when sema release, found woken up = 1, it will not release goroutine from blocking queue, so that in goroutines spin state of competition


Code lock

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))
           }
}复制代码

Unlock code

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)
   }}复制代码


Guess you like

Origin juejin.im/post/5e818910f265da47b554db6d