您的位置 首页 golang

golang chan 探究

前言

之前在看 golang 多线程通信的时候, 看到了go 的管道. 当时就觉得这玩意很神奇, 因为之前接触过的不管是 php , java , Python , js , c 等等, 都没有这玩意, 第一次见面, 难免勾起我的好奇心. 所以就想着看一看它具体是什么东西. 很明显, 管道是 go 实现在语言层面的功能, 所以我以为需要去翻他的源码了. 虽然最终没有翻到 C 的层次, 不过还是受益匪浅.

见真身

结构体

要想知道他是什么东西, 没什么比直接看他的定义更加直接的了. 但是其定义在哪里么? 去哪里找呢? 还记得我们是如何创建 chan 的么? make 方法. 但是当我找过去的时候, 发现 make 方法只是一个函数的声明.

golang chan 探究

这, 还是没有函数的具体实现啊. 汇编看一下. 编写以下内容:

 package main

func main() {
_ = make( ch an int)
}
  

执行命令:

go tool compile -N -l -S main.go

虽然汇编咱看不懂, 但是其中有一行还是引起了我的注意.

golang chan 探究

make 调用了 runtime .makechan . 漂亮, 就找他.

golang chan 探究

找到他了, 是 hchan 指针对象. 整理了一下对象的字段(不过人家自己也有注释的):

 // 其内部维护了一个循环队列(数组), 用于管理发送与接收的缓存数据. 
type hchan struct {
  // 队列中元素个数
qcount   uint
  // 队列的大小(数组长度)
dataqsiz uint
  // 指向底层的缓存队列, 是一个可以指向任意类型的指针. 
buf      unsafe.Pointer
  // 管道每个元素的大小
elemsize uint16
  // 是否被关闭了
closed   uint32
  // 管道的元素类型
elemtype *_type
  // 当前可以发送的元素 索引 (队尾)
sendx    uint  
  // 当前可以接收的元素索引(队首)
recvx    uint  
  // 当前等待接收数据的 goroutine 队列
recvq    waitq
  // 当前等待发送数据的 goroutine 队列
sendq    waitq 
// 锁, 用来保证管道的每个操作都是原子性的. 
 lock  mutex
}
  

可以看的出来, 管道简单说就是一个队列加一把锁.

发送数据

依旧使用刚才的方法分析, 发送数据时调用了 runtime.chansend1 函数. 其实现简单易懂:

golang chan 探究

