文章目录
channel底层实现
// channel 类型定义
type hchan struct {
// channel 中的元素数量, len
qcount uint // total data in the queue
// channel 的大小, cap
dataqsiz uint // size of the circular queue
// channel 的缓冲区,环形数组实现
buf unsafe.Pointer // points to an array of dataqsiz elements
// 单个元素(数据类型)的大小
elemsize uint16
// closed 标志位
closed uint32
// 元素的类型
elemtype *_type // element type 指向类型元数据 (内存复制、垃圾回收等机制依赖数据的类型信息)
// send 和 recieve 的索引,用于实现环形数组队列(用于记录 交替读写的下标位置)
sendx uint // send index
recvx uint // receive index
// recv goroutine 等待队列 想读取数据但又被阻塞住的 goroutine 队列
recvq waitq // list of recv waiters
// send 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