示例#1
0
func (this *Consumer) Request(n int) (int64, error) {
	if n < 1 {
		return 0, ErrNotPositiveInteger
	}

	next, err := this.Get()
	if err != nil {
		return 0, err
	}

	nextSeq := next + int64(n)
	cachedGate := this.cachedGate

	if nextSeq > cachedGate {
		var minSeq int64
		var err error

		for minSeq, err = ringbuffer.GetMinSeq(this.gates, math.MaxInt64); nextSeq > minSeq; minSeq, err = ringbuffer.GetMinSeq(this.gates, math.MaxInt64) {
			//log.Printf("consumer nextSeq = %d, minSeq = %d\n", nextSeq, minSeq)
			if err != nil {
				return 0, err
			}
			runtime.Gosched()
		}

		this.cachedGate = minSeq
	}

	return nextSeq, nil
}
示例#2
0
func (this *Producer) Request(n int) (int64, error) {
	if n < 1 {
		return 0, ErrNotPositiveInteger
	}

	cursor, err := this.Get()
	if err != nil {
		return 0, err
	}

	nextSeq := cursor + int64(n)
	wrapPoint := nextSeq - int64(this.bufferSize)
	cachedGate := this.cachedGate

	//log.Printf("producer cursor = %d, nextSeq = %d, wrapPoint = %d, cachedGate = %d\n", cursor, nextSeq, wrapPoint, cachedGate)

	// For the producer, cachedGate is the previous lowest consumer sequence. If wrapPoint
	// is greater than cacehdGate, that means the consumer has't reached the next slot yet,
	// which means we cannot do anything// with that slot until all the consumers have
	// passed it. So we wait...
	//
	// TODO: Figure out what "cachedGate > next" means
	if wrapPoint > cachedGate || cachedGate > cursor {
		var minSeq int64
		var err error

		for minSeq, err = ringbuffer.GetMinSeq(this.gates, cursor); wrapPoint > minSeq; minSeq, err = ringbuffer.GetMinSeq(this.gates, cursor) {
			if err != nil {
				return 0, err
			}
			//runtime.Gosched()
		}

		this.cachedGate = minSeq
	}

	return nextSeq, nil
}