然后查看真正实现, 函数步骤如下(个人理解, 有一些 test 使用的代码被我删掉了. ):

 func chansend(c *hchan, ep unsafe.Pointer,  block  bool, callerpc uintptr) bool {
  // 异常处理, 若管道指针为空
if c == nil {
if !block {
return false
}
gopark(nil, nil, waitReasonChanSendNilChan, traceEvGoStop, 2)
throw("unreachable")
}
// 常量判断, 恒为 false, 应该是开发时调试用的. 
if debugChan {
print("chansend: chan=", c, "n")
}
// 常量, 恒为 false, 没看懂这个判断
if raceenabled {
racereadpc(c.raceaddr(), callerpc, funcPC(chansend))
}
  // 若当前操作不阻塞, 且管道还没有关闭时判断
  // 当前队列容量为0且没有等待接收数据的 或 当前队列容量不为0且队列已满
  // 那么问题来了, 什么时候不加锁呢? select 的时候. 可以在不阻塞的时候快速返回
if !block && c.closed == 0 && ((c.dataqsiz == 0 && c.recvq.first == nil) ||
(c.dataqsiz > 0 && c.qcount == c.dataqsiz)) {
return false
}
// 上锁, 保证操作的原子性
lock(&c.lock)
// 若管道已经关闭, 报错
if c.closed != 0 {
unlock(&c.lock)
panic(plainError("send on closed  channel "))
}
// 从接受者队列获取一个接受者, 若存在, 数据直接发送, 不走缓存, 提高效率
if sg := c.recvq.dequeue(); sg != nil {
send(c, sg, ep, func() { unlock(&c.lock) }, 3)
return true
}
// 若缓存为满, 则将数据放到缓存中排队
if c.qcount < c.dataqsiz {
    // 取出对尾的地址
qp := chanbuf(c, c.sendx)
    // 将ep 的内容拷贝到 ap 地址
typedmemmove(c.elemtype, qp, ep)
    // 更新队尾索引
c.sendx++
if c.sendx == c.dataqsiz {
c.sendx = 0
}
c.qcount++
unlock(&c.lock)
return true
}
// 若当前不阻塞, 直接返回
if !block {
unlock(&c.lock)
return false
}
// 当走到这里, 说明数据没有成功发送, 且需要阻塞等待. 
  // 以下代码没看懂, 不过可以肯定的是, 其操作为阻塞当前协程, 等待发送数据
 gp  := getg()
mysg := acquireSudog()
mysg.releasetime = 0
if t0 != 0 {
mysg.releasetime = -1
}
mysg.elem = ep
mysg.waitlink = nil
mysg.g = gp
mysg.isSelect = false
mysg.c = c
gp.waiting = mysg
gp.param = nil
c.sendq.enqueue(mysg)
gopark(chanparkcommit, unsafe.Pointer(&c.lock), waitReasonChanSend, traceEvGoBlockSend, 2)
KeepAlive(ep)
if mysg != gp.waiting {
throw("G waiting list is corrupted")
}
gp.waiting = nil
gp.activeStackChans = false
if gp.param == nil {
if c.closed == 0 {
throw("chansend: spurious wakeup")
}
panic(plainError("send on closed channel"))
}
gp.param = nil
if mysg.releasetime > 0 {
blockevent(mysg.releasetime-t0, 2)
}
mysg.c = nil
releaseSudog(mysg)
return true
}
  

虽然最终阻塞的地方没看太明白, 不过发送数据的大体流程很清楚:

  1. 若无需阻塞且不能发送数据, 返回失败
  2. 若存在接收者, 直接发送数据
  3. 若存在缓存, 将数据放到缓存中
  4. 若无需阻塞, 返回失败
  5. 阻塞等待发送数据

其中不加锁的操作, 在看到 selectnbsend 函数的注释时如下:

 // compiler implements
//
//select {
//case c <- v:
//... foo
//default:
//... bar
//}
//
// as
//
//if selectnbsend(c, v) {
//... foo
//} else {
//... bar
//}
//
func selectnbsend(c *hchan, elem unsafe.Pointer) (selected bool) {
return chansend(c, elem, false, getcallerpc())
}
  

看这意思, select 关键字有点类似于语法糖, 其内部会转换成调用 selectnbsend 函数的简单 if 判断.

接收数据

至于接收数据的方法, 其内部实现与发送大同小异. runtime.chanrecv 方法.

源码简单看了一下, 虽理解不深, 但对 channel 也有了大体的认识.

上手

简单对 channel 的使用总结一下.

定义

 // 创建普通的管道类型, 非缓冲
a := make(chan int)
// 创建缓冲区大小为10的管道
b := make(chan int, 10)
// 创建只用来发送的管道
c := make(chan<- int)
// 创建只用来接收的管道
d := make(<-chan int)
// eg: 只用来接收的管道, 每秒一个
e := time.After(time.Second)
  

发送与接收

 // 接收数据
a := <- ch
b, ok := <- ch
// 发送数据
ch <- 2
  

最后, 看了一圈, 感觉 channel 并不是很复杂, 就是一个队列, 一端接受, 一端发送. 不过其对多协程处理做了很多优化. 与协程配合, 灵活使用的话, 应该会有不错的效果.

文章来源:智云一二三科技

文章标题:golang chan 探究

文章地址:https://www.zhihuclub.com/98273.shtml

关于作者: 智云科技

热门文章

评论已关闭

