channel的实现,都在$GOROOT/src/pkg/runtime/chan.c里
它是通过共享内存实现的。
struct Hchan {
}
ch := make(chan interface{}, 5)
具体的实现是chan.c里的 Hchan* runtime·makechan_c(ChanType *t, int64 hint)
此时,hint=5, t=interface{}
它完成的任务就是:
分配hint * sizeof(t) + sizeof(Hchan)的内存空间[也就是说,buffered chan的buffer越大,占用
内存越大]
ch <- 5 就会调用
void runtime·chansend(ChanType *t, Hchan *chan, byte *ep, bool *pres)
lock(chan)
如果chan是buffer chan {
比较当前已经放入buffer里的数据是否满了A
如果没有满 {
把ep(要放入到chan里的数据)拷贝到chan的内存区域 (此区域是sender/recver共享的)
找到receiver goroutine, make it ready, and schedule it to recv
} else {
已经满了
把当前goroutine状态设置为Gwaiting
yield
}
} else {
// 这是blocked chan
找到receiver goroutine (channel的隐喻就是一定存在多个goroutine)
让该goroutine变成ready (之前是Gwaiting), 从而参与schedule,获得控制权
具体执行什么,要看chanrecv的实现
}