// NOTE: This function needs to be safe for
// concurrent goroutines waiting on nextWg.
180751
func (e *CElement) SetNext(newNext *CElement) {
	e.mtx.Lock()

	oldNext := e.next
	e.next = newNext
	if oldNext != nil && newNext == nil {
		// See https://golang.org/pkg/sync/:
		//
		// If a WaitGroup is reused to wait for several independent sets of
		// events, new Add calls must happen after all previous Wait calls have
		// returned.
		e.nextWg = waitGroup1() // WaitGroups are difficult to re-use.
		e.nextWaitCh = make(chan struct{})
	}
	if oldNext == nil && newNext != nil {
		e.nextWg.Done()
		close(e.nextWaitCh)
	}
	e.mtx.Unlock()
}
477634
func (_m *RWSetScanner) Next() (*transientstore.EndorserPvtSimulationResults, error) {
	ret := _m.Called()

	var r0 *transientstore.EndorserPvtSimulationResults
	if rf, ok := ret.Get(0).(func() *transientstore.EndorserPvtSimulationResults); ok {
		r0 = rf()
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).(*transientstore.EndorserPvtSimulationResults)
		}
	}

	var r1 error
	if rf, ok := ret.Get(1).(func() error); ok {
		r1 = rf()
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
1018624
func (twg *TimeoutWaitGroup) Wait(waitDuration time.Duration) int {
	twg.mutex.Lock()
	if twg.workers == 0 {
		twg.mutex.Unlock()
		return 0
	}
	// Don't make the chan unless its needed. This lets us avoid a constructor
	if twg.last == nil {
		twg.last = make(chan struct{})
	}
	timeout := time.After(waitDuration)
	twg.mutex.Unlock()
	select {
	case <-twg.last:
	case <-timeout:
	}
	twg.mutex.Lock()
	defer twg.mutex.Unlock()
	return twg.workers
}
459483
func (wg *WaitGroup) Wait() {
	spin := NewSpinner(SpinPriorityHigh)
	for wg.Active() {
		spin.Yield()
	}
}
261271
func (nfei *NotFoundErrorIterator) Next() (*cb.Block, cb.Status) {
	return nil, cb.Status_NOT_FOUND
}
745509
func (w *Worker) waitIndexFuture(f raft.IndexFuture) (uint64, error) {
	errch := make(chan error, 1)
	go func() {
		errch <- f.Error()
	}()
	select {
	case <-w.catacomb.Dying():
		return 0, w.catacomb.ErrDying()
	case err := <-errch:
		return f.Index(), err
	}
}
646575
func (g *GoroutinePriorityPool) Wait() error {
	g.wg.Wait()
	return g.ctx.Err()
}
735728
func (c *C) Next() {
	c.index++
	s := int8(len(c.handlers))
	if c.index < s {
		c.handlers[c.index](c)
	}
}
1173421
func (api *DiskAPI) AsyncSleepWhileCopying(id int64, timeout time.Duration) (chan (interface{}), chan (interface{}), chan (error)) {
	handler := waitingForAvailableFunc(func() (hasAvailable, error) {
		return api.Read(id)
	}, 0)
	return poll(handler, timeout)
}
1203811
func (s *scene) WaitFlagAndFetch(topic string) (interface{}, error) {
	err := s.WaitFlag(topic)
	if err != nil {
		return nil, err
	}
	return s.Fetch(topic)
}
877619
func (p *planner) asyncPlanWait(waitCh chan struct{}, future raft.ApplyFuture,
	result *structs.PlanResult, pending *pendingPlan) {
	defer metrics.MeasureSince([]string{"nomad", "plan", "apply"}, time.Now())
	defer close(waitCh)

	// Wait for the plan to apply
	if err := future.Error(); err != nil {
		p.logger.Error("failed to apply plan", "error", err)
		pending.respond(nil, err)
		return
	}

	// Respond to the plan
	result.AllocIndex = future.Index()

	// If this is a partial plan application, we need to ensure the scheduler
	// at least has visibility into any placements it made to avoid double placement.
	// The RefreshIndex computed by evaluatePlan may be stale due to evaluation
	// against an optimistic copy of the state.
	if result.RefreshIndex != 0 {
		result.RefreshIndex = maxUint64(result.RefreshIndex, result.AllocIndex)
	}
	pending.respond(result, nil)
}
1133678
func (w *WindowedSchedule) Wait(last time.Time) Response {
	// If within the window we wait our interval and return
	// otherwise we exit with a completed state.
	var m uint

	if (last == time.Time{}) {
		// the first waiting in cycles, so
		// set the `stopOnTime` determining the right-window boundary
		w.setStopOnTime()
	}

	// Do we even have a specific start time?
	if w.StartTime != nil {
		// Wait till it is time to start if before the window start
		if time.Now().Before(*w.StartTime) {
			wait := w.StartTime.Sub(time.Now())
			logger.WithFields(log.Fields{
				"_block":         "windowed-wait",
				"sleep-duration": wait,
			}).Debug("Waiting for window to start")
			time.Sleep(wait)
		}
	}

	// Do we even have a stop time?
	if w.stopOnTime != nil {
		if time.Now().Before(*w.stopOnTime) {
			logger.WithFields(log.Fields{
				"_block":           "windowed-wait",
				"time-before-stop": w.stopOnTime.Sub(time.Now()),
			}).Debug("Within window, calling interval")

			m, _ = waitOnInterval(last, w.Interval)

			// check if the schedule should be ended after waiting on interval
			if time.Now().After(*w.stopOnTime) {
				logger.WithFields(log.Fields{
					"_block": "windowed-wait",
				}).Debug("schedule has ended")
				w.state = Ended
			}
		} else {
			logger.WithFields(log.Fields{
				"_block": "windowed-wait",
			}).Debug("schedule has ended")
			w.state = Ended
			m = 0
		}
	} else {
		// This has no end like a simple schedule
		m, _ = waitOnInterval(last, w.Interval)

	}
	return &WindowedScheduleResponse{
		state:    w.GetState(),
		missed:   m,
		lastTime: time.Now(),
	}
}
1332350
func (s *Secure) HandlerFuncWithNextForRequestOnly(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	// Let secure process the request. If it returns an error,
	// that indicates the request should not continue.
	responseHeader, r, err := s.processRequest(w, r)

	// If there was an error, do not call next.
	if err == nil && next != nil {
		// Save response headers in the request context
		ctx := context.WithValue(r.Context(), ctxSecureHeaderKey, responseHeader)

		// No headers will be written to the ResponseWriter.
		next(w, r.WithContext(ctx))
	}
}
1361331
func goroutineCountCheck(threshold int) error {
	count := runtime.NumGoroutine()
	if count > threshold {
		return fmt.Errorf("too many goroutines (%d > %d)", count, threshold)
	}
	return nil
}
349552
func (w *window) waitWriterBlocked() {
	w.Cond.L.Lock()
	for w.writeWaiters == 0 {
		w.Cond.Wait()
	}
	w.Cond.L.Unlock()
}
823393
func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler {
	if fn == nil {
		return c.Then(nil)
	}
	return c.Then(fn)
}
556678
func (rw *RetryWaiter) WaitIf(failure bool) <-chan struct{} {
	if failure {
		return rw.Failed()
	}
	return rw.Success()
}