// ParallelizeUntil is a framework that allows for parallelizing N
// independent pieces of work until done or the context is canceled.
327335
func ParallelizeUntil(ctx context.Context, workers, pieces int, doWorkPiece DoWorkPieceFunc) {
	var stop <-chan struct{}
	if ctx != nil {
		stop = ctx.Done()
	}

	toProcess := make(chan int, pieces)
	for i := 0; i < pieces; i++ {
		toProcess <- i
	}
	close(toProcess)

	if pieces < workers {
		workers = pieces
	}

	wg := sync.WaitGroup{}
	wg.Add(workers)
	for i := 0; i < workers; i++ {
		go func() {
			defer utilruntime.HandleCrash()
			defer wg.Done()
			for piece := range toProcess {
				select {
				case <-stop:
					return
				default:
					doWorkPiece(piece)
				}
			}
		}()
	}
	wg.Wait()
}
942300
func goFinalizeCall(cCtx *C.duk_context) {
	d := contextFromPointer(cCtx)

	funPtr, ctx := d.getFunctionPtrs()
	d.transmute(unsafe.Pointer(ctx))

	d.fnIndex.delete(funPtr)
}
83119
func (o *Once) WaitUntilRunning(ctx context.Context) error {
	state := State(o.state.Load())
	if state == Running {
		return nil
	}
	if state > Running {
		return yarpcerrors.FailedPreconditionErrorf("could not wait for instance to start running: current state is %q", getStateName(state))
	}

	if _, ok := ctx.Deadline(); !ok {
		return yarpcerrors.InvalidArgumentErrorf("could not wait for instance to start running: deadline required on request context")
	}

	select {
	case <-o.startCh:
		state := State(o.state.Load())
		if state == Running {
			return nil
		}
		return yarpcerrors.FailedPreconditionErrorf("instance did not enter running state, current state is %q", getStateName(state))
	case <-ctx.Done():
		return yarpcerrors.FailedPreconditionErrorf("context finished while waiting for instance to start: %s", ctx.Err().Error())
	}
}
1283502
func (s *SAMLProviderListEntry) SetValidUntil(v time.Time) *SAMLProviderListEntry {
	s.ValidUntil = &v
	return s
}
534012
func (s *StructStruct) CallUntil(m string, args []interface{}, until func([]interface{}) bool) ([]interface{}, bool) {
	res, ok := s.Call(m, args)
	if ok && until(res) {
		return res, true
	}
	for _, item := range s.data {
		if item.embedded == nil {
			continue
		}
		res, ok := item.embedded.CallUntil(m, args, until)
		if ok && until(res) {
			return res, true
		}
	}
	return nil, false
}
1330853
func (q *Queue) nextOrNil() *Job {
	q.waitingLock.Lock()
	defer q.waitingLock.Unlock()
	if len(q.waiting) > 0 {
		return q.waiting[0]
	}
	return nil
}
1094614
func readUntilNullWorkSizeBatch(socketRead io.ReadCloser,
	batch []byte, workSize int) (size int, err error) {
	err = nil
	size = 0
	if workSize == 0 {
		size, err = io.ReadFull(socketRead, batch)
	} else {
		lastCheckedForNull := 0
		for err == nil {
			var offset int
			offset, err = socketRead.Read(batch[size:])
			size += offset
			if err == nil && size < len(batch) {
				endCheck := size - (size % workSize)
				if zeroWorkItem(batch[lastCheckedForNull:endCheck], workSize) {
					if size%workSize != 0 {
						rem := workSize - (size % workSize)
						offset, err = io.ReadFull(socketRead, batch[size:size+rem])
						size += offset
					}
					return
				}
				lastCheckedForNull = endCheck // need to check partial work items
			} else {
				return
			}
		}
	}
	return
}
368478
func (s *SimpleFSHandler) SimpleFSListRecursiveToDepth(ctx context.Context, arg keybase1.SimpleFSListRecursiveToDepthArg) error {
	ctx, cancel := s.wrapContextWithTimeout(ctx)
	defer cancel()
	cli, err := s.client()
	if err != nil {
		return err
	}
	return cli.SimpleFSListRecursiveToDepth(ctx, arg)
}
9596
func (q *Queue) TakeUntil(checker func(item interface{}) bool) ([]interface{}, error) {
	if checker == nil {
		return nil, nil
	}

	q.lock.Lock()

	if q.disposed {
		q.lock.Unlock()
		return nil, ErrDisposed
	}

	result := q.items.getUntil(checker)
	q.lock.Unlock()
	return result, nil
}
154940
func (c *Conn) SkipUntilIndex(delims ...string) (int, error) {
	_, i, err := c.readUntil(false, delims...)
	return i, err
}
927980
func (b *Bucket) CopyLargeFileInParallel(sourcePath string, destPath string, contentType string, perm ACL, options Options, maxConcurrency int) error {

	if maxConcurrency < 1 {
		maxConcurrency = 1
	}

	currentLength, err := b.GetContentLength(sourcePath)

	log.Printf("Parallel Copy large file[size: %d] from %s to %s\n", currentLength, sourcePath, destPath)

	if err != nil {
		return err
	}

	if currentLength < maxCopytSize {
		_, err := b.PutCopy(destPath, perm,
			CopyOptions{},
			b.Path(sourcePath))
		return err
	}

	multi, err := b.InitMulti(destPath, contentType, perm, options)
	if err != nil {
		return err
	}

	numParts := (currentLength + defaultChunkSize - 1) / defaultChunkSize
	completedParts := make([]Part, numParts)

	errChan := make(chan error, numParts)
	limiter := make(chan struct{}, maxConcurrency)

	var start int64 = 0
	var to int64 = 0
	var partNumber = 0
	sourcePathForCopy := b.Path(sourcePath)

	for start = 0; start < currentLength; start = to {
		to = start + defaultChunkSize
		if to > currentLength {
			to = currentLength
		}
		partNumber++

		rangeStr := fmt.Sprintf("bytes=%d-%d", start, to-1)
		limiter <- struct{}{}
		go func(partNumber int, rangeStr string) {
			_, part, err := multi.PutPartCopyWithContentLength(partNumber,
				CopyOptions{CopySourceOptions: rangeStr},
				sourcePathForCopy, currentLength)
			if err == nil {
				completedParts[partNumber-1] = part
			} else {
				log.Printf("Unable in PutPartCopy of part %d for %s: %v\n", partNumber, sourcePathForCopy, err)
			}
			errChan <- err
			<-limiter
		}(partNumber, rangeStr)
	}

	fullyCompleted := true
	for range completedParts {
		err := <-errChan
		if err != nil {
			fullyCompleted = false
		}
	}

	if fullyCompleted {
		err = multi.Complete(completedParts)
	} else {
		err = multi.Abort()
	}

	return err
}
80801
func (c *EMR) WaitUntilClusterRunning(input *DescribeClusterInput) error {
	return c.WaitUntilClusterRunningWithContext(aws.BackgroundContext(), input)
}
87473
func (c *AutoScaling) WaitUntilGroupInService(input *DescribeAutoScalingGroupsInput) error {
	return c.WaitUntilGroupInServiceWithContext(aws.BackgroundContext(), input)
}
1127247
func (i *InParallel) Add(task func() error) {
	i.wg.Add(1)

	go func() {
		defer i.wg.Done()
		err := task()
		if err != nil {
			i.pool.Put(err)
		}
	}()
}
587613
func (c *EC2) WaitUntilConversionTaskCompleted(input *DescribeConversionTasksInput) error {
	return c.WaitUntilConversionTaskCompletedWithContext(aws.BackgroundContext(), input)
}
732856
func (l *Scanner) FetchUntil(c byte) bool {
	l.resetItem()
	if l.pos == len(l.data) {
		return false
	}
	return l.fetchOctet(c)
}
725033
func PollUntil(interval time.Duration, condition ConditionFunc, stopCh <-chan struct{}) error {
	ctx, cancel := contextForChannel(stopCh)
	defer cancel()
	return WaitFor(poller(interval, 0), condition, ctx.Done())
}