// goroutine safe
169822
func (p *Processor) Route(msg interface{}, userData interface{}) error {
	// raw
	if msgRaw, ok := msg.(MsgRaw); ok {
		if msgRaw.msgID >= uint16(len(p.msgInfo)) {
			return fmt.Errorf("message id %v not registered", msgRaw.msgID)
		}
		i := p.msgInfo[msgRaw.msgID]
		if i.msgRawHandler != nil {
			i.msgRawHandler([]interface{}{msgRaw.msgID, msgRaw.msgRawData, userData})
		}
		return nil
	}

	// protobuf
	msgType := reflect.TypeOf(msg)
	id, ok := p.msgID[msgType]
	if !ok {
		return fmt.Errorf("message %s not registered", msgType)
	}
	i := p.msgInfo[id]
	if i.msgHandler != nil {
		i.msgHandler([]interface{}{msg, userData})
	}
	if i.msgRouter != nil {
		i.msgRouter.Go(msgType, msg, userData)
	}
	return nil
}
784855
func (*HandlerT) Stacks() string {
	buf := new(bytes.Buffer)
	pprof.Lookup("goroutine").WriteTo(buf, 2)
	return buf.String()
}
175917
func (s *GracefulServer) StartRoutine() {
	s.lcsmu.Lock()
	defer s.lcsmu.Unlock()
	s.wg.Add(1)
	s.routinesCount++
}
460539
func (c *Client) Go(method string, args ...interface{}) chan *response {
	return c.GoWithTimeout(method, 0, args...)
}
758503
func Run(params Params, f func(T)) []Entry {
	behavior := params[BehaviorParam]
	delete(params, BehaviorParam)
	t := entryT{
		params:   params,
		behavior: behavior,
	}

	done := make(chan struct{})

	// We run the function inside a goroutine so that Fatalf can simply call
	// runtime.Goexit to stop execution.
	go func() {
		defer func() {
			if err := recover(); err != nil {
				t.Errorf("%v\n%s", err, string(debug.Stack()))
			}
			close(done)
		}()

		if runtime.Version() == "go1.5" {
			// Gnarly workaround for https://github.com/golang/go/issues/12253
			//
			// In short: A bug in Go 1.5 causes a specific form of comparison
			// (runtime.assertE2T2) to leave an invalid pointer on the stack instead
			// of zeroing it out. Usually, this isn't a problem because the function
			// returns afterwards. However, we're consistently hitting a case where
			// another function call is causing the stack to be grown while the
			// pointer is still invalid. The scanner responsible for copying the
			// stack as part of growing it runs into the invalid pointer and
			// crashes.
			//
			// To work around this, we grow the stack significantly beforehand to
			// reduce the likelihood of another growth attempt while the pointer is
			// invalid.
			growStack([1024]int64{})
		}

		f(&t)
	}()

	<-done
	return t.entries
}
1220513
func Caller(skip int) Call {
	// As of Go 1.9 we need room for up to three PC entries.
	//
	// 0. An entry for the stack frame prior to the target to check for
	//    special handling needed if that prior entry is runtime.sigpanic.
	// 1. A possible second entry to hold metadata about skipped inlined
	//    functions. If inline functions were not skipped the target frame
	//    PC will be here.
	// 2. A third entry for the target frame PC when the second entry
	//    is used for skipped inline functions.
	var pcs [3]uintptr
	n := runtime.Callers(skip+1, pcs[:])
	frames := runtime.CallersFrames(pcs[:n])
	frame, _ := frames.Next()
	frame, _ = frames.Next()

	return Call{
		frame: frame,
	}
}
1361044
func (s *Segment) flush() {
	b, _ := json.Marshal(s)
	// append so we make only one call to Write to be goroutine-safe
	s.conn.Write(append([]byte(udpHeader), b...))
}
1063749
func (e *Environment) Wait() error {
	<-e.stopCh
	if log.Enabled(log.LvDebug) {
		log.Debug("well: waiting for all goroutines to complete", nil)
	}
	e.wg.Wait()
	e.cancel() // in case no one calls Cancel

	e.mu.Lock()
	defer e.mu.Unlock()

	return e.err
}
355740
func (tms *TrustMetricStore) saveRoutine() {
	t := time.NewTicker(defaultStorePeriodicSaveInterval)
	defer t.Stop()
loop:
	for {
		select {
		case <-t.C:
			tms.SaveToDB()
		case <-tms.Quit():
			break loop
		}
	}
}
1041406
func go_callback(queueId C.int, data *C.uchar, length C.int, idx uint32, vc* VerdictContainerC) {
	xdata := C.GoBytes(unsafe.Pointer(data), length)

	var packet gopacket.Packet
	if xdata[0] & 0xf0 == ipv4version {
		packet = gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
	} else {
		packet = gopacket.NewPacket(xdata, layers.LayerTypeIPv6, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
	}

	p := NFPacket{
		verdictChannel: make(chan VerdictContainer),
		Packet: packet,
	}

	theTabeLock.RLock()
	cb, ok := theTable[idx]
	theTabeLock.RUnlock()
	if !ok {
		fmt.Fprintf(os.Stderr, "Dropping, unexpectedly due to bad idx=%d\n", idx)
		(*vc).verdict = C.uint(NF_DROP)
		(*vc).data = nil
		(*vc).length = 0
	}
	select {
		case *cb <- p:
			select {
				case v := <-p.verdictChannel:
					if v.Packet == nil {
						(*vc).verdict = C.uint(v.Verdict)
						(*vc).data = nil
						(*vc).length = 0
					} else {
						(*vc).verdict = C.uint(v.Verdict)
						(*vc).data = (*C.uchar)(unsafe.Pointer(&v.Packet[0]))
						(*vc).length = C.uint(len(v.Packet))
					}
			}

		default:
			fmt.Fprintf(os.Stderr, "Dropping, unexpectedly due to no recv, idx=%d\n", idx)
			(*vc).verdict = C.uint(NF_DROP)
			(*vc).data = nil
			(*vc).length = 0
	}
}
10518
func go_connection_error(spSession unsafe.Pointer, spErr C.sp_error) {
	sessionCall(spSession, func(s *Session) {
		s.cbConnectionError(spError(spErr))
	})
}
1016245
func Run(fns ...func() error) []error {
	wg := sync.WaitGroup{}
	errCh := make(chan error, len(fns))
	wg.Add(len(fns))
	for i := range fns {
		go func(i int) {
			if err := fns[i](); err != nil {
				errCh <- err
			}
			wg.Done()
		}(i)
	}
	wg.Wait()
	close(errCh)
	var errs []error
	for err := range errCh {
		errs = append(errs, err)
	}
	return errs
}
419189
func (e *endpoint) Close() {
	// Tell dispatch goroutine to stop, then write to the eventfd so that
	// it wakes up in case it's sleeping.
	atomic.StoreUint32(&e.stopRequested, 1)
	syscall.Write(e.rx.eventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0})

	// Cleanup the queues inline if the worker hasn't started yet; we also
	// know it won't start from now on because stopRequested is set to 1.
	e.mu.Lock()
	workerPresent := e.workerStarted
	e.mu.Unlock()

	if !workerPresent {
		e.tx.cleanup()
		e.rx.cleanup()
	}
}
931091
func (idx *TruncIndex) Iterate(handler func(id string)) {
	idx.Lock()
	defer idx.Unlock()
	idx.trie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
		handler(string(prefix))
		return nil
	})
}
1180147
func (g *GoroutinePool) Wait() error {
	g.wg.Wait()
	return g.ctx.Err()
}
646575
func (g *GoroutinePriorityPool) Wait() error {
	g.wg.Wait()
	return g.ctx.Err()
}
1003936
func (t *T) TryUntilf(
	f func() bool, timeout time.Duration, spec string, args ...interface{},
) {
	prefix := fmt.Sprintf(spec, args...) + ": "

	end := time.Now().Add(timeout)
	for time.Now().Before(end) {
		if f() {
			return
		}
		// Yield the processor so that other goroutines have a chance to work.
		// This is necessary since the function may not actually sleep at
		// all.
		runtime.Gosched()
	}

	t.Fatalf("%sTimeout after %s", prefix, timeout)
}