39条评论

  1. Simultaneous use with alpha-blockers Currents often meander closer to the shore; when this happens, the sea-beans may be more likely to become stranded on the beach

  2. Inform patients of contraindication of ADCIRCA with any use of organic nitrates or GC stimulators Long-term use of high levels of corticosteroid hormones can cause other symptoms such as thinning skin, changes in body fat distribution, blood sugar problems, bone loss, and increased acne or facial hair

  3. Sections of the E muscle, F liver, G heart, H stomach, I duodenum, J jejunum, K ileum and the L colon from 18 week old TetO Cre Jaw J mTmG mice without doxycycline Danger in a life

  4. From 1999 to May 2005, more than 1, 000 patients with CML in any stage of the disease were treated with imatinib at M

  5. Time points for blood collection were 1, 2, 4, 6, 8, 12, 24, 36, 48, and 72 h It was very dark, Grigoryev said

  6. To further investigate the impact of IL 6 on glucose metabolism, we used U 13 C glucose tracing and measured labeled metabolites after 0, 1, 3 and 24 hours 39, focusing on the fractional enrichment of metabolites directly related to glucose metabolism Fig

  7. The silencing of LncRNA H19 decreases chemoresistance of human glioma cells to temozolomide by suppressing epithelial mesenchymal transition via the Wnt beta Catenin pathway

  8. 14 We herein perform a similar analysis for postmenopausal women 60 years or older who receive partial mastectomy with low risk DCIS

  9. The nearby lymph nodes will also need to be checked, either with a sentinel lymph node biopsy SLNB or an axillary lymph node dissection ALND Hundreds donate blood at Fenway Park in 9 11 tribute

  10. Alternative therapy for elderly patients with breast cancer Put simply, they see payday lenders as highly destructive to their communities

  11. According to survey results, 20 of patients are hospitalised average length of stay, 7 days, and 70 of patients receive care in a hospice 30, day visits; 40, average of five overnight stays Table 4 The Codeine Tylenol and FOUR Advil did absolutely nothing when my cramps were at their worst about 4 hours after inserting Misoprostol

  12. It is more cost effective, less invasive and therefore less traumatic to the patient, requires only a local anaesthetic, and can be performed routinely in the outpatient department as part of the triple assessment protocol 5, 6 29 There is a significant up regulation of monocytic TLR 2 and TLR 4 expression in septic patients when compared with healthy individuals

  13. But he also let me know that this was an extremely rare complication of following a ketogenic diet, and he had only seen it one other time in his career Focal adhesion kinase deletion reduced proliferative capacity of p53 null and p53 R270H mammary epithelial cells but did not lead to increased apoptosis in vivo

  14. Prostate cancer is largely a disease of men over 40, so it s around this age doctors recommend the first prostate screening

  15. In addition, some background articles on reproductive complications of endocrinopathies were included

  16. The imbalance may result from an increase in serum estrogen through such mechanisms as increased hormone production from the testes, adrenal gland or various neoplasms; increased displacement of estrogen relative to androgen from the blood borne sex hormone binding globulin SHBG; decreased estrogen metabolism; or the administration of exogenous estrogen or estrogen like compounds

  17. 4 years, when the first interim analysis was conducted, the independent data and safety monitoring committee recommended termination of the trial and prompt communication of the results, because women on letrozole had a significantly superior disease free survival 93 v 87; P

  18. To compare variance of the three composite measures within and across the first and last collection periods, we used Levene s test of equality of variances

  19. While a few previous reports have described post treatment effects on MMPs, this study is the first to our knowledge to extensively characterize the effect of ATT on MMP levels in TB disease

  20. Ovarian reserve can be tested by a physician with a blood test called the Anti- Mullerian Hormone

  21. Results from clinical trials investigating the antitumor effects of antiresorptive therapies have been conflicting but generally support a small absolute improvement in OS, with the largest treatment impact on reductions in the recurrence of bone metastasis Efficacy of ultraviolet radiation as an alternative technology to inactivate microorganisms in grape juices and wines

  22. In this study, she and her colleagues showed that tamoxifen increased breast specific Lactobacillus in preclinical models Guvakova MA, Surmacz E

  23. And in his dantian, an extremely small pellet of dan essence gathered from the cold energy gathered out

  24. A single chain and fast responding light inducible Cre recombinase as a novel optogenetic switch

网站地图