channel是在同一个进程内不同协程之间的通信方式, 最常见的goroutine就是协程。Channel代码相对简单,主要就是四个函数:初始化makechan,消息发送chansend,消息接收chanrecv,关闭通道closechan。先来看看channel的数据结构:
type hchan struct {
// 通道中实际的元素个数,len(ch)的返回值
qcount uint
// 通道的容量,cap(ch)的返回值,qcount <= dataqsiz
// ch := make(chan T, x)中的 x,dataqsiz为0表示非缓冲通道
dataqsiz uint
//存储通道元素的缓冲队列地址,使用环形数组实现
buf unsafe.Pointer
//通道内单个元素的大小,单位为字节
elemsize uint16
//通道是否关闭的标志位
closed uint32
//通道元素的类型,make(chan T, x)中的T,被go编译器抽象为_type结构体,记录这该类型的全部属性
elemtype *_type // element type
//待发送元素在缓冲队列中的索引
sendx uint // send index
//待接收元素在缓冲队列中的索引
recvx uint // receive index
//接收goroutine等待队列,当通道为空时,用来存放阻塞的接收goroutine
recvq waitq // list of recv waiters
//发送goroutine等待队列,当通道满时,用来存放阻塞发送goroutine
sendq waitq // list of send waiters
//发送和接收同时操作通道时使用的互斥锁
lock mutex
}
Channel的数据缓冲区是个环形数组,sendx和recvx控制着接收和发送位置,如果数组已满发送请求或者接收请求将挂在recvq或者sendq上。数据缓冲区的大小就是dataqsiz。整体结构如下图:
recvq 和 sendq 对应的结构 waitq 是一个链表,包含一个头结点和一个尾结点,队列中的每个成员是一个sudog结构体:
type waitq struct {
first *sudog
last *sudog
}
type sudog struct {
//g表示一个goroutine协程
g *g
isSelect bool
//sudog双向链表对应的指针
next *sudog
prev *sudog
//g代表的goroutine协程从channel接收消息的地址
elem unsafe.Pointer // data element (may point to stack)
acquiretime int64
releasetime int64
ticket uint32
parent *sudog // semaRoot binary tree
waitlink *sudog // g.waiting list or semaRoot
waittail *sudog // semaRoot
c *hchan // channel
}
func makechan(t *chantype, size int) *hchan {
elem := t.elem
//检查T类型大小是否超过限制,比如传入一个大于64k大数组,会报错
//ch := make(chan [8192]int64, 1) 64k = 65536 = 8(int64占8字节) * 8192
if elem.size >= 1<<16 {
throw("makechan: invalid channel element type")
}
//判断对齐限制
if hchanSize%maxAlign != 0 || elem.align > maxAlign {
throw("makechan: bad alignment")
}
//这里做了两个判断:
//判断缓冲通道的容量是否为负
//判断当缓冲通道满时,队列大小是否超出系统最大内存
if size < 0 || uintptr(size) > maxSliceCap(elem.size) || uintptr(size)*elem.size > maxAlloc-hchanSize {
panic(plainError("makechan: size out of range"))
}
var c *hchan
switch {
case size == 0 || elem.size == 0:
//当创建的是非缓冲通道
//或者缓冲通道的元素类型大小为0(如 struct{}{})
//只需要申请hchan的内存而不需要申请缓冲队列的内存
c = (*hchan)(mallocgc(hchanSize, nil, true))
//由于申请的内存只给hchan使用
//c.buf直接指向申请的hchan的内存地址
c.buf = unsafe.Pointer(c)
case elem.kind&kindNoPointers != 0:
//当创建的是缓冲通道,并且通道元素类型不是指针类型的
//需要申请hchan的内存和缓冲队列的内存
//计算公式为:hchan内存 + 缓冲队列元素个数 * 元素大小
c = (*hchan)(mallocgc(hchanSize+uintptr(size)*elem.size, nil, true))
//由于申请的内存是给hchan和缓冲队列一起用的
//指向内存缓冲中,hchan的位置
c.buf = add(unsafe.Pointer(c), hchanSize)
default:
//当创建的是缓冲通道,并且通道元素类型是指针类型的
//调用了两次mallocgc来申请内存,hchan和缓冲队列不共用内存(内存空间是不连续的)
c = new(hchan)
c.buf = mallocgc(uintptr(size)*elem.size, elem, true)
}
//记录单个元素的大小,元素类型及通道容量
c.elemsize = uint16(elem.size)
c.elemtype = elem
c.dataqsiz = uint(size)
return c
}
Mallocgc、new内存申请在前面文章中有做介绍。
func chansend1(c *hchan, elem unsafe.Pointer) {
chansend(c, elem, true, getcallerpc())
}
func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
if c == nil {
//参数block是用来指定通道是否阻塞的
if !block {
return false
}
// gopark函数将当前goroutine置于等待状态并通过unlockf唤醒
// 但是传入的unlockf为nil(第一个参数)
// 所以,当通道为nil时,向其发送数据,会永久阻塞
gopark(nil, nil, waitReasonChanSendNilChan, traceEvGoStop, 2)
throw("unreachable")
}
if debugChan {
print("chansend: chan=", c, "\n")
}
if raceenabled {
racereadpc(unsafe.Pointer(c), callerpc, funcPC(chansend))
}
if !block && c.closed == 0 && ((c.dataqsiz == 0 && c.recvq.first == nil) ||
(c.dataqsiz > 0 && c.qcount == c.dataqsiz)) {
return false
}
var t0 int64
if blockprofilerate > 0 {
t0 = cputicks()
}
//在数据发送到通道前,先获取互斥锁,保证线程安全
lock(&c.lock)
//向已经关闭的通道发送数据,会panic
if c.closed != 0 {
unlock(&c.lock)
panic(plainError("send on closed channel"))
}
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).
// 接收goroutine的等待队列中,有等待着的goroutine
// 说明通道为非缓冲通道或者缓冲通道的缓冲队列为空�
// 取出接收队列中排在最前边的goroutine
// 然后不经过通道的缓冲区,将发送的数据直接拷贝给这个goroutine
send(c, sg, ep, func() { unlock(&c.lock) }, 3)
return true
}
//接收goroutine队列为空,缓冲通道的元素个数小于通道容量
if c.qcount < c.dataqsiz {
// Space is available in the channel buffer. Enqueue the element to send.
// 获取指向缓冲通道中第i个槽的指针,后边将数据拷贝到此指针对应的空间内
qp := chanbuf(c, c.sendx)
if raceenabled {
raceacquire(qp)
racerelease(qp)
}
//将发送goroutine中需要发送的数据拷贝到缓冲通道中
typedmemmove(c.elemtype, qp, ep)
//发送index + 1
c.sendx++
//如果缓冲通道元素数量达到了通道容量,就将发送index改为0,构造环形数组
if c.sendx == c.dataqsiz {
c.sendx = 0
}
c.qcount++
unlock(&c.lock)
return true
}
if !block {
unlock(&c.lock)
return false
}
// Block on the channel. Some receiver will complete our operation for us.
//如果缓冲通道元素数量达到了通道容量
//获取这个发送gouroutine指针
gp := getg()
//新建一个sudog结构
mysg := acquireSudog()
mysg.releasetime = 0
if t0 != 0 {
mysg.releasetime = -1
}
// No stack splits between assigning elem and enqueuing mysg
// on gp.waiting where copystack can find it.
//设置sudog.elem=发送goroutine中需要发送的数据的地址
mysg.elem = ep
mysg.waitlink = nil
//设置sudog.g=发送gouroutine指针
mysg.g = gp
mysg.isSelect = false
//设置sudog.c=当前通道
mysg.c = c
gp.waiting = mysg
gp.param = nil
//将sudog结构放到通道的sendq队列中
c.sendq.enqueue(mysg)
//goparkunlock->用于协程切换的gopark函数->mcall(park_m)
//mcall中会将此goroutine当前的状态进行保存,在调度是恢复状态
//park_m中逻辑(以后分析goroutine的时候回详细分析,现在先粗略分析下)
//1、将此发送gouroutine休眠(状态由_Grunning变为_Gwaiting),等待被唤醒
//2、解除M和此gouroutine之间的关联
//3、调用schedule调度函数,让可以被执行的gouroutine放到M上
//4、由于此发送gouroutine休眠,阻塞
goparkunlock(&c.lock, waitReasonChanSend, traceEvGoBlockSend, 3)
// someone woke us up.
//发送gouroutine被唤醒后执行的代码
if mysg != gp.waiting {
throw("G waiting list is corrupted")
}
gp.waiting = nil
if gp.param == nil {
if c.closed == 0 {
throw("chansend: spurious wakeup")
}
//唤醒后发现通道被关,直接panic
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
}
如果是直接交给消息接收goroutine处理是调用send函数:
// send处理在通道为非缓冲通道或者缓冲通道的缓冲队列为空
// 直接将数据从发送goroutine,复制到接收goroutine,而不经过缓冲队列
// 接收goroutine接到数据后,调用goready,唤醒该goroutine,放入P的本地运行队列,并和M对接
func send(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
if raceenabled {
if c.dataqsiz == 0 {
racesync(c, sg)
} else {
// Pretend we go through the buffer, even though
// we copy directly. Note that we need to increment
// the head/tail locations only when raceenabled.
qp := chanbuf(c, c.recvx)
raceacquire(qp)
racerelease(qp)
raceacquireg(sg.g, qp)
racereleaseg(sg.g, qp)
c.recvx++
if c.recvx == c.dataqsiz {
c.recvx = 0
}
c.sendx = c.recvx // c.sendx = (c.sendx+1) % c.dataqsiz
}
}
if sg.elem != nil {
//直接将数据复制到接收goroutine
sendDirect(c.elemtype, sg, ep)
sg.elem = nil
}
gp := sg.g
unlockf()
gp.param = unsafe.Pointer(sg)
if sg.releasetime != 0 {
sg.releasetime = cputicks()
}
//唤醒接收goroutine
goready(gp, skip+1)
}
发送消息函数中隐藏着一个优先级:
func chanrecv2(c *hchan, elem unsafe.Pointer) (received bool) {
_, received = chanrecv(c, elem, true)
return
}
// 从通道接收数据,并将数据写入ep参数
// ep参数可能为nil,这样的话接收的数据将被忽略(_, ok := <-ch,ep 为_)
// 当ep不为nil,通道关闭,并且通道内无数据时,ep会被赋值为对应类型的零值
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
}
//同发送一样,如果通道为nil,则会永久阻塞
gopark(nil, nil, waitReasonChanReceiveNilChan, traceEvGoStop, 2)
throw("unreachable")
}
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 int64
if blockprofilerate > 0 {
t0 = cputicks()
}
lock(&c.lock)
if c.closed != 0 && c.qcount == 0 {
if raceenabled {
raceacquire(unsafe.Pointer(c))
}
unlock(&c.lock)
if ep != nil {
//当ep不为nil,通道关闭,并且通道内无数据时,ep会被赋值为对应类型的零值
typedmemclr(c.elemtype, ep)
}
return true, false
}
if sg := c.sendq.dequeue(); sg != nil {
// 发送goroutine的等待队列中,有等待着的goroutine
// 说明通道为非缓冲通道或者缓冲通道的缓冲队列已经满了
// 当通道为非缓冲通道时
// recv逻辑和send一样,取出发送队列中排在最前边的goroutine
// 然后不经过通道的缓冲区,直接拷贝
// 当缓冲通道的缓冲队列缓冲通道满时
// 先从缓冲通道中取出排在最前边的数据,写入到ep(若有)
// 清除缓冲通道对应位置的空间
// 取出发送队列中排在最前边的goroutine,将其所携带的数据放入缓冲队列尾部
// 由于此时缓冲队列是满的
// 所以从缓冲队列中拿出的数据地址,和发送goroutine放入数据的地址,是一个地址
recv(c, sg, ep, func() { unlock(&c.lock) }, 3)
return true, true
}
// 缓冲队列中不为空
if c.qcount > 0 {
// Receive directly from queue
qp := chanbuf(c, c.recvx)
if raceenabled {
raceacquire(qp)
racerelease(qp)
}
if ep != nil {
//ep不为nil,将缓冲队列中,索引为c.recvx对应的值写到ep中
typedmemmove(c.elemtype, ep, qp)
}
//将缓冲通道中c.recvx索引指向的值变为通道类型的零值
//由于上边已经判断ep != nil的情况了,所以这里直接将值丢弃
//为缓冲通道清理空间
typedmemclr(c.elemtype, qp)
c.recvx++
if c.recvx == c.dataqsiz {
//如果缓冲通道元素数量达到了通道容量,就将发送index改为0,构造环形数组
c.recvx = 0
}
c.qcount--
unlock(&c.lock)
return true, true
}
if !block {
unlock(&c.lock)
return false, false
}
// no sender available: block on this channel.
// 下边的逻辑和发送goroutine队列逻辑一样,就不重复分析了
gp := getg()
mysg := acquireSudog()
mysg.releasetime = 0
if t0 != 0 {
mysg.releasetime = -1
}
// No stack splits between assigning elem and enqueuing mysg
// on gp.waiting where copystack can find it.
mysg.elem = ep
mysg.waitlink = nil
gp.waiting = mysg
mysg.g = gp
mysg.isSelect = false
mysg.c = c
gp.param = nil
c.recvq.enqueue(mysg)
goparkunlock(&c.lock, waitReasonChanReceive, traceEvGoBlockRecv, 3)
// someone woke us up
if mysg != gp.waiting {
throw("G waiting list is corrupted")
}
gp.waiting = nil
if mysg.releasetime > 0 {
blockevent(mysg.releasetime-t0, 2)
}
closed := gp.param == nil
gp.param = nil
mysg.c = nil
releaseSudog(mysg)
return true, !closed
}
如果发送等待队列有数据,则调用recv函数,该函数会从缓冲区取最新数据,然后把发送等待队列上的最前面请求放入缓冲区的末尾位置。
func recv(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
if c.dataqsiz == 0 {
if raceenabled {
racesync(c, sg)
}
if ep != nil {
// copy data from sender
//非缓冲通道,并且ep != nil,直接将发送goroutine的数据写ep
recvDirect(c.elemtype, sg, ep)
}
} else {
// Queue is full. Take the item at the
// head of the queue. Make the sender enqueue
// its item at the tail of the queue. Since the
// queue is full, those are both the same slot.
// 缓冲通道,并且只有缓冲队列满了,才会走到这里
qp := chanbuf(c, c.recvx)
if raceenabled {
raceacquire(qp)
racerelease(qp)
raceacquireg(sg.g, qp)
racereleaseg(sg.g, qp)
}
// copy data from queue to receiver
if ep != nil {
//先从缓冲通道中取出c.recvx对应的数据,写入到ep
typedmemmove(c.elemtype, ep, qp)
}
// 将发送队列中排在最前边的goroutine所携带的数据
// 放入c.recvx对应的空间
typedmemmove(c.elemtype, qp, sg.elem)
c.recvx++
if c.recvx == c.dataqsiz {
c.recvx = 0
}
c.sendx = c.recvx // c.sendx = (c.sendx+1) % c.dataqsiz
}
sg.elem = nil
gp := sg.g
unlockf()
gp.param = unsafe.Pointer(sg)
if sg.releasetime != 0 {
sg.releasetime = cputicks()
}
goready(gp, skip+1)
}
消息接收函数的处理比发送函数多做了一件事:
如果发送队列有数据,消息接收函数在从缓冲区提取最前面的消息后需要把发送队列中的最前面数据拷贝进缓冲区,然后唤醒该等待的goroutine,并且设置sendx是最后一个缓冲区位置。其实就是移动环形缓冲区的sendx,使其记录缓冲区最后一个消息位置,当有发送消息记录进缓冲区时从这个位置向后记录。
func closechan(c *hchan) {
//关闭未初始化(nil)的通道,会panic
if c == nil {
panic(plainError("close of nil channel"))
}
lock(&c.lock)
if c.closed != 0 {
//关闭已经关闭的通道,会panic
unlock(&c.lock)
panic(plainError("close of closed channel"))
}
if raceenabled {
callerpc := getcallerpc()
racewritepc(unsafe.Pointer(c), callerpc, funcPC(closechan))
racerelease(unsafe.Pointer(c))
}
c.closed = 1
var glist *g
// release all readers
// 唤醒所有接收队列中的goroutine,清空接收队列
for {
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()
}
// 将goroutine入glist
// 为最后唤醒(goready)全部goroutine做准备
gp := sg.g
gp.param = nil
if raceenabled {
raceacquireg(gp, unsafe.Pointer(c))
}
gp.schedlink.set(glist)
glist = gp
}
// release all writers (they will panic)
// 唤醒所有发送队列中的goroutine,清空发送队列
// 该操作会使所有发送goroutine panic
for {
sg := c.sendq.dequeue()
if sg == nil {
break
}
//释放内存
sg.elem = nil
if sg.releasetime != 0 {
sg.releasetime = cputicks()
}
gp := sg.g
gp.param = nil
if raceenabled {
raceacquireg(gp, unsafe.Pointer(c))
}
gp.schedlink.set(glist)
glist = gp
}
unlock(&c.lock)
// Ready all Gs now that we've dropped the channel lock.
for glist != nil {
gp := glist
glist = glist.schedlink.ptr()
gp.schedlink = 0
//唤醒goroutine(Grunnable)
goready(gp, 3)
}
}
Channel的实现除去goroutine阻塞和唤醒部分之外并不复杂,使用缓冲区存放临时消息,使用等待队列储存缓冲区溢出的goroutine。