github.com/sitano/gsysint@v0.0.0-20190607084937-69a4f3233e4e/g/chan.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package g
     6  
     7  // This file contains the implementation of Go channels.
     8  
     9  // Invariants:
    10  //  At least one of c.sendq and c.recvq is empty,
    11  //  except for the case of an unbuffered channel with a single goroutine
    12  //  blocked on it for both sending and receiving using a select statement,
    13  //  in which case the length of c.sendq and c.recvq is limited only by the
    14  //  size of the select statement.
    15  //
    16  // For buffered channels, also:
    17  //  c.qcount > 0 implies that c.recvq is empty.
    18  //  c.qcount < c.dataqsiz implies that c.sendq is empty.
    19  
    20  import (
    21  	"unsafe"
    22  )
    23  
    24  const (
    25  	maxAlign  = 8
    26  	hchanSize = unsafe.Sizeof(HChan{}) + uintptr(-int(unsafe.Sizeof(HChan{}))&(maxAlign-1))
    27  	debugChan = false
    28  )
    29  
    30  type HChan struct {
    31  	qcount   uint           // total data in the queue
    32  	dataqsiz uint           // size of the circular queue
    33  	buf      unsafe.Pointer // points to an array of dataqsiz elements
    34  	elemsize uint16
    35  	closed   uint32
    36  	elemtype *Type // element type
    37  	sendx    uint  // send index
    38  	recvx    uint  // receive index
    39  	recvq    WaitQ // list of recv waiters
    40  	sendq    WaitQ // list of send waiters
    41  
    42  	// lock protects all fields in hchan, as well as several
    43  	// fields in sudogs blocked on this channel.
    44  	//
    45  	// Do not change another G's status while holding this lock
    46  	// (in particular, do not ready a G), as this can deadlock
    47  	// with stack shrinking.
    48  	lock Mutex
    49  }
    50  
    51  type WaitQ struct {
    52  	first *Sudog
    53  	last  *Sudog
    54  }
    55