// Poll removes and returns an item from the queue.
// If the queue is empty, nil will be returned.
971153
func (q *AtomicQueue) Poll() (res interface{}) {
	q.mutex.Lock()

	// if queue is not empty
	if q.wrapped || (q.tail != q.head) {
		if q.tail+1 == q.size {
			q.wrapped = false
		}
		q.tail = (q.tail + 1) % q.size
		res = q.data[q.tail]
	}

	q.mutex.Unlock()
	return res
}
634043
func (taskApi *TaskApi) Poll(id string, milliseconds time.Duration) ([]byte, error) {
	ticker := time.NewTicker(time.Millisecond * milliseconds)
	task, err := taskApi.Get(id)
	if err != nil {
		return nil, err
	}
	done := task.Completed()
	for !done {
		<-ticker.C
		task, err = taskApi.Get(id)
		if err != nil {
			return nil, err
		}
		done = task.Completed()
	}
	if task.Failed() {
		return nil, FailedTask(*task)
	}
	return task.Result, nil
}
1355812
func (c *Consumer) Poll(timeoutMs int) (event Event) {
	ev, _ := c.handle.eventPoll(nil, timeoutMs, 1, nil)
	return ev
}
617615
func (q *Queue) Empty() bool {
	q.lock.Lock()
	defer q.lock.Unlock()

	return len(q.items) == 0
}
194997
func (p *DecisionTaskPoller) Poll() (*PollForDecisionTaskResponse, error) {
	resp, err := p.client.PollForDecisionTask(PollForDecisionTaskRequest{
		Domain:       p.Domain,
		Identity:     p.Identity,
		ReverseOrder: true,
		TaskList:     TaskList{Name: p.TaskList},
	})
	if err != nil {
		log.Printf("component=DecisionTaskPoller at=error error=%s", err.Error())
		return nil, err
	}
	if resp.TaskToken != "" {
		log.Printf("component=DecisionTaskPoller at=decision-task-recieved workflow=%s", resp.WorkflowType.Name)
		p.logTaskLatency(resp)
		return resp, nil
	}
	log.Println("component=DecisionTaskPoller at=decision-task-empty-response")
	return nil, nil
}
1295463
func (p *Poller) String() string {
	str := make([]string, 0)
	for i, poll := range p.items {
		str = append(str, fmt.Sprintf("%v%v", p.socks[i], State(poll.events)))
	}
	return fmt.Sprint("Poller", str)
}
459670
func (q *Queue) Get(pos int) (interface{}, error) {
	q.Lock()
	defer q.Unlock()
	return q.get(pos)
}
1367944
func (pq *PriorityQueue) Run(ctx context.Context, f func(interface{})) {
	top := len(pq.Queues) - 1
	p := top
READ:
	for {
		q := pq.Queues[p]
		select {
		case <-ctx.Done():
			return
		case x := <-q:
			val := x.(struct {
				v interface{}
				t time.Time
			})
			f(val.v)
			metrics.GetOrRegisterResettingTimer("pq.run", nil).UpdateSince(val.t)
			p = top
		default:
			if p > 0 {
				p--
				continue READ
			}
			p = top
			select {
			case <-ctx.Done():
				return
			case <-pq.wakeup:
			}
		}
	}
}
1374313
func (q *Type) Get() (item interface{}, shutdown bool) {
	q.cond.L.Lock()
	defer q.cond.L.Unlock()
	for len(q.queue) == 0 && !q.shuttingDown {
		q.cond.Wait()
	}
	if len(q.queue) == 0 {
		// We must be shutting down.
		return nil, true
	}
	item, q.queue = q.queue[0], q.queue[1:]
	q.processing.insert(item)
	q.dirty.delete(item)
	return item, false
}
485100
func (q *Queue) Run(c *colly.Collector) error {
	wg := &sync.WaitGroup{}
	for i := 0; i < q.Threads; i++ {
		wg.Add(1)
		go func(c *colly.Collector, wg *sync.WaitGroup) {
			defer wg.Done()
			for {
				if q.IsEmpty() {
					if q.activeThreadCount == 0 {
						break
					}
					ch := make(chan bool)
					q.lock.Lock()
					q.threadChans = append(q.threadChans, ch)
					q.lock.Unlock()
					action := <-ch
					if action == stop && q.IsEmpty() {
						break
					}
				}
				q.lock.Lock()
				atomic.AddInt32(&q.activeThreadCount, 1)
				q.lock.Unlock()
				rb, err := q.storage.GetRequest()
				if err != nil || rb == nil {
					q.finish()
					continue
				}
				r, err := c.UnmarshalRequest(rb)
				if err != nil || r == nil {
					q.finish()
					continue
				}
				r.Do()
				q.finish()
			}
		}(c, wg)
	}
	wg.Wait()
	return nil
}
1102622
func (p *Prque) Pop() (interface{}, int64) {
	item := heap.Pop(p.cont).(*item)
	return item.value, item.priority
}
1304031
func (pq *txPriorityQueue) Pop() interface{} {
	n := len(pq.items)
	item := pq.items[n-1]
	pq.items[n-1] = nil
	pq.items = pq.items[0 : n-1]
	return item
}
12134
func (channel MessageQueue) Pop() (*Message, bool) {
	msg, more := <-channel
	return msg, more
}
209640
func (hd *HealthD) poll(responses chan *pollResponse) {
	var wg sync.WaitGroup
	for _, hs := range hd.hostStatus {
		wg.Add(1)
		go func(hs *HostStatus) {
			defer wg.Done()
			poll(hd.stream, hs.HostPort, responses)
		}(hs)
	}
	wg.Wait()
	responses <- nil
}
564184
func (mp *MachineProvider) poll(ctx context.Context, hostname, backend string) (*machine.ComponentsMachineProviderRpcMessagesPollResponse, error) {
	return mp.client.Poll(&machine.ComponentsMachineProviderRpcMessagesPollRequest{
		Backend:  backend,
		Hostname: hostname,
	}).Context(ctx).Do()
}
495133
func (q *Queue) Remove() interface{} {
	if q.count <= 0 {
		panic("queue: Remove() called on empty queue")
	}
	ret := q.buf[q.head]
	q.buf[q.head] = nil
	// bitwise modulus
	q.head = (q.head + 1) & (len(q.buf) - 1)
	q.count--
	// Resize down if buffer 1/4 full.
	if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) {
		q.resize()
	}
	return ret
}
21557
func (s *SeesawSync) Poll(id SyncSessionID, sn *SyncNotes) error {
	if sn == nil {
		return errors.New("sync notes is nil")
	}

	s.sync.sessionLock.RLock()
	session, ok := s.sync.sessions[id]
	s.sync.sessionLock.RUnlock()
	if !ok {
		return errors.New("no session with ID %d")
	}

	// Reset expiry time and check for desynchronisation.
	session.Lock()
	session.expiryTime = time.Now().Add(sessionDeadtime)
	if session.desync {
		// TODO(jsing): Discard pending notes?
		sn.Notes = append(sn.Notes, SyncNote{Type: SNTDesync})
		session.desync = false
		session.Unlock()
		return nil
	}
	session.Unlock()

	// Block until a notification becomes available or our poll expires.
	select {
	case note := <-session.notes:
		sn.Notes = append(sn.Notes, *note)
	case <-time.After(syncPollTimeout):
		return errors.New("poll timeout")
	}
pollLoop:
	for i := 0; i < syncPollMsgLimit; i++ {
		select {
		case note := <-session.notes:
			sn.Notes = append(sn.Notes, *note)
		default:
			break pollLoop
		}
	}

	log.V(1).Infof("Sync server poll returning %d notifications", len(sn.Notes))

	return nil
}