詳解Golang中select的使用與源碼分析

背景

golang 中主推 channel 通信。單個 channel 的通信可以通過一個goroutinechannel 發數據,另外一個從channel取數據進行。這是阻塞的,因為要想順利執行完這個步驟,需要 channel 準備好才行,準備好的條件如下:

1.發送

  • 緩存有空間(如果是有緩存的 channel)
  • 有等待接收的 goroutine

2.接收

  • 緩存有數據(如果是有緩存的 channel)
  • 有等待發送的 goroutine

channel實際使用中還有如下兩個需求,這個時候就需要select瞭。

  • 同時監聽多個channel
  • 在沒有channel準備好的時候,也可以往下執行。

select 流程

1.空select。作用是阻塞當前goroutine。不要用for{}來阻塞goroutine,因為會占用cpu。而select{}不會,因為當前goroutine不會再被調度。

 if len(cases) == 0 {
         block()
 }

2.配置好poll的順序。由於是同時監聽多個channel的發送或者接收,所以需要按照一定的順序查看哪個channel準備好瞭。如果每次采用select中的順序查看channel是否準備好瞭,那麼隻要在前面的channel準備好的足夠快,那麼會造成後面的channel即使準備好瞭,也永遠不會被執行。打亂順序的邏輯如下,采用瞭洗牌算法\color{red}{洗牌算法}洗牌算法,註意此過程中會過濾掉channel為nil的case。\color{red}{註意此過程中會過濾掉 channel 為 nil 的 case。}註意此過程中會過濾掉channel為nil的case。

 // generate permuted order
 norder := 0
 for i := range scases {
         cas := &scases[i]

         // Omit cases without channels from the poll and lock orders.
         if cas.c == nil {
                 cas.elem = nil // allow GC
                 continue
         }

         j := fastrandn(uint32(norder + 1))
         pollorder[norder] = pollorder[j]
         pollorder[j] = uint16(i)
         norder++
 }

3.配置好lock的順序。由於可能會修改channel中的數據,所以在打算往channel中發送數據或者從channel接收數據的時候,需要鎖住 channel。而一個channel可能被多個select監聽,如果兩個select對兩個channel A和B,分別按照順序A, B和B,A上鎖,是可能會造成死鎖的,導致兩個select都執行不下去。

所以select中鎖住channel的順序至關重要,解決方案是按照channel的地址的順序鎖住channel。因為在兩個selectchannel有交集的時候,都是按照交集中channel的地址順序鎖channel

實際排序代碼如下,采用堆排序算法\color{red}{堆排序算法}堆排序算法按照channel的地址從小到大對channel進行排序。

 // sort the cases by Hchan address to get the locking order.
 // simple heap sort, to guarantee n log n time and constant stack footprint.
 for i := range lockorder {
         j := i
         // Start with the pollorder to permute cases on the same channel.
         c := scases[pollorder[i]].c
         for j > 0 && scases[lockorder[(j-1)/2]].c.sortkey() < c.sortkey() {
                 k := (j - 1) / 2
                 lockorder[j] = lockorder[k]
                 j = k
         }
         lockorder[j] = pollorder[i]
 }
 for i := len(lockorder) - 1; i >= 0; i-- {
         o := lockorder[i]
         c := scases[o].c
         lockorder[i] = lockorder[0]
         j := 0
         for {
                 k := j*2 + 1
                 if k >= i {
                         break
                 }
                 if k+1 < i && scases[lockorder[k]].c.sortkey() < scases[lockorder[k+1]].c.sortkey() {
                         k++
                 }
                 if c.sortkey() < scases[lockorder[k]].c.sortkey() {
                         lockorder[j] = lockorder[k]
                         j = k
                         continue
                 }
                 break
         }
         lockorder[j] = o
 }

4.鎖住select中的所有channel。要查看channel中的數據瞭。

 // lock all the channels involved in the select
 sellock(scases, lockorder)

5.第一輪查看是否已有準備好的channel。如果有直接發送數據到channel或者從channel接收數據。註意selectchannel切片中,前面部分是從channel接收數據的case,後半部分是往channel發送數據的case。

