首頁 > 軟體

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

2022-12-31 14:01:11

背景

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內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com