Go的channel是Go的一大特性,相对于其他编程而言,Go的channel提供了进程内消息通信,数据同步的重要功能,因为是内置数据结构,因此使用起来非常方便,因此,针对这个常用的数据结构,我们需要探究下其实现原理以及设计思想。
https://github.com/golang/go/blob/go1.12.7/src/runtime/chan.go
Go管理channel结构体如下,我们使用make创建channel时,就是初始化hchan结构体,hchan的定义如下:
type hchan struct {qcount uint // 当前环形队列内的数据个数dataqsiz uint // 环形队列总长度buf unsafe.Pointer // 数据缓冲区指针,指向一个dataqsiz大小的数组,环形队列由该数组实现elemsize uint16 // 单个数据的大小closed uint32 // channel是否关闭elemtype *_type // element type // 环形队列内的数据类型sendx uint // send index // 队列下标,元素写入队列时的队列的位置,即队列尾recvx uint // receive index // 队列下标,下一个被读取的元素在队列的位置,即队列头recvq waitq // 等待读消息的goroutine队列,环形队列空时,recvq会堆积sendq waitq // 等待写消息的goroutine队列,环形队列满时,sendq会堆积lock mutex // 互斥锁,保证channel并发读写安全}type waitq struct {first *sudog // 队列头指针last *sudog // 队列尾指针}
由channel的结构体可以看出,组合成channel的几大部分是:
- 环形队列,用于缓存数据
- groutine等待队列
- 互斥锁
channel的创建
我们使用
c := make(chan int, 6)
创建了一个channel,其缓冲区大小为6,元素类型为int。我们通过源码makeChan可以看出,我们调用make创建channel时返回的返回值的类型值hchan指针。
func makechan(t *chantype, size int) *hchan {elem := t.elem// 单个元素size过大if elem.size >= 1<<16 {throw("makechan: invalid channel element type")}// 字节对齐if hchanSize%maxAlign != 0 || elem.align > maxAlign {throw("makechan: bad alignment")}// 计算需要分配的内存大小,如果申请的内存过大,就panicmem, overflow := math.MulUintptr(elem.size, uintptr(size))if overflow || mem > maxAlloc-hchanSize || size < 0 {panic(plainError("makechan: size out of range"))}var c *hchanswitch {case mem == 0:// 申请的是0缓冲队列c = (*hchan)(mallocgc(hchanSize, nil, true))// Race detector uses this location for synchronization.c.buf = c.raceaddr()case elem.kind&kindNoPointers != 0:// 元素不包含指针// Allocate hchan and buf in one call.c = (*hchan)(mallocgc(hchanSize+mem, nil, true))c.buf = add(unsafe.Pointer(c), hchanSize)default:// 元素包含指针c = new(hchan)c.buf = mallocgc(mem, elem, true)}c.elemsize = uint16(elem.size)c.elemtype = elemc.dataqsiz = uint(size)...return c}
根据makechan的源码,我们了解到创建一个channel的本质是创建一个hchan结构体,我们通过makechan拿到的就是这个hchan结构体的指针。makechan具体做了以下几个事情:
- 前置检查,检查元素大小,以及申请分配的内存空间是否合理。
- 根据channel类型触发相应的内存分配,无缓冲长度的channel不分配内存给环形缓冲区;带缓冲区长度的channel调用不同的方法创建缓冲区,分配相应内存。
- hchan结构体赋值,返回hchan指针。
channel的数据发送
当执行c <- 1这样的代码时,即向channel发送数据,实际调用的函数是chansend。这里的chansend做了以下几件事情:
- 先加锁,保证并发读写安全
- 检查channel是否关闭,如果向已关闭的channel发送数据,触发panic
- 检查recvq是否有等待数据的goroutine,如果有,则取出recvq头部的goroutine,调用send,把发送方的数据直接拷贝给接收方,而无需拷贝到环形缓冲区。
- 如果环形缓冲队列未满,则把要发送的数据拷贝到环形缓冲区,更新环形队列的sendx值。
- 环形队列已满时,如果是非阻塞模式,则立即返回;如果是阻塞模式,那就把这个goroutine先挂起放到sendq等待数据。
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")}if debugChan {print("chansend: chan=", c, "\n")}if raceenabled {racereadpc(c.raceaddr(), callerpc, funcPC(chansend))}// ready for sending and then observe that it is not closed, that implies that the// channel wasn't closed during the first observation.// 这里的判断没有加锁,而是使用了if !block && c.closed == 0 && ((c.dataqsiz == 0 && c.recvq.first == nil) ||(c.dataqsiz > 0 && c.qcount == c.dataqsiz)) {return false}var t0 int64if blockprofilerate > 0 {t0 = cputicks()}// chansend操作全程加锁lock(&c.lock)// channel已关闭,继续往里send会panicif c.closed != 0 {unlock(&c.lock)panic(plainError("send on closed channel"))}// 检查recvq是否有等待数据的goroutine,如果有,取出recvq头部的goroutine,调用send,把发送方的数据直接拷贝给接收方,而无需拷贝到环形缓冲区if sg := c.recvq.dequeue(); sg != nil {// Found a waiting receiver. We pass the value we want to send// directly to the receiver, bypassing the channel buffer (if any).send(c, sg, ep, func() { unlock(&c.lock) }, 3)return true}// 环形队列未满,则将要发送的数据放入环形队列,更新sendx值。if c.qcount < c.dataqsiz {// Space is available in the channel buffer. Enqueue the element to send.qp := chanbuf(c, c.sendx)if raceenabled {raceacquire(qp)racerelease(qp)}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}// 如果是阻塞模式,因为队列已满,但是此时又需要发送数据到该队列,那就把这个goroutine先挂起放到sendqgp := getg()mysg := acquireSudog()mysg.releasetime = 0if t0 != 0 {mysg.releasetime = -1}// No stack splits between assigning elem and enqueuing mysg// on gp.waiting where copystack can find it.mysg.elem = epmysg.waitlink = nilmysg.g = gpmysg.isSelect = falsemysg.c = cgp.waiting = mysggp.param = nilc.sendq.enqueue(mysg) // 将当前要发送数据的goroutine挂起放到sendqgoparkunlock(&c.lock, waitReasonChanSend, traceEvGoBlockSend, 3)KeepAlive(ep)// someone woke us up.if mysg != gp.waiting {throw("G waiting list is corrupted")}gp.waiting = nilif gp.param == nil {if c.closed == 0 {throw("chansend: spurious wakeup")}panic(plainError("send on closed channel"))}gp.param = nilif mysg.releasetime > 0 {blockevent(mysg.releasetime-t0, 2)}mysg.c = nilreleaseSudog(mysg)return true}
channel的数据接收
当执行a <- c这样的代码时,即从channel读取数据时,实际调用的函数是chanrecv。这里的chanrecv做了以下几件事情:
- 首先加锁
- 如果环形缓冲区已满,且sendq有goroutine在的等待发送数据,直接调用recv去取sendq头部goroutine,将其要发送的数据拷贝给自己,不走环形缓冲区;
- 环形缓冲区有数据但队列未满,就消费环形队列里的头部元素;
- 环形缓冲区没有数据,非阻塞模式下就直接返回;阻塞模式下,此时需要把goroutine放入recvq等待数据。
func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool) {// raceenabled: don't need to check ep, as it is always on the stack// or is new memory allocated by reflect.if debugChan {print("chanrecv: chan=", c, "\n")}if c == nil {if !block {return}gopark(nil, nil, waitReasonChanReceiveNilChan, traceEvGoStop, 2)throw("unreachable")}// The order of operations is important here: reversing the operations can lead to// incorrect behavior when racing with a close.if !block && (c.dataqsiz == 0 && c.sendq.first == nil ||c.dataqsiz > 0 && atomic.Loaduint(&c.qcount) == 0) &&atomic.Load(&c.closed) == 0 {return}var t0 int64if blockprofilerate > 0 {t0 = cputicks()}lock(&c.lock)if c.closed != 0 && c.qcount == 0 {if raceenabled {raceacquire(c.raceaddr())}unlock(&c.lock)if ep != nil {typedmemclr(c.elemtype, ep)}return true, false}// 值得注意,如果channel已经关闭了,我们还是可以走下面的逻辑,即数据读取// 此时环形缓冲区已满,sendq有goroutine在的等待发送数据,直接调用recv去取sendq头部goroutine,将其要发送的数据拷贝给自己,不走环形缓冲区if sg := c.sendq.dequeue(); sg != nil {recv(c, sg, ep, func() { unlock(&c.lock) }, 3)return true, true}// 环形缓冲区有数据但队列未满,就消费环形队列里的头部元素if c.qcount > 0 {// Receive directly from queueqp := chanbuf(c, c.recvx)if raceenabled {raceacquire(qp)racerelease(qp)}if ep != nil {typedmemmove(c.elemtype, ep, qp) //数据拷贝,sender->reciever}typedmemclr(c.elemtype, qp) // 清理sender的发送数据内存c.recvx++if c.recvx == c.dataqsiz {c.recvx = 0}c.qcount--unlock(&c.lock)return true, true}if !block {unlock(&c.lock)return false, false}// 环形缓冲区没有数据,此时需要把goroutine放入recvq等待数据// no sender available: block on this channel.gp := getg()mysg := acquireSudog()mysg.releasetime = 0if t0 != 0 {mysg.releasetime = -1}// No stack splits between assigning elem and enqueuing mysg// on gp.waiting where copystack can find it.mysg.elem = epmysg.waitlink = nilgp.waiting = mysgmysg.g = gpmysg.isSelect = falsemysg.c = cgp.param = nilc.recvq.enqueue(mysg)goparkunlock(&c.lock, waitReasonChanReceive, traceEvGoBlockRecv, 3)// someone woke us upif mysg != gp.waiting {throw("G waiting list is corrupted")}gp.waiting = nilif mysg.releasetime > 0 {blockevent(mysg.releasetime-t0, 2)}closed := gp.param == nilgp.param = nilmysg.c = nilreleaseSudog(mysg)return true, !closed}
channel的关闭
close(c)用于关闭channel。channel的关闭实际调用的是closechan,注意以下几点:
- 关闭已关闭的channel,触发panic
- channel关闭时会通知该recvq队列中的所有Reader当前channel已关闭,这些recvq中的g会被释放。
- channel关闭时会通知该sendq队列中的所有writer当前channel已关闭,这会触发panic。这些sendq中的g会被释放。
- channel关闭操作也是全程加锁。
func closechan(c *hchan) {if c == nil {panic(plainError("close of nil channel"))}lock(&c.lock)// 关闭已关闭的channel,触发panicif c.closed != 0 {unlock(&c.lock)panic(plainError("close of closed channel"))}if raceenabled {callerpc := getcallerpc()racewritepc(c.raceaddr(), callerpc, funcPC(closechan))racerelease(c.raceaddr())}c.closed = 1var glist gList// 通知所有该channel的Reader这个channel关闭了,释放recvq中的所有g// release all readersfor {sg := c.recvq.dequeue()if sg == nil {break}if sg.elem != nil {typedmemclr(c.elemtype, sg.elem)sg.elem = nil}if sg.releasetime != 0 {sg.releasetime = cputicks()}gp := sg.ggp.param = nilif raceenabled {raceacquireg(gp, c.raceaddr())}glist.push(gp)}// release all writers (they will panic)// 释放sendq中的所有gfor {sg := c.sendq.dequeue()if sg == nil {break}sg.elem = nilif sg.releasetime != 0 {sg.releasetime = cputicks()}gp := sg.ggp.param = nilif raceenabled {raceacquireg(gp, c.raceaddr())}glist.push(gp)}unlock(&c.lock)// 释放这些挂起的gfor !glist.empty() {gp := glist.pop()gp.schedlink = 0goready(gp, 3)}}
如果channel已经关闭,但此时还有goroutine去接收该channel数据,如果channel的环形缓冲队列里已无数据,则x,ok := <- ch中,x为nil,ok为false;如果channel的环形缓冲队列还有数据,此时去接收数据时可以收到数据的,x为接收到的数据,ok为true。只有当channel关闭且无数据时,x才为nil,ok为false。