按照pollorder順序查看是否有channel準備好瞭。

 for _, casei := range pollorder {
         casi = int(casei)
         cas = &scases[casi]
         c = cas.c
         if casi >= nsends {
                 sg = c.sendq.dequeue()
                 if sg != nil {
                         goto recv
                 }
                 if c.qcount > 0 {
                         goto bufrecv
                 }
                 if c.closed != 0 {
                         goto rclose
                 }
         } else {
                 if raceenabled {
                         racereadpc(c.raceaddr(), casePC(casi), chansendpc)
                 }
                 if c.closed != 0 {
                         goto sclose
                 }
                 sg = c.recvq.dequeue()
                 if sg != nil {
                         goto send
                 }
                 if c.qcount < c.dataqsiz {
                         goto bufsend
                 }
         }
 }

6.直接執行default分支

 if !block {
         selunlock(scases, lockorder)
         casi = -1
         goto retc
 }

7.第二輪遍歷channel。創建sudog把當前goroutine放到每個channel的等待列表中去,等待channel準備好時被喚醒。

 // pass 2 - enqueue on all chans
 gp = getg()
 if gp.waiting != nil {
         throw("gp.waiting != nil")
 }
 nextp = &gp.waiting
 for _, casei := range lockorder {
         casi = int(casei)
         cas = &scases[casi]
         c = cas.c
         sg := acquireSudog()
         sg.g = gp
         sg.isSelect = true
         // No stack splits between assigning elem and enqueuing
         // sg on gp.waiting where copystack can find it.
         sg.elem = cas.elem
         sg.releasetime = 0
         if t0 != 0 {
                 sg.releasetime = -1
         }
         sg.c = c
         // Construct waiting list in lock order.
         *nextp = sg
         nextp = &sg.waitlink

         if casi < nsends {
                 c.sendq.enqueue(sg)
         } else {
                 c.recvq.enqueue(sg)
         }
 }

8.等待被喚醒。其中gopark的時候會釋放對所有channel占用的鎖。

 // wait for someone to wake us up
 gp.param = nil
 // Signal to anyone trying to shrink our stack that we're about
 // to park on a channel. The window between when this G's status
 // changes and when we set gp.activeStackChans is not safe for
 // stack shrinking.
 atomic.Store8(&gp.parkingOnChan, 1)
 gopark(selparkcommit, nil, waitReasonSelect, traceEvGoBlockSelect, 1)
 gp.activeStackChans = false

9.被喚醒

  • 鎖住所有channel
  • 清理當前goroutine的等待sudog
  • 找到是被哪個channel喚醒的,並清理每個channel上當前的goroutine對應的sudog
 sellock(scases, lockorder)

 gp.selectDone = 0
 sg = (*sudog)(gp.param)
 gp.param = nil

 // pass 3 - dequeue from unsuccessful chans
 // otherwise they stack up on quiet channels
 // record the successful case, if any.
 // We singly-linked up the SudoGs in lock order.
 casi = -1
 cas = nil
 caseSuccess = false
 sglist = gp.waiting
 // Clear all elem before unlinking from gp.waiting.
 for sg1 := gp.waiting; sg1 != nil; sg1 = sg1.waitlink {
         sg1.isSelect = false
         sg1.elem = nil
         sg1.c = nil
 }
 gp.waiting = nil

 for _, casei := range lockorder {
         k = &scases[casei]
         if sg == sglist {
                 // sg has already been dequeued by the G that woke us up.
                 casi = int(casei)
                 cas = k
                 caseSuccess = sglist.success
                 if sglist.releasetime > 0 {
                         caseReleaseTime = sglist.releasetime
                 }
         } else {
                 c = k.c
                 if int(casei) < nsends {
                         c.sendq.dequeueSudoG(sglist)
                 } else {
                         c.recvq.dequeueSudoG(sglist)
                 }
         }
         sgnext = sglist.waitlink
         sglist.waitlink = nil
         releaseSudog(sglist)
         sglist = sgnext
 }

到此這篇關於詳解Golang中select的使用與源碼分析的文章就介紹到這瞭,更多相關Golang select內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: