• Golang channel实现


    Golang channel 初探

    Goroutine和channel是Golang实现高并发的基础。深入理解其背后的实现,写起代码来才不慌-_-

    首先我们定义如下代码,来看看Golang底层是如何实现channel的。

    package main
    
    import (
            "fmt"
    )
    
    func main() {
            c := make(chan int, 5)
            c <- 1
    
            g := <-c
            fmt.Println(g)
    
    }
    

    编译后我们看下相关函数

    go build -gcflags "-N -l" -o chan chan.go
    go tool objdump -s "main.main" chan
    

    可以看到初始化调用了runtime.makechan,

    写channel调用了runtime.chansend1,

    读channel调用了runtime.chanrecv1.

    在runtime/chan.go中我们找到对应函数

    func makechan(t *chantype, size int) *hchan 
    

    返回一个hchan结构,我们先看下channel的结构,我们为一眼可以理解的加点注释。

    channel结构

    type hchan struct {
       qcount   uint           // total data in the queue 队列中存在的个数
       dataqsiz uint           // size of the circular queue buffer大小 实现看起来是个循环数组
       buf      unsafe.Pointer // points to an array of dataqsiz elements 数组指针
       elemsize uint16       //channel类型的大小
       closed   uint32      //channel是否关闭
       elemtype *_type // element type //channel 类型
       sendx    uint   // send index  //发送index
       recvx    uint   // receive index //接收index
       recvq    waitq  // list of recv waiters //接收链表 即读channel的goroutine
       sendq    waitq  // list of send waiters //发送链表 即写channel的goroutine
    
       // 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
    }
    

    接下来来看下到底如何初始化的。

    channel初始化

    func makechan(t *chantype, size int) *hchan {
    	elem := t.elem
       ...
    	//一些合法判断 
    	
    
    	// Hchan does not contain pointers interesting for GC when elements stored in buf do not contain pointers.
    	// buf points into the same allocation, elemtype is persistent.
    	// SudoG's are referenced from their owning thread so they can't be collected.
    	// TODO(dvyukov,rlh): Rethink when collector can move allocated objects.
    	var c *hchan
    	switch {
    	//channel buffer大小为0 或者类型大小为0
    	case size == 0 || elem.size == 0:
    		// Queue or element size is zero.
    		c = (*hchan)(mallocgc(hchanSize, nil, true))
    		// Race detector uses this location for synchronization.
    		c.buf = unsafe.Pointer(c)
    		
    		//channel非指针
    	case elem.kind&kindNoPointers != 0:
    		// Elements do not contain pointers.
    		// Allocate hchan and buf in one call.
    		c = (*hchan)(mallocgc(hchanSize+uintptr(size)*elem.size, nil, true))
    		c.buf = add(unsafe.Pointer(c), hchanSize)
    	default:
    		// Elements contain pointers.
    		c = new(hchan)
    		c.buf = mallocgc(uintptr(size)*elem.size, elem, true)
    	}
    
    	c.elemsize = uint16(elem.size)
    	c.elemtype = elem
    	c.dataqsiz = uint(size)
    
    	if debugChan {
    		print("makechan: chan=", c, "; elemsize=", elem.size, "; elemalg=", elem.alg, "; dataqsiz=", size, "
    ")
    	}
    	return c
    }
    

    可以看出主要是根据channel类型以及buffer大小申请hcan.buf的内存,同时设置对应的datasiz、elemsize等,比较简单。

    那么写channel是怎么实现的呢

    写channel

    c<-1这种形式的写channel会调用chansend1

    // entry point for c <- x from compiled code
    //go:nosplit
    func chansend1(c *hchan, elem unsafe.Pointer) {
    	chansend(c, elem, true, getcallerpc())
    }
    
    
    

    看源码还有select的selectnbsend,reflect的reflect_chansend,

    它们最后都会调用chansend,所以只用看chansend的实现,它们只是参数不一样而已。

    /*
     * generic single channel send/recv
     * If block is not nil,
     * then the protocol will not
     * sleep but return if it could
     * not complete.
     *
     * sleep can wake up with g.param == nil
     * when a channel involved in the sleep has
     * been closed.  it is easiest to loop and re-run
     * the operation; we'll see that it's now closed.
     */
    func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
        //如果设置了block为true 且channel为nil,goroutine将会死在这
    	if c == nil {
    		if !block {
    			return false
    		}
    		gopark(nil, nil, "chan send (nil chan)", traceEvGoStop, 2)
    		throw("unreachable")
    	}
    	//忽略一些无用代码
    	.....
    	//block false  channel没buffer或者已满 直接返回
    	if !block && c.closed == 0 && ((c.dataqsiz == 0 && c.recvq.first == nil) ||
    		(c.dataqsiz > 0 && c.qcount == c.dataqsiz)) {
    		return false
    	}
    
    	...
    	
    	lock(&c.lock)
        //写已经关闭的channel 将会panic
    	if c.closed != 0 {
    		unlock(&c.lock)
    		panic(plainError("send on closed channel"))
    	}
    
        //优先检查读队列是否有等待的goroutine,有的话直接调用并返回
    	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
    	}
        
        //channel有buffer可以写
    	if c.qcount < c.dataqsiz {
    		// Space is available in the channel buffer. Enqueue the element to send.
    		//根据sendx计算该数据在数组的位置
    		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
    	}
        //没buffer 且block false直接返回
    	if !block {
    		unlock(&c.lock)
    		return false
    	}
       //没buffer写 初始化一个sudog结构
       
    	// Block on the channel. Some receiver will complete our operation for us.
    	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.
    	
    	//将该数据放在sudog的elem里,所以没buffer的channel数据其实在调用的goroutine里
    	mysg.elem = ep
    	mysg.waitlink = nil
    	mysg.g = gp
    	mysg.isSelect = false
    	mysg.c = c
    	gp.waiting = mysg
    	gp.param = nil
    	
    	//放到channel的写队列,并阻塞
    	c.sendq.enqueue(mysg)
    	goparkunlock(&c.lock, "chan send", traceEvGoBlockSend, 3)
    
    	// someone woke us up.
    	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(plainError("send on closed channel"))
    	}
    	gp.param = nil
    	if mysg.releasetime > 0 {
    		blockevent(mysg.releasetime-t0, 2)
    	}
    	mysg.c = nil
    	releaseSudog(mysg)
    	return true
    

    ​ 整个写channel的逻辑还是很清晰的。 参照这个其实读channel差不多可以推断出是个对应的逻辑。

    读channel

    我们还是在runtime/chan.go中找到相关代码

    // entry points for <- c from compiled code
    //go:nosplit
    func chanrecv1(c *hchan, elem unsafe.Pointer) {
    	chanrecv(c, elem, true)
    }
    
    

    读channel还有chanrecv2,selectnbrecv,selectnbrecv2,reflect_chanrecv。跟写channel一样,

    它们之间也是参数的区别。

    我们继续看chanrecv

    // 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.
    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, "
    ")
    	}
        //block true的时候读nil的channel 将会永远阻塞
    	if c == nil {
    		if !block {
    			return
    		}
    		gopark(nil, nil, "chan receive (nil chan)", traceEvGoStop, 2)
    		throw("unreachable")
    	}
    	
        //block false的时候 判断channel buffer 已满或者没buffer 没有读的goroutine就直接返回
    	if !block && (c.dataqsiz == 0 && c.sendq.first == nil ||
    		c.dataqsiz > 0 && atomic.Loaduint(&c.qcount) == 0) &&
    		atomic.Load(&c.closed) == 0 {
    		return
    	}
    
    	
    
    	lock(&c.lock)
         //如果channel已空 且没有读的数据 清除并退出
    	if c.closed != 0 && c.qcount == 0 {
    		if raceenabled {
    			raceacquire(unsafe.Pointer(c))
    		}
    		unlock(&c.lock)
    		if ep != nil {
    			typedmemclr(c.elemtype, ep)
    		}
    		return true, false
    	}
        //还是优先检查写队列 拿出阻塞的goroutine
    	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).
    		recv(c, sg, ep, func() { unlock(&c.lock) }, 3)
    		return true, true
    	}
        //buffer中有数据 跟写对应处理
    	if c.qcount > 0 {
    		// Receive directly from queue
    		qp := chanbuf(c, c.recvx)
    		if raceenabled {
    			raceacquire(qp)
    			racerelease(qp)
    		}
    		if ep != nil {
    			typedmemmove(c.elemtype, ep, qp)
    		}
    		typedmemclr(c.elemtype, qp)
    		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
    	}
        //没有数据读 初始化一个sudog并阻塞
    	// no sender available: block on this channel.
    	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, "chan receive", 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
    }
    

    关channel

    最后我们来看看close channel

    func closechan(c *hchan) {
    //关nil channel直接panic
    	if c == nil {
    		panic(plainError("close of nil channel"))
    	}
    
    	lock(&c.lock)
    	//重复关闭 panic
    	if c.closed != 0 {
    		unlock(&c.lock)
    		panic(plainError("close of closed channel"))
    	}
    
    	if raceenabled {
    		callerpc := getcallerpc()
    		racewritepc(unsafe.Pointer(c), callerpc, funcPC(closechan))
    		racerelease(unsafe.Pointer(c))
    	}
     //设置channel标记
    	c.closed = 1
    
    	var glist *g
    
    	// release all readers
    	for {
    	   //通知所有的读goroutine
    		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.g
    		gp.param = nil
    		if raceenabled {
    			raceacquireg(gp, unsafe.Pointer(c))
    		}
    		gp.schedlink.set(glist)
    		glist = gp
    	}
    
    	// release all writers (they will 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
    		goready(gp, 3)
    	}
    }
    
  • 相关阅读:
    chapter16 计算机体系结构基础
    ASP.NET 2.0加密Web.config 配置文件
    用IIS6.0的Kernel Caching 压缩技术提高应用程序性能
    .net中怎样执行一个字符串
    ASP.NET 页面生存周期中的关键事件
    ASP.NET获取客户端IP及MAC地址
    DOS命令关闭计算机
    1个式子检测密码强度
    徐州话六级考试
    .net2.0中新增的Substitution控件
  • 原文地址:https://www.cnblogs.com/golangguo/p/10633702.html
Copyright © 2020-2023  润新知