func (b *BoundedChan) Enqueue(e Elt) {
	b.adjust()
	startHead := index(atomic.LoadUint64((*uint64)(&b.q.H)))
	myInd := index(atomic.AddUint64((*uint64)(&b.q.T), 1) - 1)
	cell, cellInd := myInd.SplitInd()
	seg := b.q.findCell(b.tail, cell)
	if myInd > startHead && (myInd-startHead) > index(uint64(b.bound)) {
		const patience = 4
		for i := 0; i < patience; i++ {
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[cellInd])),
				sentinel, unsafe.Pointer(e)) {
				if debug {
					dbgPrint("[enq] swapped out for sentinel\n")
				}
				return
			}
		}
		var w interface{} = makeWeakWaiter(2)
		if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[cellInd])),
			unsafe.Pointer(nil), unsafe.Pointer(Elt(&w))) {
			w.(*weakWaiter).Wait()
			if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[cellInd])),
				unsafe.Pointer(Elt(&w)), unsafe.Pointer(e)) {
				if debug {
					dbgPrint("[enq] blocked then swapped successfully\n")
				}
				return
			}  
		} else if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[cellInd])),
			sentinel, unsafe.Pointer(e)) {
			if debug {
				dbgPrint("[enq] swapped out for sentinel\n")
			}
			return
		}
	} else {
		if tryCas(seg, cellInd, unsafe.Pointer(e)) {
			if debug {
				dbgPrint("[enq] successful tryCas\n")
			}
			return
		}
	}
	for i := 0; ; i++ {  
		if i >= 2 {
			panic("[enq] bug!")
		}
		ptr := atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[cellInd])))
		w := (*waiter)(ptr)
		w.Send(e)
		if debug {
			dbgPrint("[enq] sending to waiter on %v\n", ptr)
		}
		return
	}
}
Enqueue sends e on b. If there are already >=bound goroutines blocking, then
Enqueue will block until sufficiently many elements have been received.
package fchan
import (
	"runtime"
	"sync/atomic"
	"unsafe"
)
var (
	s        = 1
	sentinel = unsafe.Pointer(&s)
)
type waitch chan struct{}
func waitChan() waitch {
	return make(chan struct{}, 2)
}
type BoundedChan struct {
	q          *queue
	head, tail *segment
	bound      uint64
}
func NewBounded(bufsz uint64) *BoundedChan {
	segPtr := &segment{}
	cur := segPtr
	for b := uint64(segSize); b < bufsz; b += segSize {
		cur.Next = &segment{ID: index(b) >> segShift}
		cur = cur.Next
	}
	q := &queue{
		H:           0,
		T:           0,
		SpareAllocs: segList{MaxSpares: int64(runtime.GOMAXPROCS(0))},
	}
	return &BoundedChan{
		q:     q,
		head:  segPtr,
		tail:  segPtr,
		bound: bufsz,
	}
}
func (b *BoundedChan) NewHandle() *BoundedChan {
	return &BoundedChan{
		q:     b.q,
		head:  b.head,
		tail:  b.tail,
		bound: b.bound,
	}
}
func (b *BoundedChan) adjust() {
	H := index(atomic.LoadUint64((*uint64)(&b.q.H)))
	T := index(atomic.LoadUint64((*uint64)(&b.q.T)))
	cellH, _ := H.SplitInd()
	advance(&b.head, cellH)
	cellT, _ := T.SplitInd()
	advance(&b.tail, cellT)
}
func tryCas(seg *segment, segInd index, elt unsafe.Pointer) bool {
	return atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[segInd])),
		sentinel, elt) ||
		atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[segInd])),
			unsafe.Pointer(nil), elt) ||
		atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&seg.Data[segInd])),
			sentinel, elt)
}