作为golang并发编程思想的重要组成,channel(通道)非常重要,和goroutine(go协程)一起使用,用来实现go的CSP(Communicating Sequential Processes)并发模型。
Do not communicate by sharing memory; instead, share memory by communicating
不要以共享内存的方式来通信,相反,要通过通信来共享内存。
因为channel的重要性,有必要对其原理和源码进行学习,在参考了网络上各种大牛的分享后,将其作为笔记记录下来,如有不足之处,还请指正。
源码路径: go1.11/src/runtime/chan.go
channel结构体
type hchan struct {
// 通道中实际的元素个数,len(ch)的返回值
qcount uint //total data in the queue
// 通道的容量,cap(ch)的返回值,qcount <= dataqsiz
// ch := make(chan T, x)中的 x,dataqsiz为0表示非缓冲通道
dataqsiz uint // size of the circular queue
//存储通道元素的缓冲队列地址,使用环形数组实现
buf unsafe.Pointer // points to an array of dataqsiz elements
//通道内单个元素的大小,单位为字节
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 protects all fields in hchan, as well as several
// fields in sudogs blocked on this channel.
//
// Do not change another G's status while holding this lock
// (in particular, do not ready a G), as this can deadlock
// with stack shrinking.
//操作通道时使用的互斥锁
lock mutex
}
recvq 和 sendq 对应的结构 waitq 是一个链表,包含一个头结点和一个尾结点,队列中的每个成员是一个sudog结构体
type waitq struct {
first *sudog
last *sudog
}
// sudog represents a g in a wait list, such as for sending/receiving
// on a channel.
//
// sudog is necessary because the g ↔ synchronization object relation
// is many-to-many. A g can be on many wait lists, so there may be
// many sudogs for one g; and many gs may be waiting on the same
// synchronization object, so there may be many sudogs for one object.
//
// sudogs are allocated from a special pool. Use acquireSudog and
// releaseSudog to allocate and free them.
// 当goroutine遇到阻塞,或需要等待的场景时,会被打包成sudog这样一个结构(封装了该goroutine指针)
// 之所以需要 sudog是由于goroutine和同步对象的关系是多对多的
// 一个goroutine可以在多个等待队列中,因此一个goroutine可能被打包为多个sudog
// 许多goroutine可能在同一个同步对象上等待,因此一个对象可能有多个sudog
// sudog是从一个特殊的池中分配的。使用AcquireDog和ReleaseSudog分配和释放它们
type sudog struct {
// The following fields are protected by the hchan.lock of the
// channel this sudog is blocking on. shrinkstack depends on
// this for sudogs involved in channel ops.
// 以下的这些字段都是被该goroutine所在的通道中的hchan.lock来保护的
g *g
// isSelect indicates g is participating in a select, so
// g.selectDone must be CAS'd to win the wake-up race.
isSelect bool
//sudog双向链表对应的指针
next *sudog
prev *sudog
elem unsafe.Pointer // data element (may point to stack)
// The following fields are never accessed concurrently.
// For channels, waitlink is only accessed by g.
// For semaphores, all fields (including the ones above)
// are only accessed when holding a semaRoot lock.
acquiretime int64
releasetime int64
ticket uint32
parent *sudog // semaRoot binary tree
waitlink *sudog // g.waiting list or semaRoot
waittail *sudog // semaRoot
c *hchan // channel
}
make(chan T, x)
func makechan(t *chantype, size int) *hchan {
elem := t.elem
//检查T类型大小是否超过限制,比如传入一个大于64k大数组,会报错
//64位操作系统下
//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
}
从makechan代码中,可以以下总结几点
1、当创建的是非缓冲通道或者缓冲通道的元素类型大小为0时,是不需要申请缓冲队列的内存的
2、当创建的是缓冲通道,并且通道元素类型不是指针类型的,会向系统申请
一块连续内存
,用来存放hchan结构体和缓冲队列3、当创建的是缓冲通道,并且通道元素类型是指针类型的,会向系统申请
两块内存
,用来存放hchan结构体和缓冲队列4、当创建缓冲通道时,如果通道元素没有实际意义(如信号的传递)时,可以用
make(chan struct{}, n)
,因为 struct{} 类型的大小为0,创建通道时,会走第一个case, 不会为缓冲队列分配内存
向通道发送数据 ch <- x
// entry point for c <- x from compiled code
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))
}
// Fast path: check for failed non-blocking operation without acquiring the lock.
//
// After observing that the channel is not closed, we observe that the channel is
// not ready for sending. Each of these observations is a single word-sized read
// (first c.closed and second c.recvq.first or c.qcount depending on kind of channel).
// Because a closed channel cannot transition from 'ready for sending' to
// 'not ready for sending', even if the channel is closed between the two observations,
// they imply a moment between the two when the channel was both not yet closed
// and not ready for sending. We behave as if we observed the channel at that moment,
// and report that the send cannot proceed.
//
// It is okay if the reads are reordered here: if we observe that the channel is not
// 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 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
}
// send processes a send operation on an empty channel c.
// The value ep sent by the sender is copied to the receiver sg.
// The receiver is then woken up to go on its merry way.
// Channel c must be empty and locked. send unlocks c with unlockf.
// sg must already be dequeued from c.
// ep must be non-nil and point to the heap or the caller's stack.
// 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)
}
原文地址 https://www.jianshu.com/p/b9a76325ccc5
从通道接收数据 <- ch
func chanrecv2(c *hchan, elem unsafe.Pointer) (received bool) {
_, received = chanrecv(c, elem, true)
return
}
// chanrecv receives on channel c and writes the received data to ep.
// ep may be nil, in which case received data is ignored.
// If block == false and no elements are available, returns (false, false).
// Otherwise, if c is closed, zeros *ep and returns (true, false).
// Otherwise, fills in *ep with an element and returns (true, true).
// A non-nil ep must point to the heap or the caller's stack.
// 从通道接收数据,并将数据写入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")
}
// Fast path: check for failed non-blocking operation without acquiring the lock.
//
// After observing that the channel is not ready for receiving, we observe that the
// channel is not closed. Each of these observations is a single word-sized read
// (first c.sendq.first or c.qcount, and second c.closed).
// Because a channel cannot be reopened, the later observation of the channel
// being not closed implies that it was also not closed at the moment of the
// first observation. We behave as if we observed the channel at that moment
// and report that the receive cannot proceed.
//
// 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 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 {
// Found a waiting sender. If buffer is size 0, receive value
// directly from sender. Otherwise, receive from head of queue
// and add sender's value to the tail of the queue (both map to
// the same buffer slot because the queue is full).
// 发送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 processes a receive operation on a full channel c.
// There are 2 parts:
// 1) The value sent by the sender sg is put into the channel
// and the sender is woken up to go on its merry way.
// 2) The value received by the receiver (the current G) is
// written to ep.
// For synchronous channels, both values are the same.
// For asynchronous channels, the receiver gets its data from
// the channel buffer and the sender's data is put in the
// channel buffer.
// Channel c must be full and locked. recv unlocks c with unlockf.
// sg must already be dequeued from c.
// A non-nil ep must point to the heap or the caller's stack.
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)
}
关闭通道close(ch)
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涉及到select相关的源码分析,等和select源码一起分析吧~
最后,贴上通道数据传递的图,结合通道发送、接收的源码来看~非常形象
参考:
http://www.cnblogs.com/zkweb/p/7815600.html?utm_campaign=studygolang.com&utm_medium=studygolang.com&utm_source=studygolang.com
https://blog.csdn.net/qq_25870633/article/details/83388952
http://legendtkl.com/2017/07/30/understanding-golang-channel/
有疑问加站长微信联系(非本文作者)